user1015214
user1015214

Reputation: 3081

using php to fill in an existing table

I have a blank html table (column) on my page. I have a csv file with last years info for that column available, but I don't want to initially write those values into the table cells (because there might be new info which needs to be manually entered). I would like to make a button which would populate this column. What would be the best way of going about this? Since I want the info entered dynamically I would obviously need to use an ajax call, and I would call a php file because that has a useful function fgetcsv(), but can I use this function to enter info into a existing table? I was able to use php to CREATE a table with this info, but as far as I understand, php has no knowledge of the DOM... I would have to somehow package the entire column info and have javascript do the job but I'm a little confused as to how...

Below is a snippet of how I used php to CREATE my table.

$file = fopen("SGS_Fall_Booklist.csv", "r");
$entry= fgetcsv($file);
echo $entry[0] . $_GET["program"] . $entry[1] . $GET["school"] . 
$entry[2] . $GET["term"];
echo "<br>AutoFill (last years ISBN's)<br><input id='checkall' type='checkbox' </input>";
echo "<table><tr>
     <th>Edit</th>
    <th class='coursename'>" .   $entry[6] . "</th>
    <th class='startdate'>" .  $entry[7] . "</th>
    <th class='booktitle'>" . $entry[17]. "</th>
    <th class='author'>" . $entry[18]. "</th>
    <th class='isbn'>" . $entry[16]. "</th>
    </tr>";
while(! feof($file))
{
    $entry = fgetcsv($file); 
   echo "<tr>
      <td><input type='checkbox'></input></td>
      <td class='coursename'>" . $entry[6] . "</td>
      <td class='startdate'>" . $entry[7] . "</td>
      <td class='booktitle'>" . $entry[17]. "</td>
      <td class='author'>" . $entry[18]. "</td>
      <td class='isbn'></td>
    </tr>";                                 
}

echo "</table>";
fclose($file);

Upvotes: 0

Views: 390

Answers (2)

John Zumbrum
John Zumbrum

Reputation: 2856

So here's a few things you can do:

  1. (worst) Send the entire table back in the ajax call to PHP, and then let php add your column, and return the updated table (bad plan, lots of unchanged data transferring)

  2. (better) Ajax call to return an entirely new table with your new column (better)

  3. (best) Ajax call to fetch an array or object with your data and then use javascript to iterate over the table and add the data

Pseudo code:

//do your ajax request
foreach(newColumnValues AS key=>value) {
     foreach(document.getElementByID("yourtable").elements) {
          //If the ordering can change, check the key first, otherwise you could
          //just go through every element
          if(element[0] == key) {
               element[1] = value;
          }
     }
}

Good luck!

Upvotes: 1

voldomazta
voldomazta

Reputation: 1340

If you have the code above print your ajax response you can then just do this on your javascript:

document.getElementById('container-id-here').innerHTML(your_ajax_response_text);

Upvotes: 1

Related Questions