AJAX- Update Grid from array

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
cirom
Forum Newbie
Posts: 6
Joined: Wed Aug 06, 2008 10:43 am

AJAX- Update Grid from array

Post 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!
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: AJAX- Update Grid from array

Post 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?
cirom
Forum Newbie
Posts: 6
Joined: Wed Aug 06, 2008 10:43 am

Re: AJAX- Update Grid from array

Post 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.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: AJAX- Update Grid from array

Post 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.
cirom
Forum Newbie
Posts: 6
Joined: Wed Aug 06, 2008 10:43 am

Re: AJAX- Update Grid from array

Post by cirom »

Yes every cell has its unique ID.

In the interim I'll take a look at your example.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: AJAX- Update Grid from array

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cirom
Forum Newbie
Posts: 6
Joined: Wed Aug 06, 2008 10:43 am

Re: AJAX- Update Grid from array

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: AJAX- Update Grid from array

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cirom
Forum Newbie
Posts: 6
Joined: Wed Aug 06, 2008 10:43 am

Re: AJAX- Update Grid from array

Post by cirom »

ok.
Before converting in Json or Xml can I see the CODE that update the page?
Last edited by cirom on Fri Aug 08, 2008 2:24 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: AJAX- Update Grid from array

Post by pickle »

I don't understand your question.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cirom
Forum Newbie
Posts: 6
Joined: Wed Aug 06, 2008 10:43 am

Re: AJAX- Update Grid from array

Post by cirom »

If it is possibile I would read your code then I can use Json or Xml.
THXS
Post Reply