Jump to content

Control Structure Confusion


Arnsenal

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/263502-control-structure-confusion/
Share on other sites

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

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.

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.