Jump to content

Display selected variable(s)


klinmy

Recommended Posts

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

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.

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
?>

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
?>

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.