Jump to content

if statment inside a for loop


hyster

Recommended Posts

im having problems with the count in the if statment. its only counting the last value _posted.

im missing something really simple but i can not see it.

 

help please.

 

<?php
$count= $_GET['seatco'];

for ($i=1; $i<=$count; $i++)
{

$ticket = $_POST['tick_com'.$i];

echo "tickets: ".$ticket;
echo "<br>";

$adult= "0";
$child= "0";
$concession = "0";


	if ($ticket =="adult") {
        $adult++;
	}
    elseif($ticket == "child"){
       $child++;
    }
    elseif($ticket =="concession"){
        $conc++;
	}
else{ 
	echo "no";}
}
}
echo "Adult".$adult;
echo "Child".$child;
echo "Concession".$conc;
?>

Link to comment
https://forums.phpfreaks.com/topic/231179-if-statment-inside-a-for-loop/
Share on other sites

Hi, try moving the starting counts out of the loop

 

<?php
$count= $_GET['seatco'];
$adult= 0;
$child= 0;
$concession = 0;

for ($i=1; $i<=$count; $i++)
{

$ticket = $_POST['tick_com'.$i];

echo "tickets: ".$ticket;
echo "<br>";

if ($ticket =="adult") {
        $adult++;
}
    elseif($ticket == "child"){
       $child++;
    }
    elseif($ticket =="concession"){
        $conc++;
}
else{ 
echo "no";}
}
}
echo "Adult".$adult;
echo "Child".$child;
echo "Concession".$conc;

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.