Arnsenal Posted June 1, 2012 Share Posted June 1, 2012 Hello Masters The following code has an issue with the control structure I believe. My Goal: out put all the mysql data into an html table What I have correct: 1. The MySQL column names correctly fill in the top table row. 2. The number of rows displayed == the numbers of rows in my MySQL table. What I have Inncorrect: 1. The data in the table is repeatedly filled with the first row of data in MySQL. Here is the Code. class display_run_data { function __construct($mysqli){ $query="SELECT * FROM run_data"; $result=$mysqli->query($query); $num=$result->num_rows; $row = $result->fetch_row(); $feilds = $mysqli->field_count; echo"<tr>"; while ($finfo = $result->fetch_field()) { echo "<td><a href=\"index.html?where=".$finfo->name."\">".strtoupper($finfo->name)."</a></td>"; } echo"</tr>"; for ($i=0;$i<=$num;$i++){ echo "<tr>"; foreach ($row as $key=>$value){ echo "<td>".$value."</td>"; } echo "</tr>"; } } } Again the problem is: The data seen in the html table is repeatedly filled with only the first row of data from the MySql table. Tough problem to find solutions for on Google. Thanks, Jerry Quote Link to comment https://forums.phpfreaks.com/topic/263502-control-structure-confusion/ Share on other sites More sharing options...
Barand Posted June 1, 2012 Share Posted June 1, 2012 You only fetch one row then repeatedly output the values Quote Link to comment https://forums.phpfreaks.com/topic/263502-control-structure-confusion/#findComment-1350377 Share on other sites More sharing options...
jcbones Posted June 1, 2012 Share Posted June 1, 2012 <?php //for syntax highlighting class display_run_data { function __construct($mysqli){ $query="SELECT * FROM run_data"; $result=$mysqli->query($query); $num=$result->num_rows; //$row = $result->fetch_row(); //this selects the row, it needs to be in a while loop. $feilds = $mysqli->field_count; echo"<tr>"; while ($finfo = $result->fetch_field()) { echo "<td><a href=\"index.html?where=".$finfo->name."\">".strtoupper($finfo->name)."</a></td>"; } echo"</tr>"; while($row = $result->fetch_row() ) { //maybe here. echo "<tr>"; foreach ($row as $key=>$value){ echo "<td>".$value."</td>"; } echo "</tr>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/263502-control-structure-confusion/#findComment-1350384 Share on other sites More sharing options...
Arnsenal Posted June 1, 2012 Author Share Posted June 1, 2012 Thanks Guys, Ill try this out Monday. Ill consider the topic solved. Thanks Again Quote Link to comment https://forums.phpfreaks.com/topic/263502-control-structure-confusion/#findComment-1350421 Share on other sites More sharing options...
Arnsenal Posted June 4, 2012 Author Share Posted June 4, 2012 JC Bones The example replacement code you gave we works except that its seems the internal pointer of the $row array is always advanced 1 row when the for each statement executes. So this means that the first row is never printed out to the html table. I've tried using prev($row) right before the for each statement, but it had no affect. Any thoughts? Never Mind!! I didn't comment out the first $row = $result->fetch_row(); All works great. Quote Link to comment https://forums.phpfreaks.com/topic/263502-control-structure-confusion/#findComment-1351118 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.