Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.