Jump to content
xisto Community
Sign in to follow this  
adly3000

For Loop for loop problem

Recommended Posts

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;	}}

then

for($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

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 by Spectre (see edit history)

Share this post


Link to post
Share on other sites

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;	}}

then

for($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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.