Adding same string during LOOP

I have a page in which you select multiple “cars” through checkbox.
Each car has its individual value of “bullets” awarded when they melt the car.
Now while its looping through each car that is checked im looking to add the total of all the bullet values together.

[php]if($_POST[‘melt’]){
$n = count($doit);
$i = 0;
if($n >= 1){
while ($i < $n){
$doit = $doit[$i];
$ifnota = mysql_query(“SELECT * FROM cars WHERE id = $doit”);
if(!$doit){}else{
$ifnot = mysql_fetch_array($ifnota);
$ifnotname = $ifnot[‘owner’];
$meltbullets = $ifnot[‘bullets’];
$meltdamage = $ifnot[‘damage’];
if($ifnotname != $gangsterusername){}
else{
if($meltdamage == ‘0’){ $meltamount = $meltbullets; }else{
$meltdamaged = 100 - $meltdamage;
$meltamounttwo = $meltdamaged * $meltbullets;
$meltamount = ceil($meltamounttwo / 100); }
$totalbullets += $meltamount + $totalbullets;
}}
$showoutcome++; $outcome = “You melted $n cars and earned $totalbullets bullets, Your crew took $theperc% of them!”;
$i++;
}}}[/php]

I am looking for the $totalbullets string to add up the total of all the cars that were selected. As of the moment it is only giving me back the value of the first one selected.

Hi Steven,

One big problem you are going to have is with this line:[php]$doit = $doit[$i];[/php]You are destroying your $doit array on the first iteration. Also, you are not outputting your $outcome variable and it will be overwritten with each iteration.

No promises, as I can’t test it, but see if this works for you (I tried to rewrite some of the code to simplify trouble shooting if it doesn’t work the first time around) I also added a couple of lines that will change the output line to read “car” when only 1 is being melted.:[php]$_POST[‘melt’] = 1;

if($_POST[‘melt’])
{
$n = count($doit);

if($n==1) $carsLabel = "car";
else $carsLabel = "cars";

for($i=0;$i<$n;$i++)
  {
    if($doit_str = $doit[$i])
      {
        $ifnota = mysql_query("SELECT * FROM cars WHERE id = $doit_str");
        $ifnot = mysql_fetch_array($ifnota);
        $ifnotname = $ifnot['owner'];
        $meltbullets = $ifnot['bullets'];
        $meltdamage = $ifnot['damage'];
        if($ifnotname == $gangsterusername)
          {
            if($meltdamage == '0')
              {
                $meltamount = $meltbullets;
              }
            else
              {
                $meltdamaged = 100 - $meltdamage;
                $meltamounttwo = $meltdamaged * $meltbullets;
                $meltamount = ceil($meltamounttwo / 100);
              }
            $totalbullets += $meltamount + $totalbullets;
          }
        }
      $showoutcome++;
      $outcome = "You melted $n $carsLabel and earned <font color=khaki><b>$totalbullets</b></font> bullets, Your crew took <b>$theperc</b>% of them!<br>";
      echo $outcome;
    }

}[/php]

That code alone has worked a treat, thankyou.

My pleasure! Glad it worked for you.

Let us know if you have any other issues.

jay

Sponsor our Newsletter | Privacy Policy | Terms of Service