saco721 Posted October 19, 2007 Share Posted October 19, 2007 Hi!, I am trying to use a loop but cannot get it to work. It seems quite straight forward. The problem is that $test2 never decrements and hence the downloads are always available. How do I decement $test2 and get the "No downloads Left" message to appear, therefore preventing any more downloads. Here is the code: <?php for ($i=1; $i <= $num_cart_items; $i++) { $test1 = "". $keyarray["item_name".$i] . ".pdf"; $test2 = "". $keyarray["quantity".$i] . ""; if ($test2 > 0){ echo ("<td><font face = \"Verdana\" size = \"2\"><a href=\"testfile.php?arg1=$test1\">click to download.</a></font></td>"); $test2 --; } else{ echo ("<td>No downloads left</td>"); } } ?> any help would be greatly appreciated! - thank you Quote Link to comment https://forums.phpfreaks.com/topic/73953-problems-with-decementing-within-loop/ Share on other sites More sharing options...
pocobueno1388 Posted October 19, 2007 Share Posted October 19, 2007 Try this <?php for ($i=1; $i <= $num_cart_items; $i++) { $test1 = "". $keyarray["item_name".$i] . ".pdf"; if ($i == 1){ $test2 = "". $keyarray["quantity".$i] . ""; } if ($test2 > 0) { echo("<td><font face = \"Verdana\" size = \"2\"><a href=\"testfile.php?arg1=$test1\">click to download.[/url]</font></td>"); } else { echo("<td>No downloads left</td>"); } $test2 --; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73953-problems-with-decementing-within-loop/#findComment-373186 Share on other sites More sharing options...
saco721 Posted October 19, 2007 Author Share Posted October 19, 2007 Thanks for the response! I have tried using the code you provided, but still have the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/73953-problems-with-decementing-within-loop/#findComment-373213 Share on other sites More sharing options...
kenrbnsn Posted October 19, 2007 Share Posted October 19, 2007 Do you store the newly decremented value anywhere? If not, there's your problem. Ken Quote Link to comment https://forums.phpfreaks.com/topic/73953-problems-with-decementing-within-loop/#findComment-373217 Share on other sites More sharing options...
Orio Posted October 19, 2007 Share Posted October 19, 2007 I think you are using this "test2" variable for no reason. Try this: <?php for ($i=1; $i <= $num_cart_items; $i++) { $test1 = "". $keyarray["item_name".$i] . ".pdf"; if ($keyarray["quantity".$i] > 0){ echo ("<td><font face = \"Verdana\" size = \"2\"><a href=\"testfile.php?arg1=$test1\">click to download.[/url]</font></td>"); $keyarray["quantity".$i] --; } else{ echo ("<td>No downloads left</td>"); } } ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/73953-problems-with-decementing-within-loop/#findComment-373218 Share on other sites More sharing options...
saco721 Posted October 19, 2007 Author Share Posted October 19, 2007 Hi!, I have changed $test2 --; to $test2--; have also put $test2 in echo below to see if it is decrementing - still no success! echo ("<td><font face = \"Verdana\" size = \"2\"><a href=\"testfile.php?arg1=$test1\">click to download.$test2[/url]</font></td>"); thanks! Quote Link to comment https://forums.phpfreaks.com/topic/73953-problems-with-decementing-within-loop/#findComment-373235 Share on other sites More sharing options...
saco721 Posted October 19, 2007 Author Share Posted October 19, 2007 Hi Orio, I have changed the code and removed $test2 as suggested, but download link still appears. I have checked the code and it is the same as yours. thanks for your time, Saco Quote Link to comment https://forums.phpfreaks.com/topic/73953-problems-with-decementing-within-loop/#findComment-373279 Share on other sites More sharing options...
saco721 Posted October 19, 2007 Author Share Posted October 19, 2007 Hi Ken, I was wondering how I store the newly decremented value? could you give me an example please. thanks, Saco Quote Link to comment https://forums.phpfreaks.com/topic/73953-problems-with-decementing-within-loop/#findComment-373304 Share on other sites More sharing options...
saco721 Posted October 23, 2007 Author Share Posted October 23, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/73953-problems-with-decementing-within-loop/#findComment-376138 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.