Jump to content

[SOLVED] Problem with IF and file_exists


Recon

Recommended Posts

Here is my code, I'm reading from a MySQL db.

[code]$username="*******";
$password="*******";
$database="***********";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database.");

//get products
$queryproducts="SELECT * FROM products WHERE status='available' ORDER BY price";
$resultproducts=mysql_query($queryproducts);

$numproducts=mysql_numrows($resultproducts);

mysql_close();

$iproduct=0;
while ($iproduct < $numproducts) {

$id=mysql_result($resultproducts,$iproduct,"id");
$name=mysql_result($resultproducts,$iproduct,"name");
$price=mysql_result($resultproducts,$iproduct,"price");

$img2 = 'img/products/$id/2.jpg';
$img3 = 'img/products/$id/3.jpg';
$img4 = 'img/products/$id/4.jpg';

echo "<img src='phpThumb.php?src=img/products/$id/1.jpg&w=150px&h=150px&zc=1' alt='$name' width='150' height='150' />


  <h3>$name</h3>
<p>Price: &pound;$price</p>

<h4>BUY NOW</h4>";

if (file_exists($img2)) {
  echo "<img src='phpThumb.php?src=img/products/$id/2.jpg&w=100px&h=40px&zc=1' width='100' height='40' alt='Image'>";
} else {
  echo "sorry no2";
}

if (file_exists($img3)) {
  echo "<img src='phpThumb.php?src=img/products/$id/3.jpg&w=100px&h=40px&zc=1' width='100' height='40' alt='Image'>";
} else {
  echo "sorry no3";
 
} if (file_exists($img4)) {
  echo "<img src='phpThumb.php?src=img/products/$id/4.jpg&w=100px&h=40px&zc=1' width='100' height='40' alt='Image'>";
} else {
  echo "sorry no4";
}


$iproduct++;
}


?>[/code]

It always shows the "sorry" text even though I definitely have the files there. If change the:

[code]$img2 = 'img/products/$id/2.jpg';
$img3 = 'img/products/$id/3.jpg';
$img4 = 'img/products/$id/4.jpg';[/code]

to

[code]$img2 = 'img/products/1/2.jpg';
$img3 = 'img/products/1/3.jpg';
$img4 = 'img/products/1/4.jpg';[/code]

it works so it must be a problem with 'inserting' (sorry I'm a newbie) the $id there.



Edit: I tried replacing $id with " . $id . " (advice from a friend) but it still doesn't work.
Link to comment
https://forums.phpfreaks.com/topic/31281-solved-problem-with-if-and-file_exists/
Share on other sites

[quote author=artacus link=topic=119297.msg488534#msg488534 date=1166568983]
*puts $5 down that Recon will ask you to spell out what you just said*
[/quote]

lol, I would have done but a friend helped me out. I think I ended up doing what you said anyway...

[code] $img1 = "img/products/".$id."/1.jpg";
$img2 = "img/products/".$id."/2.jpg";
$img3 = "img/products/".$id."/3.jpg";
$img4 = "img/products/".$id."/4.jpg";[/code]

worked in the end. :)

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.