river rhun river rhun - 2 months ago 7
PHP Question

can't get the logic to restart looping

I have 15 databases 1,2,3,4, ... 15
and I have the variable $i default at 1 and each time the $i count gets to 3, $i restarts the looping from 1 again until 3 and it will stop until value from database is done counting.

$detailPsycho = mysql_query("SELECT * FROM `psycho` WHERE `flag` = 2 ") or die(mysql_error());
while($detail = mysql_fetch_array($detailPsycho)){

for($i = 1;$i<=3;$i++){
echo $detail['sequence']."&".$i." <br>";
}
}


and run over like this :/


1&1
1&2
1&3
2&1
2&2
2&3


Answer

You try to do a loop (for{}) in a loop (while{}) which is a complicated way of doing something trivial. This is the "while+for" result :

$detailPsycho = mysql_query("SELECT * FROM `psycho` WHERE `flag` = 2 ") or die(mysql_error());

i = (int) 1;
while($detail = mysql_fetch_array($detailPsycho)) {

    echo $detail['sequence']."&".$i." <br>";
    $i++;
    if ($i > 3) $i = 1;
}