Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Using PHP loops results

  • 10-02-2006 02: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,173 ✭✭✭✭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,173 ✭✭✭✭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,173 ✭✭✭✭seamus


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


  • Advertisement
Advertisement