Jump to content

[SOLVED] Multiple WHILE loops help


d3web

Recommended Posts

Hi there.  I'm writing an application that is pulling and displaying results for real estate listings and associated photographs.  The output is using two MySQL querys and WHILE statements.  The outer WHILE loop pulls listing information for all listings within a given category.  This loop is working just fine.

 

The problem lies within the second, inner WHILE loop.  The second loop pulls photograph information for each of the listings provided in the first loop.  What's happening is; after each subsequent iteration of the first loop, the second loop is displaying all results of all previous second loops plus the current run.  (Additionally, the first result of only the first run is displaying twice) Hopefully I can demonstrate better below.

 

Listing 1 information

(assuming Listing 1 has 2 photos)

Listing 1 photo 1, Listing 1 photo 1, Listing 1 photo 2 //for some reason the first photo of only the first listing is displayed twice

 

Listing 2 information

(assuming Listing 2 has 2 photos)

Listing 1 photo 1, Listing 1 photo 2, Listing 2 photo 1, Listing 2 photo 2

 

Listing 3 information

(assuming Listing 3 has 1 photo)

Listing 1 photo 1, Listing 1 photo 2, Listing 2 photo 1, Listing 2 photo 2, Listing 3 photo 1

 

Here is the code I'm using:

 

  $catlist = @mysql_query("SELECT * FROM category GROUP BY CatName ORDER BY CatName");	
	if (!$catlist) {
	echo("<p>Error getting results: " . mysql_error . "</p>");
	  }

	// display category listing
  echo("<p align=\"center\">| \n");
while($pagerow = mysql_fetch_array($catlist)) {
   $CatName=$pagerow["CatName"];
   $CatID=$pagerow["CatID"];
   
   	   	$count = @mysql_query("SELECT * FROM CategoryLookup WHERE CatID='$CatID'");
		if(!$count) {
			echo ("<p>Could not count rows: " . mysql_error . "</p>");
			}
	$number = mysql_num_rows($count);

  echo(" <a href='$PHP_SELF?cat=" . $CatID . "'>" . $CatName ." (" . $number . ")</a> |");
	}  
echo("</p>\n");  

if (isset($_GET['cat'])) {
 $cat =  $_GET['cat'];	
$result = @mysql_query("SELECT * FROM CategoryLookup, Listing WHERE CategoryLookup.CatID='$cat' AND Listing.ID=CategoryLookup.ID");
	if (!$result)
	  {
	  echo("<P>Error getting results: " . mysql_error . "</P>"); 
	  }

//Display contacts in paragraphs
//with delete and modify link beside each
while ( $row = mysql_fetch_array($result) )
  {
	$ID = $row["ID"];
	$MLSNum = $row["MLSNum"];
    $Price = $row["Price"];
    $Status = $row["Status"];
   //Removed for brevity
    $Direction = $row['Direction'];

//Build output and remove empty lines	
	$output = '<p>';
	if($MLSNum != '') {
	$output .= '<strong style="font-size:120%;">' . $MLSNum . '</strong><br />';
	}
	if($Price != '') {
	$output .= '$' . $Price . '<br />';
	}
	if($Status != '') {
	$output .= $Status . '<br />';
	}
	//Again, removed for brevity
	$output .= "<p>Directions:<br />" . $Direction . "</p>\n";



	//Start output display
	echo("<table width='800' bgcolor='#DEDEDE'><tr><td>\n");
	echo("<table><tr><td valign=\"top\">\n");
	echo $output;
	echo("</td></tr>\n");

	//Start photograph display
	echo("<tr><td colspan=\"2\">\n");

                //###############################
               //#####PROBLEM AREA STARTS HERE#######
               //###############################
	$photoresult = @mysql_query("SELECT Photo, Main FROM photos WHERE photos.MLSNum='$MLSNum' AND photos.ID='$ID'");
	if (!$photoresult)
	  {
	  echo("<P>Error getting results: " . mysql_error . "</P>"); 
	  }
	while ( $photorow = mysql_fetch_array($photoresult) )
  {
	$Photo = $photorow["Photo"];
	$Main = $photorow["Main"];
	$Timg='../pics/' . $Photo . '';

	$photooutput .= " <a href=\"" . $Timg . "\"><img src=\"RESIZE_IMAGE.PHP?image=" . $Timg . "\" border=\"0\" ";

	if($Main=='Yes') {
	$photooutput .= "style=\"border:2px solid #f00;\" ";
	}
	$photooutput .= " /></a>\n";

	echo $photooutput;
  }

           //#########################
          //########end phoblem area######
         //##########################

	echo("</td></tr></table>\n");

	//Finish output display
	echo("</td>" .
		 "<td align='center'><p><a href='$current_url?delcontact=$ID'>Delete this listing?</a></p>\n" . 
		 "<p><a href='$current_url?modcontact=$ID'>Modify this listing?</a></p>\n" . 
		 "<p><a href='photos.php?ID=$ID&MLSNum=$MLSNum'>Add / Edit Photos</a></p>\n" .
		 "</td></tr></table><br>\n");

	echo $ID . " | " . $MLSNum;

  }
  
 } else {
   echo("<p>Select a category above</p>\n");  
}

 

 

I must be missing something small but my lone set of eyeballs are going crazy.

 

Please help,

Dan

Link to comment
https://forums.phpfreaks.com/topic/180890-solved-multiple-while-loops-help/
Share on other sites

Ok, I don't know why but the photos are looping in some sort of incremental way now...

 

Listing 1 information

(assuming Listing 1 has 2 photos)

Listing 1 photo 1, Listing 1 photo 1, Listing 1 photo 2 //for some reason the first photo of only the first listing is displayed twice

 

Listing 2 information

(assuming Listing 2 has 2 photos)

Listing 1 photo 1, Listing 1 photo 2, Listing 2 photo 1, Listing 1 photo 1, Listing 1 photo 2, Listing 2 photo 1

 

Listing 3 information

(assuming Listing 3 has 2 photos)

Listing 1 photo 1, Listing 1 photo 2, Listing 2 photo 1, Listing 2 photo 2, Listing 3 photo 1, Listing 1 photo 1, Listing 1 photo 2, Listing 2 photo 1, Listing 2 photo 2, Listing 3 photo 1, Listing 3 photo 2

 

........................................................

 

I need to find a way to make the second WHILE loop end after each loop and clear the way for the next.

I'm looking at it and thinking maybe you could do a join instead of calling 3 whiles loops which just become alot of information to handle. Will get back to you asap..

 

Also, I am just going to assume you made your DB connections at the beginning. If not thats a definite problem..

 

I would highly suggest not using $_GET. Its an unsafe method. You should know whether the user is going to put it through using url, post, or cookie..

Alright so I think I got it. Who knows right? haha.

Few pointers,

Can be your best friend, Showing ALL errors. Call at the beginning

error_reporting(E_ALL);

This then helps with security to hide those errors

error_reporting(0);

So when your code said,

$Timg='../pics/' . $Photo . '';

At the end you .' then add another '

Instead do this, It was save you the hassle when you forget to put a . or '

$Timg='../pics/' . $Photo;

As I said before using Get is not the best idea because it makes it easy for people to get into your website. Before you know it somebody that has no life is deleting your Database. You can clean inputs with html_entity_decode or addslashes. I am not sure what the most prefered method is but I am sure you can ask.

Alright again like I said, I am assuming you already used mysql_connect and mysql_select_db.

 

As for your code the biggest problem I saw was the line that said this

$photooutput .= " <a href=\"" . $Timg . "\"><img src=\"RESIZE_IMAGE.PHP?image=" . $Timg . "\" border=\"0\" ";

the problem is $photooutput was never called to begin with so .= doesn't work. You need to just use =

 

This final thing I got was this

 

<?php	
$catlist=@mysql_query("SELECT * FROM category GROUP BY CatName ORDER BY CatName");
if(!$catlist){
	die("<p>Error getting results: " . mysql_error . "</p>");
}
 echo "<p align=\"center\">| \n";
 while($pagerow = mysql_fetch_array($catlist)){
	 $CatName=pagerow['CatName'];
	 $CatID=$pagerow['CatID'];
	 $count=@mysql_query("SELECT * FROM CategoryLookup WHERE CatID='$CatID'");
	 if($count){
		 die("<p>Could not count rows: " . mysql_error . "</p>");
	 }
	 $number=mysql_num_rows($count);
	 echo "<a href='$PHP_SELF?cat=" . $CatID . "'>" . $CatName ." (" . $number . ")</a> |";
 }
 echo "</p>\n";
 if(isset($_GET['cat'])){
	 $cat=$_GET['cat'];
	 $result=@mysql_query("SELECT * FROM CategoryLookup, Listing WHERE CategoryLookup.CatID='$cat' AND Listing.ID=CategoryLookup.ID");
	 if(!$result){
		 die("<P>Error getting results: " . mysql_error . "</P>");
	 }
	 $output='';
	 while($row=mysql_fetch_array($result)){
		 $ID=$row['ID'];
		 $MLSNum=$row['MLSNum'];
		 $Price=$row['Price'];
		 $Status=$row['Status'];
		//Removed for brevity
		 $Direction=$row['Direction'];
		 $output.='<p>';
		if($MLSNum !== NULL) {
			$output .= '<strong style="font-size:120%;">' . $MLSNum . '</strong><br />';
		}
		if($Price !== NULL) {
			$output .= '$' . $Price . '<br />';
		}
		if($Status !== NULL) {
			$output .= $Status . '<br />';
		}
		//Again, removed for brevity
		$output .= "<p>Directions:<br />" . $Direction . "</p>\n";

	 	echo("<table width='800' bgcolor='#DEDEDE'><tr><td>\n");
	 	echo("<table><tr><td valign=\"top\">\n");
	 	echo $output;
	 	echo("</td></tr>\n");
	 	echo("<tr><td colspan=\"2\">\n");
		//problem area
		$photoresult=@mysql_query("SELECT Photo, Main FROM photos WHERE MLSNum='$MLSNum' AND ID='$ID'");
		if(!$photoresult){
			die("<P>Error getting results: " . mysql_error . "</P>");
		}
		while($photorow=mysql_fetch_array($photresult)){
			$Photo = $photorow["Photo"];
			$Main = $photorow["Main"];
			$Timg='../pics/'.$Photo;
			$photooutput = ' <a href="'.$Timg.'"><img src=\"RESIZE_IMAGE.PHP?image='.$Timg.'" border="0"';
			if($Main=='Yes'){
				$photooutput.='style="border:2px solid #f00;';
			}
			$photooutput .= " /></a>\n";
			echo $photooutput;
		}
		//end problem??		
		echo("</td></tr></table>\n");
		echo("</td>" .
			"<td align='center'><p><a href='$current_url?delcontact=$ID'>Delete this listing?</a></p>\n" . 
			"<p><a href='$current_url?modcontact=$ID'>Modify this listing?</a></p>\n" . 
			"<p><a href='photos.php?ID=$ID&MLSNum=$MLSNum'>Add / Edit Photos</a></p>\n" .
			"</td></tr></table><br>\n");
		echo $ID . " | " . $MLSNum;
	 }
 }else{
  	echo("<p>Select a category above</p>\n");  
 }

 

Let me know if it fixes the problem.

iversonm,

 

Thank you.  You found the glitch.  It was that period after I first declared $photooutput.  I knew it had to be something simple like that.

 

Thanks for the other pointers too.  I'll start playing with them when my work load lightens up enough.

 

Thanks again!

Dan

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.