Jump to content

Loop problem when trying to Query multiple MySQL Tables


suttercain

Recommended Posts

Hi everyone,

 

I will try and explain this as best I can. I have a database with tables separated by year (exp. 1994data, 1995data, 1996data, etc.). I am trying to perform a search that will allow the user to enter a value, iterate through the tables and echo the result. Here is the code I am using:

 

<?php
if (isset($_POST['searchdecs']) && $_POST['decs'] != '') {  //Verify thet a searchtype was selected!
require('get_connected.php');
$year_arr = array("1994","1995","1996");
        $arr_size = count($year_arr);

	//If the USER Searches by EFN
	if (isset($_POST['decs'])) {
	$decs = mysql_real_escape_string($_POST['decs']);
	$flag = 0;
              for ($i = 0; $i < $arr_size; $i++) 
              {
                //SQL Statement to SELECT EFN
                $sql = mysql_query("SELECT * FROM " .$year_arr[$i]. "data WHERE DECSFamilyName = '$decs' ORDER BY level DESC") or die(mysql_error());
		  	$total = mysql_num_rows($sql);
			if ($total > 1) {
			echo "<div class=\"results\">$total results matching $decs.</div><br />"; 
			}
			if ($total == 1) {
			echo "<div class=\"results\">$total result matching $decs.</div><br />"; 
			}
			if ($total > 0) {
			$flag = 1;
			while ($row = mysql_fetch_array($sql)) {
			include('verdevresults.php'); //The results layout
			}
		  }	
	   }
	   if ($flag == 0) {
              echo "Sorry, no records were found matching $decs.";
           }
        }
     }
?>

 

The problem I am having is that it it lumps the results by year. So it will say 25 Results found, list the results for a single year then repeat this for the next year by saying 53 results found then display the results for the next year... exp:

 

2 Results found.

Result 1 1994

Result 2 1994

 

4 Results found.

Result 1 1995

Result 2 1995

Result 3 1995

Result 4 1995

 

and so on and on... I would like it to do this:

 

6 Results found.

Result 1 1994

Result 2 1994

Result 3 1995

Result 4 1995

Result 5 1995

Result 6 1995

 

Can anyone help a brother out?

 

Thanks.

Link to comment
Share on other sites

The first thing you should do is use the while loop to call upon the num_rows of every table and then add them up. This will get all the results.

 

After that, you echo out $total, which should now have the total number of matched queries. Now you loop through to print the actual results.

Link to comment
Share on other sites

<?php
if ($_POST['searchdecs'] && $_POST['decs'] != ''){
require('get_connected.php');
$year_arr = array("1994","1995","1996");
$arr_size = count($year_arr);

if ($_POST['decs']){
	$decs = mysql_real_escape_string($_POST['decs']);
	$flag = 0;
	$total = 0;
	for ($i=0; $i<$arr_size; $i++){
		$sql = mysql_query("SELECT * FROM " .$year_arr[$i]. "data WHERE DECSFamilyName = '$decs' ORDER BY level DESC") or die(mysql_error());
		$total .= mysql_num_rows($sql);
	}

	if (!$total) echo "No queries were found.";
	else {
		for ($i=0; $i<$arr_size; $i++){
			$sql = mysql_query("SELECT * FROM ".$year_arr[$i]."data WHERE DECSFamilyName='$decs' ORDER BY level DESC") or die(mysql_error());
			while ($row = mysql_fetch_assoc($sql)){
				// echo results
			}
		}
	}
}
}
?>

Link to comment
Share on other sites

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.