|
|
|
|
![]() ![]() |
Feb 22 2006, 02:56 PM
Post
#1
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 58 Joined: 31-January 06 Member No.: 17,937 |
i'd like to make for loop which dependes on a $_GET variable:
here is my tries: CODE 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; } } then CODE for($exp1; $exp2; $condition) { //** do something **// } that is what i want to do, sure this code is wrong so what is the valid code?!! |
|
|
|
Feb 23 2006, 01:44 AM
Post
#2
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 874 Joined: 30-July 04 Member No.: 246 |
Although I wouldn't recommend it, you could use something like:
CODE eval($exp1.';'); while( eval('return '.$exp2.';') ) { /* Do something */ eval($condition.';'); } This will, essentially, perform the same as a for() loop. This post has been edited by Spectre: Feb 23 2006, 01:44 AM |
|
|
|
Feb 23 2006, 04:10 AM
Post
#3
|
|
|
Newbie ![]() Group: Members Posts: 1 Joined: 16-November 05 Member No.: 14,367 |
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.... |
|
|
|
Feb 23 2006, 06:03 AM
Post
#4
|
|
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 1,161 Joined: 9-May 05 From: Brisbane, QLD Member No.: 6,818 |
i need it badly.... If you need it that badly, maybe you should pay someone to do it for you. |
|
|
|
Feb 24 2006, 03:10 AM
Post
#5
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 773 Joined: 4-November 04 Member No.: 2,118 |
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. CODE 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: CODE 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; } } then CODE for($exp1; $exp2; $condition) { //** do something **// } that is what i want to do, sure this code is wrong so what is the valid code?!! |
|
|
|
Feb 24 2006, 10:32 PM
Post
#6
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 58 Joined: 31-January 06 Member No.: 17,937 |
thanks no9t9;
CODE 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: CODE $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 } |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 7th October 2008 - 09:23 AM |