jeff5656 Posted November 30, 2008 Share Posted November 30, 2008 I got great help on this code and it works but now I want to display it correctly. The way this code works is it goes across from <td> to <td> for each variable then down a row <tr>. However I want it to display UNDER each column, instead of going across. In the code, I have <td> and </td> so I know this is why it goes across but how do I make it go down but then come back to the top for the next column? In this example column 1 is "which_date" and column 2 is "icufellow". See screenshot. <?php include ("../connectdb.php"); $next_month= date("F", strtotime("+1 month")); $curr_month = date ('F'); $consultsq1 = "SELECT * FROM `staffsched` WHERE `which_month`='".$curr_month."'"; $results = mysql_query ($consultsq1) or die (mysql_error()); while ($row = mysql_fetch_assoc ($results)) { $varArray = array("which_date","icufellow","icustaff","f2staff","intervent","wb","tx","phtn","warren","wn"); $varArraySize = count($varArray); echo "<table border='1' width='100%'>"; echo "<th width='2%'>Date</th><th>icufellow</th><th>icustaff</th><th>f2staff</th><th>intervent</th><th>wb</th><th>tx</th><th>phtn</th><th>warren</th><th>wn</th>"; for ($i = 0; $i < $varArraySize;$i++) { echo "<tr>"; for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; } echo "<td>"; echo "</tr>"; } echo "</table>"; } ?> See the sample screenshot of how it goes ACROSS instead of down each column See that first row with the numbers should be going DOWN under date and then "Quddus" should be under "icufellow" and NEXT TO "1". [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/134918-populating-a-table-by-rows/ Share on other sites More sharing options...
.josh Posted November 30, 2008 Share Posted November 30, 2008 You're gonna have to start with only assigning your data to an array in your while loop, because there's no way you can build your table like that from inside that loop. Then you're going to have to make some nested loops to create some nested tables. In short, that's going to involve a complete code rewrite. Link to comment https://forums.phpfreaks.com/topic/134918-populating-a-table-by-rows/#findComment-702631 Share on other sites More sharing options...
jeff5656 Posted December 1, 2008 Author Share Posted December 1, 2008 I solved it this way: <?php // Report all PHP errors error_reporting(E_ALL); // Same as error_reporting(E_ALL); ini_set('error_reporting', E_ALL); include ("../connectdb.php"); $next_month= date("F", strtotime("+1 month")); $curr_month = date ('F'); $consultsq1 = "SELECT * FROM `staffsched` WHERE `which_month`='".$curr_month."'"; $results = mysql_query ($consultsq1) or die (mysql_error()); echo "<table border='1' width='100%'>"; echo "<th width='2%'>Date</th><th>icufellow</th><th>icustaff</th><th>f2staff</th><th>intervent</th><th>wb</th><th>tx</th><th>phtn</th><th>warren</th><th>wn</th>"; while ($row = mysql_fetch_assoc ($results)) { echo "<tr><td><table>"; $varArray = array("which_date"); $varArraySize = count($varArray); for ($i = 0; $i < $varArraySize;$i++) { for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; echo "<tr>"; } } echo "</table></td>"; echo "<td valign='top'><table>"; $varArray = array("icufellow"); $varArraySize = count($varArray); for ($i = 0; $i < $varArraySize;$i++) { for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; echo "<tr>"; } } echo "</table></td>"; echo "<td valign='top'><table>"; $varArray = array("icustaff"); $varArraySize = count($varArray); for ($i = 0; $i < $varArraySize;$i++) { for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; echo "<tr>"; } } echo "</table></td>"; echo "<td valign='top'><table>"; $varArray = array("f2staff"); $varArraySize = count($varArray); for ($i = 0; $i < $varArraySize;$i++) { for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; echo "<tr>"; } } echo "</table></td>"; echo "<td valign='top'><table>"; $varArray = array("intervent"); $varArraySize = count($varArray); for ($i = 0; $i < $varArraySize;$i++) { for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; echo "<tr>"; } } echo "</table></td>"; echo "<td valign='top'><table>"; $varArray = array("wb"); $varArraySize = count($varArray); for ($i = 0; $i < $varArraySize;$i++) { for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; echo "<tr>"; } } echo "</table></td>"; echo "<td valign='top'><table>"; $varArray = array("tx"); $varArraySize = count($varArray); for ($i = 0; $i < $varArraySize;$i++) { for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; echo "<tr>"; } } echo "</table></td>"; echo "<td valign='top'><table>"; $varArray = array("phtn"); $varArraySize = count($varArray); for ($i = 0; $i < $varArraySize;$i++) { for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; echo "<tr>"; } } echo "</table></td>"; echo "<td valign='top'><table>"; $varArray = array("warren"); $varArraySize = count($varArray); for ($i = 0; $i < $varArraySize;$i++) { for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; echo "<tr>"; } } echo "</table></td>"; echo "<td valign='top'><table>"; $varArray = array("wn"); $varArraySize = count($varArray); for ($i = 0; $i < $varArraySize;$i++) { for ($f = 1; $f < 32; $f++) { echo "<td> " . $row[$varArray[$i] . $f] . "</td>"; echo "<tr>"; } } echo "</table></td>"; echo "</table>"; } ?> Link to comment https://forums.phpfreaks.com/topic/134918-populating-a-table-by-rows/#findComment-702647 Share on other sites More sharing options...
Barand Posted December 1, 2008 Share Posted December 1, 2008 I'd do it like this (data from Data Joins tutorial) <?php mysql_connect ('localhost'); mysql_select_db('jointute'); $sql = "SELECT pupil_name FROM pupil ORDER BY pupil_name"; $res = mysql_query($sql); $recs = mysql_num_rows($res); $numcols = 3; $rows = ceil($recs/$numcols); $k = 0; while ($row = mysql_fetch_row($res)) { if ($k % $rows == 0) { if ($k > 0) echo "</div>"; echo "<div style='float: left; width: 30%'>"; } echo $row[0], '<br/>'; $k++; } echo "</div>"; ?> --> Adam Simms Gearge Wilson Mary Blake Allan Blair Henry Irving Mary Sheldon Anna Hamilton Jane Morrison Mary Whitehouse Anne Bailey John Patterson Michael Grove Anthony Bell John Tully Peter Adamson Caroline Freeman John Watson Peter Appleby David Powell John Williams Wayne Jones Emma Watson Margaret Norton William Smith Link to comment https://forums.phpfreaks.com/topic/134918-populating-a-table-by-rows/#findComment-702685 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.