php


jQuery looping only works on last row


I am trying to show the results of the status of a bidding item using jQuery every second on every row in MySQL table, however only the result of the last row of the table is returned.
<?php
$query = "SELECT item, description, price, imageData, status, username, item_id FROM items";
$result = mysql_query($query) or die(mysql_error());
$z=0;
while($row = mysql_fetch_array($result))
{
//echo other columns here//
echo "<td><div id=status$z></div></td>";
?>
<script type=text/javascript>
function updatestatus(itemnum)
{
var url="updatestatus.php?auc=<?php echo $row['item_id']; ?>";
jQuery('#status' + itemnum).load(url);
}
setInterval("updatestatus(<? echo $z?>)", 1000);
</script>
<?
$z++;
}
?>
When I view source in the browser, the values for #status and auc for every row are correct. What am I missing here?
Here's the code for updatestatus.php
<?php
session_start();
require_once("connect.php");
$id = $_GET['auc'];
$getstatus = mysql_query("SELECT status FROM items WHERE item_id = '$id' ");
$row = mysql_fetch_array($getstatus);
echo"$row[status]";
?>
Everything looks good, save for the fact that it looks like you're creating multiple references to your updatestatus() function.
In Javascript, if you create multiple functions with the same name, calling them will only result in one of them running, usually the first or last one (depending on the implementation), so all the code you need to run in those functions needs to sit together in one function body.
If you're determined to use the code you've created, you'd need to throw all those update calls into one function body. There would be better ways to achieve what you need, but doing it with the code you've created, this would probably work better:
<?php
$query = "SELECT item, description, price, imageData, status, username, item_id FROM items";
$result = mysql_query($query) or die(mysql_error());
$javascript = "";
$z=0;
while($row = mysql_fetch_array($result))
{
//echo other columns here//
echo "<td><div id=status$z></div></td>";
// build the javascript to be put in the function later over here...
$javascript .= "jQuery('#status". $z ."').load('updatestatus.php?auc=". $row['item_id'] ."');";
$z++;
}
?>
...and then further down the page, create the javascript (modified slightly):
<script type=text/javascript>
function updatestatus()
{
<?php echo $javascript; ?>
}
setInterval(updatestatus, 1000);
</script>
So you're basically building up the Javascript that you'll need in your function, echoing it out inside the function body, and then setting the interval will call all that code, in this case, every second.
Like I said, there are definitely more efficient ways to achieve what you're trying to do, but this should work fine for now. I hope this makes sense, but please let me know if you need any clarity on anything! :)
I don't see that you're populating data using a incrementor. Is this supposed to be adding content to a page or replacing the content? from what it looks like it will just display one item, and then replace that one item with the next until it's done, which is why you see only the last row.
OR ...
the jquery update isn't being fed the $i variable .. change the function to
function updatestatus(itemnum) {
and then jquery echo to jQuery('#status' + itemnum).load(url);
then you can add the on-click/ or whatever to include the number
onclick='updatestatus("1")'
on the other hand you might be needing to pass the total number of items to the function and then creating an if there to cycle through them (not tested, but you get the idea i hope)
function updatestatus(numitems) {
var url = "";
var itemID = "";
for (i = 1; i <= numitems; i++) {
itemid = getElementById('#status'+numitems).getAttribute("itemID")
url="updatestatus.php?auc="+itemID;
jQuery('#status'+numitems).load(url);
}
}
setInterval("updatestatus()", 1000);
and the html element for "#status1" as created by the PHP should look like this:
<div id="status1" itemid="23455">
</div>

Related Links

Make app configurable for different customers
PHP- correct use of quotation marks inside each other [duplicate]
PHP giving weird result String/Array
PHP Codeigniter- Allow users to use a custom domain name
PHPMailer with SMTP details
Insert value of dynamically created check boxes
Unable to access global variable which is set in functions.php
PHP: PATCH method fails when others succeed
How to install Mod_Auth_Mellon with dependencies on Ubunto 16.10?
Should I separate my db model into object-specific and table-specific actions?
Returning first x items from array : 3
Custom rule validation not working in Yii2 Contact Form extends Model
How to convert json key in lower case in php
Summerize all fields if exists from database
Apache worker with Php-FPM In SUSE
How to decrease limit RAM for php-cgi?

Categories

HOME
xpath
user-interface
android-fragments
applescript
swi-prolog
devexpress
filesize
theano
whmcs
mongoid
gallery
header
google-sheets-query
greasemonkey
clone
phpseclib
mailing-list
port
fireloop
amazon-emr
phpstorm-2017.1
cname
jquery-select2-4
grocery-crud
squid
orange-api
pywinauto
fosuserbundle
checkout
coroutine
deepfreeze
sqlplus
dashdb
web-analytics
react-dnd
tracing
beego
uilocalnotification
math.js
ccavenue
php-5.3
amazon-cloudtrail
delayed-job
epplus
custom-keyboard
bean-validation
cloudera-quickstart-vm
xll
llvm-ir
suds
word-cloud
web-inspector
latexml
zend-form
code-push
absolute
rm
xcglogger
scala-breeze
uiautomatorviewer
asp.net-web-api-routing
mongodb-aggregation
nivo-slider
portability
emf-compare
http4s
nsrunloop
wininet
wolfram-language
deferred-rendering
ajaxmin
gwt-syncproxy
i2b2
flipclock
mixins
canopy
phpredis
clarion
shell-extensions
nhibernate-criteria
dc
smacss
zend-mail
qt5.4
windows-rt
webmethod
application-blocks
mousemotionlistener
virtual-channel
void-pointers
jquery-cycle
dojo-1.9
gprof
semantic-merge
nsmutablestring
coordinate
trialware
navigationcontroller
affinetransform
nsrangeexception
upshot
asp.net-mvc-views
libxslt
git-log
raw-data

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App