PVK Posted July 1, 2004 Posted July 1, 2004 <?php $prod_quantity = tep_get_products_stock($products_id); switch ($prod_quantity) { case 0: print '<img src="images/stock_soldout.gif" border="0">'; break; case 1: print '<img src="images/stock_limitedstock.gif" border="0">'; break; case 2: print '<img src="images/stock_limitedstock.gif" border="0">'; break; case 3: print '<img src="images/stock_limitedstock.gif" border="0">'; break; case 4: print '<img src="images/stock_instock.gif" border="0">'; break; case 120: print '<img src="images/stock_instock.gif" border="0">'; break; } ?> This is a piece of code from an existing and already running contribution, but i would like to tweak it a bit, but don't know how. Hopefully someone can rewrite it a bit, because basically what i have to do now would be to make an entry for every step of the quantity available. Which is not so bad if you only have 1 to 10 products, but if you have large amounts on stock [100dreds], this would create a HUGE column in the products info page . I don't know if this would make the site or page slow ???? But what i am thinking of is to make like 3 states from the above piece of code, which would have to be something like this : case below 1 : print '<img src="images/stock_soldout.gif" border="0">'; break; case below 10 [but above 0] : print '<img src="images/stock_limitedstock.gif" border="0">'; break; case above 10 : print '<img src="images/stock_onstock.gif" border="0">'; break; I know the code i wrote above is aboslutely nonsense, but that is because i don't know jack about codes :) ANyone knows the solution?
♥yesudo Posted July 1, 2004 Posted July 1, 2004 Try: <?php if ($prod_quantity < 1){?> print '<img src="images/stock_soldout.gif" border="0">'; <?php} elseif (($prod_quantity > 0) && ($prod_quantity < 10){?> print '<img src="images/stock_limitedstock.gif" border="0">'; <?php} else{?> print '<img src="images/stock_onstock.gif" border="0">'; <?php}?> Your online success is Paramount.
♥yesudo Posted July 2, 2004 Posted July 2, 2004 Try: <?php if ($prod_quantity < 1){?> print '<img src="images/stock_soldout.gif" border="0">'; <?php} elseif (($prod_quantity > 0) && ($prod_quantity < 10)){?> print '<img src="images/stock_limitedstock.gif" border="0">'; <?php} else{?> print '<img src="images/stock_onstock.gif" border="0">'; <?php}?> actually correction above. Your online success is Paramount.
PVK Posted July 2, 2004 Author Posted July 2, 2004 Thanks yesudo . I have tried your code , but when i just copy pasted the code it gave me an eror.: Parse error: parse error, unexpected '}' in /home/storename/www/shop/product_info.php on line 237 which is in this part of the code : print '<img src="images/stock_soldout.gif" border="0">'; <?php} I saw that parts of the code in dreamweaver where marked as blue [which means something is wrong] when i put an extra space between the ? and the } the code became red [which in dreamweaver means is a correct code] SO now the code is : <?php if ($prod_quantity < 1){?> print '<img src="images/stock_soldout.gif" border="0">'; <?php } elseif (($prod_quantity > 0) && ($prod_quantity < 10){?> print '<img src="images/stock_limitedstock.gif" border="0">'; <?php } else{?> print '<img src="images/stock_onstock.gif" border="0">'; <?php }?> However, it still didn't work, itstead now it comes up with a : Parse error: parse error, unexpected ';' in /home/storename/www/shop/product_info.php on line 238 This is in this line : elseif (($prod_quantity > 0) && ($prod_quantity < 10){?> Any ideas what could be wrong? Thanks for your time and help
Acheron Posted July 2, 2004 Posted July 2, 2004 <?php ?if ($prod_quantity < 1) { ? ?echo "<img src=\"images/stock_soldout.gif\" border="0">"; ?elseif (($prod_quantity > 0) && ($prod_quantity < 10)) { ? ?echo "<img src=\"images/stock_limitedstock.gif\" border="0">"; ?} else { ? ?echo "<img src=\"images/stock_onstock.gif\" border="0">"; ?} ?> or ... <?php if ($prod_quantity < 1) { echo "<img src=\"images/stock_soldout.gif\" border=\"0\">"; elseif (($prod_quantity > 0) && ($prod_quantity < 10)) { echo "<img src=\"images/stock_limitedstock.gif\" border=\"0\">"; } else { echo "<img src=\"images/stock_onstock.gif\" border=\"0\">"; } ?>
PVK Posted July 2, 2004 Author Posted July 2, 2004 ALmost there i think. i still got errors with Acherons version, but after some tweaking i got no erros and even got to display one of the images. What follows is a code from an absolutel noob when it comes to code, but it is a trial and error effort from my side :D <?php if ($prod_quantity < 1 { echo "<img src=\"images/stock_limitedstock.gif\" border=\"0\">"; } elseif (($prod_quantity > 0) && ($prod_quantity < 10)) { echo "<img src=\"images/stock_limitedstock.gif\" border=\"0\">"; } else { echo "<img src=\"images/stock_instock.gif\" border=\"0\">"; } ?> Now i don't get any errors, displaying an image, but it doesn't matter what number on stock i have, it always displays the last part which is the : INSTOCK.GIF As an addition: when i use this code: <?php if ($prod_quantity < 1) { echo "<img src=\"images/stock_limitedstock.gif\" border=\"0\">"; } elseif (($prod_quantity > 0) && ($prod_quantity < 10)) { echo "<img src=\"images/stock_limitedstock.gif\" border=\"0\">"; } else { echo "<img src=\"images/stock_instock.gif\" border=\"0\">"; } ?> It always displays the LIMITEDSTOCK.GIF [the difference is in the first line : <1] Hope this clears up things even more to make the code work :) Acheron [or someone else] Could you please compare your original code with my tweaked ones and check what i have done and iron out the flaws? Thanks
Acheron Posted July 2, 2004 Posted July 2, 2004 Well if you want it that way ... <?php if ($prod_quantity < 10) { echo "<img src=\"images/stock_limitedstock.gif\" border=\"0\">"; } else { echo "<img src=\"images/stock_instock.gif\" border=\"0\">"; } ?>
Guest Posted July 2, 2004 Posted July 2, 2004 You didn't show your db query :blink: Anyhow, substitute $your_query with your database query: <?php $rows = 0; $products_query = tep_db_query($your_query); while ($products = tep_db_fetch_array($products_query)) { $rows++; $prod_quantity = tep_get_products_stock($products['products_id']); if ($prod_quantity < 1) { echo tep_image(DIR_WS_IMAGES . 'stock_soldout.gif', 'Sold out'); } elseif (($prod_quantity > 0) && ($prod_quantity < 10)) { echo tep_image(DIR_WS_IMAGES . 'stock_limitedstock.gif', 'Limited Stock'); } else { echo tep_image(DIR_WS_IMAGES . 'stock_onstock.gif', 'In Stock')"; } } ?> Matti
burt Posted July 2, 2004 Posted July 2, 2004 What is wrong with: <?php switch (tep_get_products_stock($products_id)) { ? ?case "0": ? ?print '<img src="images/stock_soldout.gif" border="0">'; ? ?break; ? ?case "1": ? ?case "2": ? ?case "3": ? ?case "4": ? ?case "5": ? ?case "6": ? ?case "7": ? ?case "8": ? ?case "9": ? ?print '<img src="images/stock_limitedstock.gif" border="0">'; ? ?break; ? ?default: ? ?print '<img src="images/stock_onstock.gif" border="0">'; ? ?break; } ?> Far more graceful, far more portable. Less overhead.
PVK Posted July 2, 2004 Author Posted July 2, 2004 Thanks everybody. I have tried 'em all but i just keep on getting errors. [exceept the one where i had to place my own query, but since i really have no idea what i had to fill in there, i decided to test the latest idea , reworked it a bit and that one worked fine, so i'll just go for that one The code now is : <?php $prod_quantity = tep_get_products_stock($products_id); switch ($prod_quantity) { case 0: print '<img src="images/stock_soldout.gif" border="0">'; break; case 10: print '<img src="images/stock_limitedstock.gif" border="0">'; break; default: print '<img src="images/stock_instock.gif" border="0">'; break; } ?> Thanks for trying to help a total NOSCRIPTER :rolleyes: I learned some stuff along the way, i just have to figure out what it is exactly :D
burt Posted July 2, 2004 Posted July 2, 2004 With the latest code you posted, a quantity of 10 == limited stock, but a quantity of 5 == in stock. I'm not sure that's the behaviour your require, is it ?
PVK Posted July 2, 2004 Author Posted July 2, 2004 No you are right. I also saw that when i posted it so i changed the 10 into 1 :) That should do the trick am i correct? Thanks for all the help :D
Recommended Posts
Archived
This topic is now archived and is closed to further replies.