jeff5656 Posted November 30, 2008 Share Posted November 30, 2008 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> Quote Link to comment Share on other sites More sharing options...
Guardian-Mage Posted November 30, 2008 Share Posted November 30, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 @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. Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted November 30, 2008 Author Share Posted November 30, 2008 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. Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted November 30, 2008 Author Share Posted November 30, 2008 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>"; ?> Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted November 30, 2008 Author Share Posted November 30, 2008 Does anyone have any idea why the above code gives me a blank page? How would I debug this with included array? Quote Link to comment Share on other sites More sharing options...
flyhoney Posted November 30, 2008 Share Posted November 30, 2008 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); Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted November 30, 2008 Author Share Posted November 30, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 You have date(F). It should be date('F'). Also, your for loop should read: for ($i = 0; $i < $varArraySize;$i++) { Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted November 30, 2008 Author Share Posted November 30, 2008 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 Quote Link to comment Share on other sites More sharing options...
revraz Posted November 30, 2008 Share Posted November 30, 2008 Because you don't define $row here echo "<td> " . $row[$varArray[$i] . $f] . "test" . "</td>"; Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted November 30, 2008 Author Share Posted November 30, 2008 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 Quote Link to comment 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.