0

I am trying to use data from an excel spreasheet to populate an html table using php. I am a beginner at PHP. I have tried to use code from other questions, and they were close but not quite what I needed. The excel document will be periodically updated by another person.

Here's an example of code I've used:

$file = file("/calendar.txt");

print "<table> 
<tr><td>Date</td><td>Start Time</td><td>Venue</td><td>Description</td></tr>";

foreach($file as $line){ 
$line = trim($line); 
$split = mb_split("\t",$line); 

print "<tr><td>$split[3]</td><td>$split[4]</td><td>$split[5]</td><td>$split[6]</td></tr>"; 
}

print "</table>";

?> 

But the above example does not allow for auto-population. So I tried this:

<table>
<?php
if (($handle = fopen("/calendar.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
      $num = count($data);
      for ($c=0; $c < $num; $c++) {
        print "<tr><td> $data[$c] </td></tr>";
    }

}
fclose($handle);
}
?> 
</table>

But I couldn't get the columns I wanted. Plus both examples did not allow for a new row/column created at the end of the last column from the source file (i.e. the data from the last column in the first row is combined with the first column of the second row).

I would also like to echo the line, "There are no upcoming dates currently. Please check back soon!" if there is no information to display. And is there a way to do a colspan in php? Here are my failed attempts: http://www.tonejones.com/calendar3.php
I want the table to look like this: http://www.tonejones.com/calendar.php

2
  • Is your file tab-delimited or comma-delimited? Your first example suggests tab, the second comma. Commented Mar 8, 2012 at 23:22
  • I tried both tab-delimited and comma-delimited... I have no preference. I was just trying to find the best solution. Commented Mar 8, 2012 at 23:37

2 Answers 2

1

To populate Data from Excel to Table. First We need to retrieve all data into Array then we will render all Array values into table.Get reference to retrieve data into array. https://www.studytutorial.in/how-to-upload-or-import-an-excel-file-into-mysql-database-using-spout-library-using-php. IF you get array then use below code

<table>
   <?php foreach($rows as $value){ ?>
  <tr>
    <td><?php echo $value; ?></td>
  </tr>
  <?php } ?>
</table>
Sign up to request clarification or add additional context in comments.

Comments

0

Your block should go around the collection of cells, not each individual cell:

print "<table>    
<tr><td>Date</td><td>Start Time</td><td>Venue</td><td>Description</td></tr>";
<?php 
if (($handle = fopen("/calendar.csv", "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) { 
        $num = count($data); 
        print "<tr>";
        for ($c=3; $c < $num; $c++) { 
            print "<td> $data[$c] </td>"; 
        } 
        print "<tr>"; 
    } 
    fclose($handle); 
} else {
    print "<tr><tdcolspan="4">
              There are no upcoming dates currently. Please check back soon!
           </td></tr>";
}
?>  
</table>

1 Comment

Thanks Mark! However, that created only one row. And I still have the issue where data from two cells in the source file are sharing one cell. I'm sure that's b/c there is not a comma created for the end of the row when I export the file to a csv. Is there a better way to get the results I want?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.