Jump to content

[SOLVED] I need a simple way to populate a table


jeff5656

Recommended Posts

I want to display an html table where each column has 31 rows.  Each of these rows is a variable that only differs by having 1-31 at the end.  I have more than 10 columns, each of which has 31 variables as well.  How do I do this simply without having to type it all in?

 

Here would be the manual way:

 

<?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());

<table>
<td> <?php echo $row['icufellow1']; ?></td>
<td <?php echo $row['icufellow2']; ?></td>
<td <?php echo $row['icufellow3']; ?></td>
etc
<tr>
<td <?php echo $row['staff1']; ?></td>
<td <?php echo $row['staff2']; ?></td>
etc
</tr>
</table>


I would do it by putting the list of variable names in an array. Then use this code:

 

$varArray = array("icufellow","staff","column3","column4","etc");
$varArraySize = count($varArray);
echo "<table>";

for ($i = 0; $i < varArraySize;$i++) {
   echo "<tr>";
   for ($f = 1; $f < 32; $f++) {
     echo "<td> " . $row[$varArray[$i] . $f] . "</td>";
   }
   echo "</tr>";
}

echo "</table>";

 

Don't forget to add your SQL code

 

@DarkWater Thanks for the tip

 

-Brandon

@Guardian-Mage: Put the result of count() in a separate variable rather than calling it on every iteration.  It's much faster doing something like this:

$count = count($varArray);
for($i=0;$i<$count;$i++) {

}

 

@jeff5656: Why do you have a table designed like that?  That's pretty umm...unoptimized.  I'd honestly suggest reading up on database normalization.

Why do you have a table designed like that?  That's pretty umm...unoptimized.  I'd honestly suggest reading up on database normalization.

I agree it is quite ridiculous that each record has 279 variables, but I couldn't figure out how to do it better.  I should read about normalization.  I am vaguely familier with the topic but not enough to figure out how to do mine.

When I do this, the screen is blank.  I even echoed "test" inside each <td> and it is not displayed.

 

<?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());

$varArray = array("icufellow","icustaff","f2staff","intervent","wb","tx","phtn","warren","wn");
$varArraySize = count($varArray);

echo "<table>";

for ($i = 0; $i < varArraySize;$i++) {
   echo "<tr>";
   for ($f = 1; $f < 32; $f++) {
     echo "<td> " . $row[$varArray[$i] . $f] . "test" . "</td>";
   }
   echo "</tr>";
}

echo "</table>";

?>

Does anyone have any idea why the above code gives me a blank page?  How would I debug this with included array?

 

Add this at the top of your script:

// Report all PHP errors
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

Here are the errors generated:

 

Notice: Use of undefined constant F - assumed 'F' in /home/content/j/e/f/jeff/html/schedules/schedtest.php on line 14

 

Notice: Use of undefined constant varArraySize - assumed 'varArraySize' in /home/content/j/e/f/jeff/html/schedules/schedtest.php on line 27

Thanks that worked, but now I get this error (it displays many times since there are so many variables in the record):

 

Notice: Undefined variable: row in /home/content/j/e/f/jeff/html/schedules/schedtest.php on line 31

 

Notice: Undefined variable: row in /home/content/j/e/f/jeff/html/schedules/schedtest.php on line 31

 

 

But row is the individual variables in the array.

 

Like: echo $row['icufellow1']; for the variable icufellow1.

 

I thought I defined this by doing the msql query and pulling that record.

 

oops forgort the while loop....will repost if it doesnt work :)

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.