SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Lincolnshire, UK
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding values to an array...

    I know this should be simple, and i have looked all over the net for ways of doing it, but each time i try it it seems to just replace the last thing in the array, rather than add to it.

    What i want is for the user to click the submit button, and it takes what was in the form (just one input) and to add that to the end of the array, and then print it to the screen.

    the code i have for showing to contents of the array is
    PHP Code:
    foreach($teams as $value)
    {
       print 
    "$value ";

    if you could show me the line or two that goes above that i would be greatful.
    PS. the way of capturing the data from the form is $_POST['team'].

    thanks, Gareth

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Gareth.

    $teams = "newteam"; //will overwrite your old array with the string "newteam"
    $teams[] = "newteam"; // will append "newteam" to the end of your array

    Derek

  3. #3
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Lincolnshire, UK
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm i dont get it. Im tried the 1st reply so far, and ill show you the code in a sec... but it all looks like it should work. i did
    PHP Code:
    <?
    $teams
    [] = $_POST['team'];
    foreach(
    $teams as $value)
    {
       print 
    "$value ";
    }
    ?>
    to try and test it, but again it just overwrites the old value in the array with the new one i type. Maybe its a problem with the "display the array" code?

    thanks, Gareth

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Lincolnshire, UK
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bump

    this is really getting on me nerves :P

  6. #6
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you try this?
    PHP Code:
    <?php
    array_push
    ($teams$_POST['team']);
    foreach(
    $teams as $value)
    {
       print 
    "$value ";
    }
    ?>
    If it doesn't work, show us a print_r() of the teams array before you try to add to the array . . .
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Lincolnshire, UK
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is still not working, even with your code bjcffnet. The teams array will not have a value already in it, i want it to start blank, and then get added to everytime the user submits the form.

    i think that explains what you meant by "show us a print_r() of the teams array before you try to add to the array", but in pretty new to PHP and totally new to using arrays in PHP.

    If you could help me any more i would be very greatful

    thanks, gareth

  8. #8
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is not just arrays, it's also sessions. If you want to add something everytime the user submits, you must fill some variable that exists between requests -- a persistent variable. Normal variables don't exist until the script is executed and are then destroyed. That's why you always get only one value in the array -- the value that was just posted.

    For sessions, see Session Handling Functions.

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Lincolnshire, UK
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So i have to assign the $_POST['team'] to a session? then the session into a $var, then add that var into the array?

    thanks, Gareth

  10. #10
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I asume you will read on how to start a session, so I'll start from there:
    PHP Code:
    $_SESSION['teams'][] = $_POST['team'];
    print_r($_SESSION['teams']); 

  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Lincolnshire, UK
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol i feel like such an idiot... i dont know why but it just didnt occur to me that when the page is re-loaded the old array would be lost... thats why i was only getting the one output

    thanks, Gareth


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •