Page 1 of 1
AJAX- Update Grid from array
Posted: Wed Aug 06, 2008 10:50 am
by cirom
HI!
I'm a newbie of this forum so I hope to not be "off topic".
My problem is this:
Can I update only some cells of my table?
I build my table from a bi-dimenional php array.
Sometimes only few field of this array change and I would like to update only the relative cells of the table.
Is it possible?
Thank for your help!
Re: AJAX- Update Grid from array
Posted: Wed Aug 06, 2008 10:56 am
by Chalks
It's certainly possible. We would need to see the code you use to build the table and a rough idea of what would be changing (and when) before we can give you more than just general help. How familiar are you with ajax and page manipulation using javascript?
Re: AJAX- Update Grid from array
Posted: Wed Aug 06, 2008 11:40 am
by cirom
Thanks for your help.
I must confess that I am looking forward to know the solution.
I've read a lot of thing in the internet and I think that the solution is in Ajax.
The code is something like that:
Code: Select all
<?php
echo '<table>';
for ($i=0; $i<count($array);$i++){
echo "<tr>";
echo "<td><div id='cell'.$i.'1'>";
print $array[$i]['ID-MATCH'];
echo "</div></td>";
echo "<td><div id='cell'.$i.'2'>";
print $array[$i]['RESULT'];
echo "</div></td>";
echo "<td><div id='cell'.$i.'3'>";
print $array[$i]['WHERE'];
echo "</div></td>";
echo "<td><div id='cell'.$i.'4'>";
print $array[$i]['WHEN'];
echo "</div></td>";
echo "<td><div id='cell'.$i.'5'>";
print $array[$i]['NOTE'];
echo "</div></td>";
echo "</tr>";
}
echo "</table>" ;
?>
Every cells has got its ID, so I can immediately identify where I have the put the new value.
So As soon As the a field of the php array changes, the server PUSH the only new value to the client and this put the new value in the exact cell.
Re: AJAX- Update Grid from array
Posted: Wed Aug 06, 2008 12:23 pm
by Chalks
cirom wrote:Every cells has got its ID, so I can immediately identify where I have the put the new value.
So As soon As the a field of the php array changes, the server PUSH the only new value to the client and this put the new value in the exact cell.
That's excellent, and makes this much easier to do. I just yesterday wrote a script that is fairly similar to what you need, as soon as I get home (at work now... I'm done in 45 minutes) I'll post it over here.
To get you started in the interim, take a look at
this example of using AJAX. You're going to want to be pulling xml with ajax, and use the xml to populate the form (using javascript).
I am a little bit confused by your for loop... why does it display so many divs with the same id? Shouldn't each div have a unique id? EDIT: never mind, I understand it now... just had to look a little closer.
Re: AJAX- Update Grid from array
Posted: Wed Aug 06, 2008 1:53 pm
by cirom
Yes every cell has its unique ID.
In the interim I'll take a look at your example.
Re: AJAX- Update Grid from array
Posted: Wed Aug 06, 2008 5:45 pm
by pickle
I think using JSON might be easier here, though admittedly I've never accessed XML with Javascript.
Get your PHP script (the script you'll call via AJAX) to generate an array. The keys would be the ids of the cells to update, and the values would be the new values. Run it through json_encode() & output it.
In your Javascript then, iterate through the properties of the returned object (json_encode() will turn your array into a string that Javascript will treat as an object) & for each property, update the appropriate cell.
Re: AJAX- Update Grid from array
Posted: Thu Aug 07, 2008 4:23 am
by cirom
I can't use JSON.
I'm obliged to use this array.
However Can I see a demo and the code?
I have thought to create a php page that controls every two second,on the server, if the newest array has changed.
Then it values in which "ID cell" it has to push the new value.
I think to be capable to create this page but I don't know in which way I can push the update.
Re: AJAX- Update Grid from array
Posted: Thu Aug 07, 2008 1:59 pm
by pickle
Well you can't transfer & access a PHP array. You're going to have to convert it into something else in order to access it via Javascript.
Re: AJAX- Update Grid from array
Posted: Fri Aug 08, 2008 6:44 am
by cirom
ok.
Before converting in Json or Xml can I see the CODE that update the page?
Re: AJAX- Update Grid from array
Posted: Fri Aug 08, 2008 9:38 am
by pickle
I don't understand your question.
Re: AJAX- Update Grid from array
Posted: Sun Aug 17, 2008 4:13 am
by cirom
If it is possibile I would read your code then I can use Json or Xml.
THXS