On my site I have page where rows are generated using below code:
<?php
while($row=mysql_fetch_assoc($sql)) {
if($row['type']=='1'){ $tipo="one";}
if($row['type']=='2'){ $tipo="two";}
if($row['type']=='3'){ $tipo="three";}
if($row['type']=='4'){ $tipo="four";}
if($row['type']=='5'){ $tipo="five";}
if($row['type']=='6'){ $tipo="six";}
if($row['type']=='7'){ $tipo="seven";}
if($row['type']=='8'){ $tipo="eight";}
if($row['type']=='9'){ $tipo="nine";}
if($row['type']=='10'){ $tipo="ten";}
?>
<tbody>
<tr>
<td>
<?php
if($row['type']=='9') {
echo 'Text One';
}
else {
echo 'Text two';
} ?>
</td>
</tr>
</tbody>
<?php
}
?>
After this I need a code which will do next thing:
If there is one row and $row['type']=='9'
than echo text "nine", if there is one row and $row['type']!=='9'
echo text "not nine", but if there are several rows and if for at least one of them $row['type']=='9'
echo text "both" else echo "non of them"
I can't figure it out how to do this. Could you help me a little?
use flag variable for what you want
Try something like this:
<?php
$row_cnt=0;
$type_9=false;
while($row=mysql_fetch_assoc($sql)) {
if($row['type']=='1'){ $tipo="one";}
if($row['type']=='2'){ $tipo="two";}
if($row['type']=='3'){ $tipo="three";}
if($row['type']=='4'){ $tipo="four";}
if($row['type']=='5'){ $tipo="five";}
if($row['type']=='6'){ $tipo="six";}
if($row['type']=='7'){ $tipo="seven";}
if($row['type']=='8'){ $tipo="eight";}
if($row['type']=='9'){ $tipo="nine";}
if($row['type']=='10'){ $tipo="ten";}
$rowcnt++;
?>
<?php
if($row['type']=='9') {
$type_9=true;
}
?>
<?php
}
?>
<tbody><tr><td>
<?php
if($row_cnt == 1)
{
if($type_9)
echo "nine";
else
echo "not nine";
}
else if($rowcnt > 1)
{
if($type_9)
echo "both";
else
echo "non of them";
}
?>
</td></tr></tbody>
I have omitted the HTML-tags but I think you can add those easily.
$tipo = array();
$multiple = false;
$present = false;
$numbers = array (
'1' => "one",
'2' => "two",
'3' => "three",
'4' => "four",
'5' => "five",
'6' => "six",
'7' => "seven",
'8' => "eight",
'9' => "nine",
'10' => "ten",
);
while ( $row = mysql_fetch_assoc($sql) )
$tipo[] = $numbers[ $row['type'] ];
$multiple = (count($tipo) > 1 ) ? true : false;
$present = ( in_array('9', $tipo) ) ? true : false;
if( $multiple ) {
if ( $present )
echo 'both';
else
echo 'none of them';
} else {
if ( $present )
echo 'nine';
else
echo 'not nine';
}