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
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.

Link to comment
Share on other sites

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>"; 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.