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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

 

 

 

 

Link to comment
Share on other sites

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.