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:
Code: $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>
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
Login script (probably a simple error)
dbConfig.phpCode: <?// Replace the variable values below// with your specific database information.$host = "localhost";$user = "username";$pass = "password";$db
Web Host List
Here is a list of web hosts that offer PHP and MySQL. It is no where near a full list, but it is most popular. I'm locking this so you can't reply. I am pulling this list from the most popular google
Replacing a string on click of a button
Hi! Is there a way to replace a string on a click of a text link?This is what I've come up with. It doesn't work at all ^^;;Code: <head><?php $string = Red;function changeBlue()
Parse String
I need to parse the string below into three variables. How would I do that?john + 10/20/2008 - 10/30/2008+ and - are separators
How to use php and sql to check if values match the ones in a table (for logins)
How would I code it that the script takes two variables that are passed to it (UserID and PIN), and checks the table to see if the PIN matches the one in the table. If it does it should assign the
simplexml and xpath - Need some help
Hello,I am trying to parse an xml which comes back from Amazon and I am trying to know how many Browsenodes are shown within the XML.I am using the classs Tarzan to get the xml from Amazon and the
Oracle Connectivity
Hi Every One,
$_GET problem
Hi all,Have a bit of a problem with a little piece of code, I am not sure if this should be under php or ajax as I am not sure where the problem lies. Basically here is the script:Code:
Variable passed to each() is not an array or object
Hi,This is a email a friend type of form, and it isn't working anymore.<? function validate_email($email, $location) { $valid_address = true; $mail_pat = '^(.+)@(.+)$'; $valid_chars =
Get dump of MySQL DB from Live Site
I'm after an easy, but secure way of generating and importing a MySQL dump from a remote server. The way I want to achieve this is as follows: 1) I will have a page on my localhost server with a form