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

How to display weekly(Mon to Sun) data in php and mysql?
Escape double quotes of HTML attributes output by PHP
PHP Startup: Timezone database is corrupt
I need to collect an array of classes to call their static variables in php
[mysql&php], what is the first day of a week, Monday or Sunday?
Documenting a PHP extension with PHPdoc
Convert a delphi TColor with PHP
Performance of Empty Functions in PHP
Visualise OO PHP code
How to highlight changes/difference in one text paragraph from the other?
Does the order of cases matter in PHP switch statements?
Add row in one table for every nth entry in another table
unbindModel call in CakePhp. How does it work?
mysql date storage & query performance with php
Page encoding and Jquery-Ui (ajax tabs)
how to register user clicked on link ads

Categories

HOME
oracle
knockout.js
apache-nifi
clearcase
hdfs
cryptography
haxe
capistrano
supercollider
joomla3.2
port
colors
android-externalstorage
pushdown-automaton
uima
coroutine
pfobject
cmake-gui
spring-ldap
connection-refused
qa
chronicle-queue
opentext
tortoisemerge
unrar
gravity-forms-plugin
d3.js-v4
xenapp
dotcms
infopath2010
steam-web-api
python-2.x
xcrun
actframework
cloveretl
image-optimization
directfb
polyml
rails-postgresql
testlink
cargo
installanywhere
cloudera-quickstart-vm
elastica
reset
suds
optionaldataexception
contactless-smartcard
latexml
code-push
wysihtml5
cyanogenmod
return-type
aws-kinesis-firehose
libreoffice-writer
scala-breeze
beeline
nusoap
entropy
sonatype
aurelia-fetch-client
glimpse
alertify
swiperefreshlayout
emf-compare
kendo-treeview
gawk
oracle-fusion-apps
allegro
multi-touch
fuzzer
dpkg
adp
node-glob
simian
psd
place
modern.ie
google-gdk
subview
cout
inputbox
sqlbindparameter
tabletools
profiles
rautomation
codahale-metrics
postgres-xc
spiral
jbox2d
session-0-isolation
cyrillic
audiotoolbox
kobold2d
microblogging
data-dump
kohana-auth
pyjamas
3-tier
odac
raw-data
fxruby
data-retrieval
twitter-feed
watchpoint

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App