simple array question

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

    simple array question

    Hi,

    (Newbie to php). I am trying to read in a list of ids. Here is my
    url



    Then I just want to print out the ids, but I can't seem to get it
    work. I search php.net and have tried a dozen things I can't get to
    work

    Tried:
    print "<BR> 0=" . $_REQUEST['id'][0];
    print "<BR> 1=" . $_REQUEST['id'][1];

    But it only prints the last id.

    Tried:
    foreach ($_REQUEST['id'] as $value) {
    print "<BR> $value";
    }


    Can someone help simple question?
    thanks
    chad
  • Senator Jay Billington Bulworth

    #2
    Re: simple array question

    In article <1e6ea9f6.04031 41917.4e46c6fe@ posting.google. com>,
    chad@windmeadow .com (chad phillips) wrote:
    [color=blue]
    > Hi,
    >
    > (Newbie to php). I am trying to read in a list of ids. Here is my
    > url
    >
    > http://localhost/a.php?id=1&id=2&id=3[/color]

    What you want is:

    http://localhost/a.php?id[]=1&id[]=2&id[]=3

    Your code should work simply by changing the URL. It's not pretty, but
    AFAIK it's the only way to pass an array through the query-string.

    hth

    --
    Bulworth : funha@fung.arg | My email address is ROT13 encoded, decode to mail
    --------------------------|--------------------------------------------------
    <http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!

    Comment

    • Jedi121

      #3
      Re: simple array question

      Senator Jay Billington Bulworth a écrit le 15/03/2004 :[color=blue]
      > What you want is:
      >
      > http://localhost/a.php?id[]=1&id[]=2&id[]=3
      >
      > Your code should work simply by changing the URL. It's not pretty, but
      > AFAIK it's the only way to pass an array through the query-string.
      >
      > hth[/color]

      I suggest using serialize() and unserialize() to pass arrays in URL.
      Beware of the protection of slashes, you may need to use stripslashes()
      as well.


      Comment

      • Kevin Thorpe

        #4
        Re: simple array question

        chad phillips wrote:[color=blue]
        > Hi,
        >
        > (Newbie to php). I am trying to read in a list of ids. Here is my
        > url
        >
        > http://localhost/a.php?id=1&id=2&id=3
        >
        > Then I just want to print out the ids, but I can't seem to get it
        > work.[/color]

        Why don't you pack up the ids in a delimited array if they're simple
        numbers:



        Then:
        $ids = explode(',',$_G ET['id']);

        foreach ($ids as $id) {
        ...
        }

        Comment

        • chad phillips

          #5
          Re: simple array question

          That would work. In perl I can read in
          http://localhost/a.php?id=1&id=2&id=3. I figured there must be a way
          to do the same thing in php. As someone pointed out I can change them
          to http://localhost/a.php?id[]=1&id[]=2&id[]=3, but it seems weird
          that php could handle id=1&id=2&id=3

          thanks
          chad


          Kevin Thorpe <kevin@pricetra k.com> wrote in message news:<40558f73$ 0$3767$afc38c87 @news.easynet.c o.uk>...[color=blue]
          > chad phillips wrote:[color=green]
          > > Hi,
          > >
          > > (Newbie to php). I am trying to read in a list of ids. Here is my
          > > url
          > >
          > > http://localhost/a.php?id=1&id=2&id=3
          > >
          > > Then I just want to print out the ids, but I can't seem to get it
          > > work.[/color]
          >
          > Why don't you pack up the ids in a delimited array if they're simple
          > numbers:
          >
          > http://localhost/a.php?id=1,2,3
          >
          > Then:
          > $ids = explode(',',$_G ET['id']);
          >
          > foreach ($ids as $id) {
          > ...
          > }[/color]

          Comment

          • Andy Hassall

            #6
            Re: simple array question

            On Mon, 15 Mar 2004 03:28:16 GMT, Senator Jay Billington Bulworth
            <funha@fung.arg > wrote:
            [color=blue]
            >In article <1e6ea9f6.04031 41917.4e46c6fe@ posting.google. com>,
            > chad@windmeadow .com (chad phillips) wrote:
            >[color=green]
            >> (Newbie to php). I am trying to read in a list of ids. Here is my
            >> url
            >>
            >> http://localhost/a.php?id=1&id=2&id=3[/color]
            >
            >What you want is:
            >
            >http://localhost/a.php?id[]=1&id[]=2&id[]=3
            >
            >Your code should work simply by changing the URL. It's not pretty, but
            >AFAIK it's the only way to pass an array through the query-string.[/color]

            IMHO this is the one thing that ASP does right over PHP (I suppose it has to
            do _something_ right). Send multiple values and you get an array; you don't
            need to put [] in the name, causing arguments about whether it's valid HTML (it
            is).

            If you have multiple elements with the same name at the moment, I'm not
            entirely sure it's deterministic which value you'll end up with in PHP, and
            non-determinism is generally annoying in a programming language. If it's
            deterministic, I don't think it's documented, which is nearly as bad.

            I think PHP ought to at least have a config option to accept multiple values
            not ending in [] as arrays - although I suppose since PHP is open-source I
            ought to just get on with it and write a patch if it bothers me ;-)

            --
            Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
            <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

            Comment

            • Chung Leong

              #7
              Re: simple array question

              Uzytkownik "Andy Hassall" <andy@andyh.co. uk> napisal w wiadomosci
              news:do7c50991e a50kb7edp60f5p5 52eo9lk6g@4ax.c om...[color=blue]
              > IMHO this is the one thing that ASP does right over PHP (I suppose it has[/color]
              to[color=blue]
              > do _something_ right). Send multiple values and you get an array; you[/color]
              don't[color=blue]
              > need to put [] in the name, causing arguments about whether it's valid[/color]
              HTML (it[color=blue]
              > is).[/color]

              That would cause massive number of type-mismatch errors, since PHP doesn't
              automatically convert a scalar to an array with one item based on the
              context.
              [color=blue]
              > If you have multiple elements with the same name at the moment, I'm not
              > entirely sure it's deterministic which value you'll end up with in PHP,[/color]
              and[color=blue]
              > non-determinism is generally annoying in a programming language. If it's
              > deterministic, I don't think it's documented, which is nearly as bad.[/color]

              There's documentation, kind of. The comment for variables_order in php.ini
              reads "[r]egistration is done from left to right, newer values override
              older values." In the OP, $_GET['id'] would yield 3.


              Comment

              • Andy Hassall

                #8
                Re: simple array question

                On Mon, 15 Mar 2004 19:16:34 -0500, "Chung Leong" <chernyshevsky@ hotmail.com>
                wrote:
                [color=blue]
                >Uzytkownik "Andy Hassall" <andy@andyh.co. uk> napisal w wiadomosci
                >news:do7c50991 ea50kb7edp60f5p 552eo9lk6g@4ax. com...[color=green]
                >> IMHO this is the one thing that ASP does right over PHP (I suppose it has[/color]
                >to[color=green]
                >> do _something_ right). Send multiple values and you get an array; you[/color]
                >don't[color=green]
                >> need to put [] in the name, causing arguments about whether it's valid[/color]
                >HTML (it[color=green]
                >> is).[/color]
                >
                >That would cause massive number of type-mismatch errors, since PHP doesn't
                >automaticall y convert a scalar to an array with one item based on the
                >context.[/color]

                Well, yes, there is that. Fair point.

                But people have nearly got over register_global s, we need a new configuration
                option to break things again ("Why do all my database rows have 'Array' in
                them?!") ;-)

                (Slightly related: I'm not that keen on arrays stringifying to Array either -
                ought to raise a warning, I don't see how 'Array' is useful).
                [color=blue][color=green]
                >> If you have multiple elements with the same name at the moment, I'm not
                >> entirely sure it's deterministic which value you'll end up with in PHP,[/color]
                >and[color=green]
                >> non-determinism is generally annoying in a programming language. If it's
                >> deterministic, I don't think it's documented, which is nearly as bad.[/color]
                >
                >There's documentation, kind of. The comment for variables_order in php.ini
                >reads "[r]egistration is done from left to right, newer values override
                >older values." In the OP, $_GET['id'] would yield 3.[/color]

                Variables_order is for the order that globals and $_REQUEST source their
                information from; if it's GP then reading from left to right, GET variables are
                overwritten by POST variables. But it doesn't say what happens for the multiple
                GET variables of the same name.

                3 seems the logical choice but I can't find the documentation that actually
                says that.

                The HTML spec at least guarantees the order they're sent out - same 'order
                they appear in the document stream' (HTML4.01 sec 17.3.4).

                --
                Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

                Comment

                Working...