adly3000 0 Report post Posted February 22, 2006 i'd like to make for loop which dependes on a $_GET variable:here is my tries: if (isset($_GET['date']){switch($_GET['date']) { case 'asc':$exp1 = "$x>0";$exp2 = "$x=$total"; $condition = '$x++'; break; case 'desc';$exp1 = "$x=$total";$exp2 = "$x>0"; $condition = '$x--'; break; }} thenfor($exp1; $exp2; $condition){//** do something **//} that is what i want to do, sure this code is wrong so what is the valid code?!! Share this post Link to post Share on other sites
Spectre 0 Report post Posted February 23, 2006 (edited) Although I wouldn't recommend it, you could use something like: eval($exp1.';');while( eval('return '.$exp2.';') ) { /* Do something */ eval($condition.';');} This will, essentially, perform the same as a for() loop. Edited February 23, 2006 by Spectre (see edit history) Share this post Link to post Share on other sites
agentredeye 0 Report post Posted February 23, 2006 can i ask something for for loop.. can you make me a program on how to make a program about PALINDROME using for loop and array....... thanks.. just pm the code plsss... i need it badly.... Share this post Link to post Share on other sites
Tyssen 0 Report post Posted February 23, 2006 i need it badly....If you need it that badly, maybe you should pay someone to do it for you. Share this post Link to post Share on other sites
no9t9 0 Report post Posted February 24, 2006 trying to do what you want is not difficult but it is not a good way to code. It is better to use logic to get things done... also, Personally, I don't like switch statements, why not just keep it simple and use if statement for only 2 conditions. one other thing, assuming what you did works... you would have for(x>0,x=total,x++) and for(x=total,x>0,x--) ... that first for loop doesn't work... I am assuming you wanted for(x=0, x<total,x++). This is what you should do. if ($_GET['date'] == 'asc') { $i = 0; $j = $total }else if ($_GET['date'] == 'desc') {$i = -$total; $j = $0 }for ($x=$i;$x<$j;$x++) { do stuff } If you are actually using the count down counter for something just switch the sign (from neg to pos). i'd like to make for loop which dependes on a $_GET variable:here is my tries: if (isset($_GET['date']){switch($_GET['date']) { case 'asc':$exp1 = "$x>0";$exp2 = "$x=$total"; $condition = '$x++'; break; case 'desc';$exp1 = "$x=$total";$exp2 = "$x>0"; $condition = '$x--'; break; }} thenfor($exp1; $exp2; $condition){//** do something **//} that is what i want to do, sure this code is wrong so what is the valid code?!! Share this post Link to post Share on other sites
adly3000 0 Report post Posted February 24, 2006 thanks no9t9; if ($_GET['date'] == 'asc') { $i = 0; $j = $total }else if ($_GET['date'] == 'desc') {$i = -$total; $j = $0 }for ($x=$i;$x<$j;$x++) { do stuff } this helps alot, that is the the same as:$start = 0;$end = $total;$step = 1;if (isset($_GET{"date"}) && $_GET{"date"} == "desc") { $start = $total; $end = 0; $step = -1;}for ($x = $start; $x <= $end; $x += $step) {// something} Share this post Link to post Share on other sites