Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> For Loop, for loop problem
adly3000
post 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?!!
Go to the top of the page
 
+Quote Post
Spectre
post 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
Go to the top of the page
 
+Quote Post
agentredeye
post 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....
Go to the top of the page
 
+Quote Post
Tyssen
post Feb 23 2006, 06:03 AM
Post #4



***********

Group: Members
Posts: 1,161
Joined: 9-May 05
From: Brisbane, QLD
Member No.: 6,818



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.
Go to the top of the page
 
+Quote Post
no9t9
post 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).

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

Go to the top of the page
 
+Quote Post
adly3000
post 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
}
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Problem Concerning Credits Recuded(8)
  2. Problem With Hosting(4)
  3. Firefox 3 Cookies Problem(4)
  4. What Is My (power) Problem?(4)
  5. Did You Face Problem Opening Pdf With Firefox?(15)
  6. Attachment Problem In Pmwiki(0)
  7. Pop-up Virus / Trojan Problem(10)
  8. Problem With The Trap17 Forum(4)
  9. Remote Assistance Problem(9)
  10. Counter Strike Problem(2)
  11. Laptop Keyboard Problem(8)
  12. Upload Component - Failed(6)
  13. Hosting Credits Problem? [resolved](9)
  14. Dandruff Problem(29)
  15. 60 Seconds Countdown That Shutdown The Pc(15)
  1. Sharing Files And Printer Between Windows Vista And Xp(9)
  2. Problem Installing Sims 2 Nightlife(26)
  3. I Have A Girl Problem Here(27)
  4. Problem With My Girlfriend Family, Need Suggestions For Action(2)
  5. Problem With Page Redirect(8)
  6. Terminating Hosting Problem(1)
  7. Browser Compatibility Problem With Firefox - Javascript + Css(3)
  8. How Many More Times Will I Have To Post About This?(11)
  9. Domain Problem : This Is Important..(0)
  10. Skype / Xampp Port Conflict(5)
  11. Php Header Problem(11)
  12. A Problem With Ftp Clients(9)
  13. Youtube People That Do Not Respect Other Opinions(7)
  14. Itunes Problem!(1)
  15. Problem Aligning In Firefox(9)


 



- Lo-Fi Version Time is now: 7th October 2008 - 09:23 AM