Link to home
Start Free TrialLog in
Avatar of pingeyeg

asked on

Same image being displayed twice instead of showing certain ones

I have this code below that displays listings from the db, but for some reason it only shows the first entry in th db twice instead of showing it once and then going down the line to look for more that have a value of Yes to them.
$conn = mysql_connect("localhost", "username", "password")
or die(mysql_error());
$getInfo = "SELECT * FROM featured_listings";
$action1 = mysql_query($getInfo) or die(mysql_error());
if($row = mysql_fetch_array($action1)) {
		$order = "SELECT ID, SUBSTRING_INDEX(strRemarks, ' ', 15) AS shorten, strPrice, strImage, strFeatureListing, strListdate FROM featured_listings WHERE strFeatureListing = 'Yes' ORDER BY strListDate DESC";
		$action2 = mysql_query($order) or die(mysql_error());
		if($row = mysql_fetch_array($action2)) {
			$id = $row['ID'];
			$remarks = $row['shorten'];
			$price = $row['strPrice'];
			$image = $row['strImage'];
			$featurelisting = $row['strFeatureListing'];
			$listdate = $row['strListDate'];
			if($featurelisting == "No") {
				echo "<center><div class='listing_specs'>There are currently no featured listings.</div></center>";
			} elseif($featurelisting == "Yes") {
					for($i = 0; $i < 2; $i++) {
						echo "<table width='175px' cellpadding='5' cellspacing='0'>";
						echo "<tr><td colspan='2' align='center'>";
							if(isset($image) && !empty($image)) {
      							echo "<img src=/listings/$image width=150px>";                        
							} else {
	  							echo "<img src=/admin/images/noimage_l.gif width=150px>";                        
								echo "</td></tr>";
								echo "<tr><td colspan='2'><div class='listing_specs'>$remarks...</div></td></tr>";
								echo "<tr><td><div class='listing_specs'>$" . $price . "</div></td>";
								echo "<td align='right'><div class='listing_specs'><a href='/listing.php?ID=$id'>More Info...</div></td></tr></table>";

Open in new window

Avatar of hielo
Flag of Wallis and Futuna image

Try changing this:
if($row = mysql_fetch_array($action1)) {

to this:
while($row = mysql_fetch_array($action1)) {
Avatar of pingeyeg


Ok I changed the second if statement to a while and now I am getting the correct listings from the db, but they are coming in duplicates.
The problem may be "tied" to the $row variable since you use the same variable for two different queries. First you have:
if($row = mysql_fetch_array($action1))

Then you have:
if($row = mysql_fetch_array($action2))

You need to differentiate between the two. Try changing the first one to:
if($row1 = mysql_fetch_array($action1))

and see what happens.
Nope, that didn't do the trick.  Although that did seem logical.
Avatar of hielo
Flag of Wallis and Futuna image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW: since your second if is now a while, I believe you can get rid of the for-loop. Most likely it was intended to iterated over the records.
In case it's still not obvious, that for loop is really just duplicating the <table> where the image appears. The $i variable is not used to "create/complete" any of the links, since these are generated directly from the db records. If you do not want the duplicate images, get rid of the for loop.
if($row = mysql_fetch_array($action1))

it should be

if($row == mysql_fetch_array($action1))

the if statement always should have an == and not = because = does assign the result to the variable which means it will alway give (True).
@ehabafia:You comments are logical, but in this case they do not apply. Prior to the if, $row is not assigned anything. IF there is a record mysql_fetch_array() will return the resource and assign it to $row, which is needed to continue processing further down. Otherwise mysql_fetch_array() will return FALSE, and the if clause will not get executed. To clarify matters, without "$row =", the statement reads:
if( mysql_fetch_array($action1) )

which is perfectly valid, but the problem is that the internal pointer to the record was advanced and you did not "catch" the results of the previous record.

In most of my scripts I use classes for database that will handle most of the queries and fetching for me.

anyway thanks hielo.