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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Using PHP loops results

  • 10-02-2006 1: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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


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


  • Advertisement
Advertisement