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>


Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

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.