Chris.P Posted March 20, 2011 Share Posted March 20, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/231203-outputting-tables-with-a-function/ Share on other sites More sharing options...
.josh Posted March 20, 2011 Share Posted March 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231203-outputting-tables-with-a-function/#findComment-1190020 Share on other sites More sharing options...
Chris.P Posted March 20, 2011 Author Share Posted March 20, 2011 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. $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>"; Quote Link to comment https://forums.phpfreaks.com/topic/231203-outputting-tables-with-a-function/#findComment-1190061 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.