Jump to content

Problems with decementing within loop


saco721

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/73953-problems-with-decementing-within-loop/
Share on other sites

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 --;
}

?>

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.

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!

 

 

 

 

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.