Jump to content

Recommended Posts

Hi everyone, many thanks in advance for any help you can offer here!

 

I sell watch straps on my website, and I would like to have the image associated with each strap automatically change to an image saying "Sold out!" when stock reaches zero on a specific strap.  Images are currently called from mysql database, and when stock reaches zero the image disappears... but I'd prefer for it to change to the Sold Out image instead... can it be done?

 

For example, I have 10 of a strap named Quark, with a description of the strap above a picture of the strap, and then an "add to cart" button below the picture.

 

When stock on Quark gets to zero currently the picture disappears, but the "add to cart" button and the description stay on the site.

 

Can I make it so when stock hits zero in my mysql database the image switches to a sold out image?

 

I hope that between those two explanations of the same problem I made some sense... I'm a total rookie at PHP...  here's the current code for one of my straps... what code can I add to this that would switch the img src to a new image when stock goes to zero?  Can this even be done?

 

<?
mysql_connect("localhost", "xxx_user", "Fjjkindkejhrk") or die(mysql_error());
mysql_select_db("xxx_products") or die(mysql_error());
$result = mysql_query("SELECT * FROM straps
 WHERE id='1'") or die(mysql_error());  
$row = mysql_fetch_array( $result );
if ($row['stock']!=0){
echo "<p class=\"straps\">";
echo $row['pname'];
echo "<br>";
echo "<img src=\"";
echo $row['picurl'];
echo "\"></a>";}
?>
 

 

Link to comment
https://forums.phpfreaks.com/topic/278574-change-an-image-when-stock-reaches-zero/
Share on other sites

Just set the src according to that condition. Also, use


tags when you post your code.

 

$picurl = $row['stock']==0?'/images/soldout.png':$row['picurl'];

echo '<img src="'.$picurl.'">';
That will set picurl to '/images/soldout.png' if the stock is zero, otherwise it will set it to whatever comes from the DB. Then you use $picurl as the source for the image.

Just set the src according to that condition. Also, use


tags when you post your code.

 

$picurl = $row['stock']==0?'/images/soldout.png':$row['picurl'];

echo '<img src="'.$picurl.'">';
That will set picurl to '/images/soldout.png' if the stock is zero, otherwise it will set it to whatever comes from the DB. Then you use $picurl as the source for the image.

 

 

Thank you so much for the quick response!

 

Where do I put your code in my current code?  I have no idea where it belongs...  also can I have the "soldout.png" sourced locally, from my server?  would the code then look like this?

 

$picurl = $row['stock']==0?'http://www.mygreatstraps.com/images/soldout.png':$row['picurl'];

 

Or am I off in the murk here?

 

Current code:

 

 

<?
mysql_connect("localhost", "xxx_user", "Fjjkindkejhrk") or die(mysql_error());
mysql_select_db("xxx_products") or die(mysql_error());
$result = mysql_query("SELECT * FROM straps
 WHERE id='1'") or die(mysql_error());  
$row = mysql_fetch_array( $result );
if ($row['stock']!=0){
echo "<p class=\"straps\">";
echo $row['pname'];
echo "<br>";
echo "<img src=\"";
echo $row['picurl'];
echo "\"></a>";}
?>
Edited by MicahD
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.