klinmy Posted May 16, 2009 Share Posted May 16, 2009 Hi, I'm having problem to display the selected data in the table. The codes are as follow, // To print table header of Date and Time echo " <table width='120%' border='1' cellspacing='0' cellpadding='0'>"; echo "<tr> <th bgcolor='#CCCCCC'>Date</th> <th bgcolor='#CCCCCC'>Time</th>"; // To print table header for the selected variables ($var) for ($i=0; $i<$c; $i++) { $var=$v[$i]; // the selected variable. eg: $v[0] is Height; $v[1] is Weight; echo "<th bgcolor='#CCCCCC'>$var</th>"; } // <- no problem with this echo "</tr>"; while ($row = mysql_fetch_array($result)) { echo '<tr><td align=center>' . $row['Date'] . '</td>'; echo '<td align=center>' . $row['Time'] . '</td>'; echo '<td align=center>' . $row[$var] . '</td>'; echo "</tr>"; } //end while And it's an example of the output: Date Time Height Weight 12 feb 10.00am 40 <-(it's the weight) 15 feb 11.00am 60 16 feb 12.00am 70 17 feb 11.00am 20 18 feb 01.00am 40 It displayed only the weight ($v[1]) values under Height. And when I tried putting the while loop inside the for loop, it displayed only the height values. What it should be done in order to print both selected variables (height & weight) together with the date and time into the table? thanks in advance. Link to comment https://forums.phpfreaks.com/topic/158364-display-selected-variables/ Share on other sites More sharing options...
Ken2k7 Posted May 16, 2009 Share Posted May 16, 2009 What's $c? The reason it's always showing the first entry is because you declared $var to be equal to $v[$i] in the for loop. Sure the value changes while it's in the for loop, but after the for loop is done, $var is only equal to one value, whatever that may be. So when you enter the while loop, $var's value never changes. You may want to find a way to move $var inside the while loop. Of course, I don't know what $c is or what $var is supposed to represent, so I can't tell you more than that. Good luck. Link to comment https://forums.phpfreaks.com/topic/158364-display-selected-variables/#findComment-835258 Share on other sites More sharing options...
mikr Posted May 16, 2009 Share Posted May 16, 2009 Taking what Ken2k7 said into account, and assuming that $v is an array containing fieldnames, the following would work (I added 'Date' and 'Time' to the array since they were both header names and field names according to your code). <?php $v = array('Date', 'Time', 'Height', 'Weight'); // To print table header of Date and Time echo " <table width='120%' border='1' cellspacing='0' cellpadding='0'>"; echo "<tr> // To print table header for the selected variables ($var) foreach ($v as $var) { echo "<th bgcolor='#CCCCCC'>$var</th>"; } // <- no problem with this echo "</tr>"; while ($row = mysql_fetch_array($result)) { foreach ($v as $var) { echo '<tr><td align=center>' . $row[$var] . '</td></tr>'; } } //end while ?> Link to comment https://forums.phpfreaks.com/topic/158364-display-selected-variables/#findComment-835264 Share on other sites More sharing options...
Ken2k7 Posted May 16, 2009 Share Posted May 16, 2009 mikr - I fixed your syntax error. <?php $v = array('Date', 'Time', 'Height', 'Weight'); // To print table header of Date and Time echo " <table width='120%' border='1' cellspacing='0' cellpadding='0'>"; echo "<tr>"; // To print table header for the selected variables ($var) foreach ($v as $var) { echo "<th bgcolor='#CCCCCC'>$var</th>"; } // <- no problem with this echo "</tr>"; while ($row = mysql_fetch_array($result)) { foreach ($v as $var) { echo '<tr><td align=center>' . $row[$var] . '</td></tr>'; } } //end while ?> Link to comment https://forums.phpfreaks.com/topic/158364-display-selected-variables/#findComment-835269 Share on other sites More sharing options...
mikr Posted May 16, 2009 Share Posted May 16, 2009 Ooh. Good catch. That's embarrassing. That's what I get for not running code through php -l... Link to comment https://forums.phpfreaks.com/topic/158364-display-selected-variables/#findComment-835270 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.