MicahD Posted May 29, 2013 Share Posted May 29, 2013 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>";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/278574-change-an-image-when-stock-reaches-zero/ Share on other sites More sharing options...
kicken Posted May 29, 2013 Share Posted May 29, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278574-change-an-image-when-stock-reaches-zero/#findComment-1433083 Share on other sites More sharing options...
MicahD Posted May 29, 2013 Author Share Posted May 29, 2013 (edited) 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 May 29, 2013 by MicahD Quote Link to comment https://forums.phpfreaks.com/topic/278574-change-an-image-when-stock-reaches-zero/#findComment-1433091 Share on other sites More sharing options...
MicahD Posted May 29, 2013 Author Share Posted May 29, 2013 Never mind, I got it to work! Thank you SO much for your help, been banging my head against the wall for a few hours with this, your code solved my problem completely. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/278574-change-an-image-when-stock-reaches-zero/#findComment-1433092 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.