For Loop - for loop problem

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

For Loop - for loop problem

adly3000
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?!!

 

 

 


Reply

Spectre
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.

Reply

agentredeye
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....

Reply

Tyssen
QUOTE(agentredeye @ Feb 23 2006, 02:10 PM) *

i need it badly....

If you need it that badly, maybe you should pay someone to do it for you.

Reply

no9t9
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).

QUOTE(adly3000 @ Feb 22 2006, 09:56 AM) *

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?!!

 

 

 


Reply

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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : loop loop

  1. Loop Through Form - How do you do it? (8)
    In ASP you'd do this: CODE For Each Field in Request.Form So how do you do it in PHP? I
    want to loop through a form, check to see if it has a value and if so, append to a string to send as
    the body of my email....
  2. Php Date() Problem [resolved] - date() seems to loop same ninute. (4)
  3. Help Needed With Directory/file Listing Code Infinite Loop - Made an infinite loop but why is this so? (5)
    Hi all ive got a small and simple (for the moment atleast /unsure.gif"
    style="vertical-align:middle" emoid=":unsure:" border="0" alt="unsure.gif" /> )file and directory
    listing script in php as follows CODE $dir = "."; $num = 0; $file =
    scandir($dir); while($file = scandir($dir)){     echo
    $file[$num];     echo "<BR>";     $num = $num + 1;     
    }; the concept is simple enough, the directory to start with is the current one, so scan this
    directory and wh...
  4. For Each Loop Help - (1)
    I have a page where I list a number of records from my db. Under each record I have a checkbox and a
    text field. At the bottom of the list I have a submit button. I want to be able to check various
    boxes and edit the text field then click the button to update all of the records. I currenlty have
    the following and it updates the records ok (checkbox, I haven't messed with the text field yet)
    when I first check the boxes but it will not update the db when I uncheck the boxes. My form: PHP
    Code: CODE <form name="form1" method="post" action=my...
  5. While Loop - how to stop the timeout (9)
    I have written a piece of code that is to make a forum which contains a table consiting of two
    colums and a determind number of rows with a while loop, however when I test the page it goes and
    goes, and then returns an error stating that the maximum 30 second limit has been reached, I
    understand what that means I just don't know why it's looping forever: CODE while
    ($weightnum > 0); { echo "<tr>";    echo  
    '<td><input name="wname"' . $appendval . ' type="text"
    id="wname...



Looking for loop, loop, problem

Searching Video's for loop, loop, problem
advertisement



For Loop - for loop problem



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE