Jump to content

Outputting tables with a function?


Chris.P

Recommended Posts

At the moment I have multiple pages with different HTML tables coded in (some have 5 columns and some 6 etc) and PHP echoing out data to populate from the database. The table in each page holds different data. It looks messy to me to code a different table for every page so I'm wondering if it's possible to create a function to do this? The function would have to print tables with different amounts of columns and echo out different data depending on what the page is along with table headers etc. How would I go about that?  :confused:

Link to comment
https://forums.phpfreaks.com/topic/231203-outputting-tables-with-a-function/
Share on other sites

largely depends on how you are getting the data from the database as to what you actually wanna do in the function, but for example sake let's assume you have a 2d array of data retrieved from database and it only contains data to actually be displayed:

 

function getTableFormat($data) {
  $table = '<table>';
  foreach ($data as $row) {
    $table .= '<tr>';
    foreach ($row as $column) {
      $table .= '<td>' . $column . '</td>';
    }
    $table .= '</tr>';
  }
  $table .= '</table>';
  return $table;
}

$data = array();
$data[] = array('id' => 1, 'name' => 'john');
$data[] = array('id' => 2, 'name' => 'betty');
$data[] = array('id' => 3, 'name' => 'sue');

$tableFormat = getTableFormat($data);

 

This will return generic html table formatted data, and the $data array can be any number of rows/columns.

Thanks for the reply! I'm a bit of a noob so I'm going to have to study what you just said a little more before I get my head around it. I'm currently doing each page like this which looks like a different way of outputting to how you did it.  :shy:

 

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

									if (mysql_num_rows($result) > 0) {

										echo "<table>";

										echo "<tr class='titles'>";

										echo "<td><h2><a href='note.php?order=date_added'>Date Added</a></h2></td>";
										echo "<td><h2><a href='note.php?order=note_description'>Note Description</a></h2></td>";
										echo "<td><h2><a href='note.php?order=added_by'>Added By</a></h2></td>";
										echo "<td><h2><a href='note.php?order=note_status'>Done?</a></h2></td>";
										echo "<td><h2>Delete</h2></td>";

										echo "</tr>";

										while($row = mysql_fetch_assoc($result)) { 

										$itemid = $row['note_id'];

										echo "<tr class='$rowclass'>";

										//echo "<td><p>".$row['item_id']."</p></td>";
										echo "<td>".$row['date_added']."</td>"; 
										echo "<td>".$row['note_description']."</td>";
										echo "<td>".$row['added_by']."</td>";
										echo "<td>".$row['note_status']."</td>";
										echo "<td><a href='?deleteitem=$itemid' onclick='return delete_me()' value='delete me box'>Delete</a></td>";
										echo "</tr>";


									} 
									echo "</table>"; 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.