accessing values from an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mtjarrett

    #1

    accessing values from an array

    i having trouble accessing the values from superglobal arrays. there
    are two situations but i'm pretty sure it's the same problem.

    here's the deal:

    on page1.php i have several check boxes. before listing them i have
    the code
    $info = array();

    and then each checkbox has

    <input type="checkbox" name="info[]" value="Red">Red <br>
    <input type="checkbox" name="info[]" value="Blue">Bl ue<br> ...and so
    on

    the form posts to the same page, then checks the info and cleans it up
    if needed. then it sets it to a session variable like so:

    if (isset($_POST['submitted'])){

    $errors = array();

    if(!empty($_POS T['info'])){
    $_SESSION['info'] = escape_data($_P OST['info']);} else {
    $errors = 'bad';}
    }

    then it directs you to the next page, page2.php.

    on page2.php, i can't figure out how to access the array. i want to
    print each element of the array on a seperate line. w/o all the other
    it would look something like:

    echo "$info[1]<br>";
    echo "$info[2]<br>";

    i'd like to do this with a foreach loop but i can't even access one
    variable. not sure where to go from here.

    NOTE: when i was doing it earlier and passing the information from page
    to page with the post method and then finally sending myself an email
    with all the array infromation imploded into a string, it worked fine.

    thanks for your time and knowledge.
    -michael

  • ZeldorBlat

    #2
    Re: accessing values from an array

    >on page2.php, i can't figure out how to access the array. i want to[color=blue]
    >print each element of the array on a seperate line. w/o all the other
    >it would look something like:
    >
    >echo "$info[1]<br>";
    >echo "$info[2]<br>";[/color]

    Shouldn't it be:

    echo $_SESSION['info'][0] . "<br/>";
    echo $_SESSION['info'][1] . "<br/>";

    Or, if you want a foreach loop:

    foreach($_SESSI ON['info'] as $val)
    echo $val . " was checked<br/>";

    Comment

    • mtjarrett

      #3
      Re: accessing values from an array

      thanks, but i've tried that:[color=blue][color=green]
      >>echo $_SESSION['info'][0] . "<br/>";
      >>echo $_SESSION['info'][1] . "<br/>";[/color][/color]

      it seems that though $_SESSION['info'] is a set it only contains the
      word "Array". any ideas on that?

      thanks again.

      Comment

      • Jerry Stuckle

        #4
        Re: accessing values from an array

        mtjarrett wrote:[color=blue]
        > thanks, but i've tried that:
        >[color=green][color=darkred]
        >>>echo $_SESSION['info'][0] . "<br/>";
        >>>echo $_SESSION['info'][1] . "<br/>";[/color][/color]
        >
        >
        > it seems that though $_SESSION['info'] is a set it only contains the
        > word "Array". any ideas on that?
        >
        > thanks again.
        >[/color]

        That's what you get if you just echo $_SESSION because it is an array.

        Try

        print_r($_SESSI ON);


        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...