Jump to content

Populating a table by rows


jeff5656

Recommended Posts

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

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. 

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

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

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.