Errors displaying variables correctly!

I have a script which I’m using along with a js script to update a list of results based on user selections from dropdown lists. Managed to get this part working but I have an issue with some of the variables not showing when I return the display.

The main problems are the product images which have their names stored in the db like so image1.jpg,image1A.jpg,image1B.jpg

Here’s the script I was using before I started making everything work via ajax, and it works brilliantly.

<?php $images = explode(',', $row_product[Picture_Refs]); echo '<img src="http://www.mysite.co.uk/admin/Stock'.$img_path .''.$images[0].'"  width="150">'; ?>

The $img_path variable is set previously in the script based on the manufacturer of the product.

Problem is that now i am using the following script, I don’t know how to code the image part correctly.

<?php 
$query = "SELECT * FROM Stock WHERE Type = '6'";
if($make != '')
	$query .= " AND Make = '$make'";
	//Execute query
$qry_result = mysql_query($query) or die(mysql_error());

	//Build Result String
	// Insert a new div for each person returned
while($row = mysql_fetch_array($qry_result)){
	$display_string = "<div class='individual-cars'>";
	$display_string .= "<h1>$row[Year] $row[Make] $row[Model] $row[Engine_Size]cc <span class='price'>&pound;$row[Price]</span></h1>";
	$display_string .= "".$images = explode(',', $row[Picture_Refs])."'<img src='http://www.mysite.co.uk/admin/Stock/$used_img_path/$images[0]' alt='$row[Make] $row[Model].' for sale' width='150'>";
	$display_string .= "<p><span>$row[Mileage]mls, $row[Colour]</span> $row[Options]</p>";
	$display_string .= "</div>";
}
echo "Query: " . $query . "<br />";
//$display_string .= "</table>";
echo $display_string;
?>

Can anyone point out where I’m going wrong with this please? Any help much appreciated, thanks in advance.

I don’t understand exactly what you mean, but I saw a problem in your code.
In the while part, you always start with writing “$display_string = …”
This means you empty your $display_string every time you run the code between “while {}”. The result of this code is that you only display the last row in your query (last element in your array).

This would be better:

$display_string = "";
while($row = mysql_fetch_array($qry_result)){ 
    $display_string .= "<div class='individual-cars'>"; 
    $display_string .= "<h1>$row[Year] $row[Make] $row[Model] $row[Engine_Size]cc <span class='price'>&pound;$row[Price]</span></h1>"; 
    $display_string .= "".$images = explode(',', $row[Picture_Refs])."'<img src='http://www.mysite.co.uk/admin/Stock/$used_img_path/$images[0]' alt='$row[Make] $row[Model].' for sale' width='150'>"; 
    $display_string .= "<p><span>$row[Mileage]mls, $row[Colour]</span> $row[Options]</p>"; 
    $display_string .= "</div>"; 
}

This will display all results of your query (all elements in the array).

thanks for spotting that error it has improved things a bit!! What I am trying to do is merely echo the image in my code. The images are all stored in one column in my db and are stored as image1.jpg,image1A.jpg,image1B.jpg in a single variable. What I would usually do is run the explode script above and then just echo the $image[0] to show the first image from the list. I can’t work out how to get this to display at all in the code I have above!

Any ideas?

take a look at your html output. What does it say after “http://www.mysite.co.uk/admin/Stock/”?

It shows the Picture_Refs variable fully. ie:
22697416_MB_460.jpg,22697416_IB_718.jpg,22697416_IB_420.jpg,22697416_IB_544.jpg,22697416_IB_897.jpg,22697416_IB_932.jpg,22697416_IB_120.jpg,22697416_IB_313.jpg

I need to get it just to display thie first image only.

Ah wait sorry was looking at the code I’m messing around with, From the code I posted above it shows nothing after the Stock/ butdoes output Array’ before the <img> bit.

Does that make sense?

Try something like this:

$display_string = "";
while($row = mysql_fetch_array($qry_result)){ 
    $display_string .= "<div class='individual-cars'>"; 
    $display_string .= "<h1>$row[Year] $row[Make] $row[Model] $row[Engine_Size]cc <span class='price'>&pound;$row[Price]</span></h1>"; 
    $images = explode(',', $row[Picture_Refs]);
    $src = "http://www.mysite.co.uk/admin/Stock/".$used_img_path."/".$images[0];
    $display_string .= "<img src='".$src."' alt='$row[Make] $row[Model].' for sale' width='150'>"; 
    $display_string .= "<p><span>$row[Mileage]mls, $row[Colour]</span> $row[Options]</p>"; 
    $display_string .= "</div>"; 
}

Genius!! that works a treat! thanks for all your help. Any chance you can tell me why the following isn’t working though? I tried to use what you had as a template. Basically I need to only show the first 150 characters of the Options variable so I have a function set up that I would usually use like so echo short($row[Options], 150) but thats not working at all.

Here’s the code I tried, could you help me with this please?

$display_string = "";
while($row = mysql_fetch_array($qry_result)){
    $display_string .= "<div class='individual-cars'>";
    $display_string .= "<h1>$row[Year] $row[Make] $row[Model] $row[Engine_Size]cc <span class='price'>&pound;$row[Price]</span></h1>";
    $images = explode(',', $row[Picture_Refs]);
    $src = "http://www.easternwestern.co.uk/admin/Stock/".$used_img_path."/".$images[0];
    $display_string .= "<img src='".$src."' alt='$row[Make] $row[Model].' for sale' width='150'>";
	$minText = short($row[Options], 150);
    $display_string .= "<p><span>$row[Mileage]mls, $row[Colour]</span> ".$minText."</p>";
    $display_string .= "</div>";
}

Its cool I managed to work it out. Thanks again for all your help with this.

okay
glad you found it!

Please help I can’t get this to work at all and I’m pulling my hair out!!

Why won’t the code below work?? Its the part with the locations that I’m having problems with it just doesn’t return anything!!

$display_string = "";
	// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
	$display_string .= "<div class='individual-cars'>";
	$display_string .= "<h1>$row[Year] $row[Make] $row[Model] $row[Engine_Size]cc <span class='price'>&pound;$row[Price]</span></h1>";
	$images = explode(',', $row[Picture_Refs]);
    $src = "http://www.easternwestern.co.uk/admin/Stock/HDEdinburgh_media/".$images[0];
    $display_string .= "<img src='".$src."' alt='$row[Make] $row[Model]' for sale' width='150'>";
	$minText = substr($row[Options],0,150); 
	$display_string .= "<p><span>$row[Mileage]mls, $row[Colour]</span> ".$minText." ...</p>";
	$display_string .= "<div class='car-location-listing'>";
	
	$querylocations = "SELECT * FROM Locations WHERE Id_Number='".$row[Feed_Id]."'";
	$placeresult = mysql_query($querylocations) or die(mysql_error());
	while($row_place = mysql_fetch_array($placeresult));
	
	$display_string .= "<strong>$row_place[Title]</strong>&nbsp;&nbsp;&nbsp;&nbsp;Tel. <strong> $row_place[Sales_Tel]</strong>";
	$display_string .= "<a href='bike-details.php?ref=$row[Vehicle_Id]'><div class='button'>Get more info</div></a>";
	$display_string .= "</div>";
	$display_string .= "</div>";
}

Are you sure you have a result in MySQL?
What’s the html output?


    while($row_place = mysql_fetch_array($placeresult)){
     
    $display_string .= "<strong>". $row_place['Title'] .
                            "</strong>Tel. <strong>". $row_place['Sales_Tel'] ."</strong>"; 
    $display_string .= "<a href='bike-details.php?ref=". $row['Vehicle_Id']. "'><div class='button'>Get more info</div></a>"; 
    $display_string .= "</div>"; 
    $display_string .= "</div>";
}

What I did:

Define the while loop more clearly with {} brackets
Drop in and out of the echo statement and expand non-scalar (array) variables before concatenating them
Quote array keys correctly to avoid error notices

Edit:


echo $querylocations;

If you echoed temporarily your sql statement you would be able to take it, paste it into your database and make sure that a) it was valid and b) that you had some matching data

works great, thanks for your help with this.