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
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 :)
 
Back
Top Bottom