PHP loop not looping through

Associate
Joined
4 Feb 2014
Posts
12
Hi Guys,

Im trying to loop through some code which builds up queries made and then prints out the data in their tables. I have a search function that the user can build up a query and then the results are presented in a table. I have a lot of tables which i want to loop through and print the results based on the query selected by the user. The table name are put into an array and i want to loop through and get the table names from the array. It is working for the first table which it prints out the results into a table but wont loop through for any of the others. Probably doing something stupid but here is my code:

Code:
$tables= array("table1","table2","table3");
//$arrlength=count($tables);

for($i=0; $i < count($tables); $i++) 
 
{
	$QBuild = "";
	$QBuild .= "SELECT * FROM ($tables[$i]) WHERE($field)";
	
		if($Date1 && $Date2 != NULL){
			$QBuild .= "AND (Date BETWEEN '$Date1' AND '$Date2') ";
		}
	
		if($Initials != NULL){
			$QBuild .= "AND (Initials LIKE '$Initials')" ;
		}
	
			if($PassFail != NULL){
			$QBuild .= "AND (PassFail LIKE '$PassFail')" ;
		}
		
		if($circuit != NULL){
			$QBuild .= "AND (circuit LIKE '$circuit')" ;
		}
	$result = mysql_query($QBuild);
	
		// Prints the columns
		echo "<table class='searchTable' border='1';  <tr>"; 
		for($i = 0; $i < mysql_num_fields($result); $i++) {     
		$field_info = mysql_fetch_field($result, $i);     
		echo "<th>{$field_info->name}</th>"; }  
		
		// Print the data 
		
		while($row = mysql_fetch_row($result)) {     
		echo "<tr>";     
			foreach($row as $_column) {         
				echo "<td>{$_column}</td>";     }     
				echo "</tr>"; 
				}  
				
				echo "</table>";
		
		 
		 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
		 $anymatches=mysql_num_rows($result); 
		 if ($anymatches == 0) 
		 { 
		 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
		 } 
		 
		
}

 }

As i said the code works fine for the first table in the array but wont loop through.

Could someone help me please??:)
 
Associate
Joined
8 Aug 2008
Posts
302
You're resetting $i here:

Code:
for($i = 0; $i < mysql_num_fields($result); $i++) {
$field_info = mysql_fetch_field($result, $i);

switch to:

Code:
for($j = 0; $j < mysql_num_fields($result); $j++) {     
$field_info = mysql_fetch_field($result, $j);
 
Associate
OP
Joined
4 Feb 2014
Posts
12
You're resetting $i here:

Code:

for($i = 0; $i < mysql_num_fields($result); $i++) {
$field_info = mysql_fetch_field($result, $i);

switch to:

Code:

for($j = 0; $j < mysql_num_fields($result); $j++) {
$field_info = mysql_fetch_field($result, $j);

Ah man didnt even notice that! lol

Working now, Thanks :)
 
Soldato
Joined
26 Aug 2012
Posts
4,399
Location
North West
Just thought I'd give you some advice. Don't use single letter variables. Correct proper variable names i.e. 'table_counter_loop1' it will make your code much easier to debug and stop errors like this happening.
 
Associate
Joined
10 Nov 2013
Posts
1,808
Have to disagree there. I think it's personal preference but single letter variables for loop counters is often more readable than using an elaborate name. If you're using nested loops then you may want to make it more explicit, but to say 'never' use single letters is a bit excessive.

I think some better advice in this instance would be to use foreach loops instead of for loops.
 
Back
Top Bottom