Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Using PHP loops results

Options
  • 10-02-2006 2:00pm
    #1
    Closed Accounts Posts: 8,478 ✭✭✭


    I must be cracking up, but I can't seem to save my loop increments and use them outside the loop.

    What I'm trying to do is create a string variable and use it elsewhere in the page. I'm using a loop to generate it. But I don;t want to use the string inside the loop itself.

    So when I try to echo the varilable outside the loop after the loop has finished all I'm getting is the final iteration of the loop:
    for($i=$insertUpInit;$i<=50;$i++){
    	
    $actionInsertItems="action".$i;
    $actionInsertItems=$actionInsertItems.',';
    	
    //here i get full list of items I need
    echo $actionInsertItems;
    }//end loop
    
    //here i just get end item
    echo $actionInsertItems;
    
    

    I know that once someone points me in the right direction I'll slap my forhead quite hard.

    Help a brother out!


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    You need to initially declare the variable outside the loop. (afaik)


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    seamus wrote:
    You need to initially declare the variable outside the loop. (afaik)

    Tried that too. Doesnt work (for PHP at least)


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    D'oh I read it wrong. I thought you only *wanted* the final iteration :D

    Have a look at the loop again. Each time it iterates, you're resetting the variable.

    Declare the variable outside the loop, i.e. $actionInsertItems = "";

    Then replace the two lines inside the loop with
    [php]
    $actionInsertItems .= "action".$i;
    $actionInsertItems .= ',';
    [/php]


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    Oh Seamus, I'd kiss you if you weren't swimming with disease.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    I'm OK now, my mom bought me deodorant.


  • Advertisement
Advertisement