Results 1 to 6 of 6

Thread: passing array in URL, cannot make $_GET produce array

  1. #1
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face passing array in URL, cannot make $_GET produce array

    Hello,
    I want to pass an array through a URL, then select data in my database from those values.
    I have this URL:
    ../elements_by_id.php?id[]=1&id[]=2&id[]=5
    Then I use this code:
    PHP Code:
    $id = array();
    $id $_GET{'id'};
    $id_select="";
    $count count($id);
    $incr 0;
    foreach (
    $id as $ids) {
        
    $id_select.="$ids";
        
    $incr $incr 1;
        if (
    $count $incr) {$comma ", ";}
        else {
    $comma "";}
        
    $id_select.="$comma";

    - to produce the array and from that a mysql select statement.

    The idea is that the $id_select should end up having this value:
    "1, 2, 5" (*)
    - but i appears to be empty. Can anyone see what I am doing wrong?

    (*) Will be using this in an mysql statement like this:
    "select * from mytable where id in (1, 2, 5)"

  2. #2
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OK - perhaps I should inverse the if statement and use "==" instead of "=":
    Code:
    if ($count == $incr) {$comma = "";}
    else {$comma = ", ";}

  3. #3
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face

    It works - sorry ...

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Your best bet would be to do something like this (modified the code you posted):

    Code:
    $id_select="";
    
    $count = count($_GET['id']);
    
    $incr = 0;
    
    foreach ($_GET['id'] as $id) {
        $id_select.="$id";
        $incr = $incr + 1;
        if ($count == $incr) {$comma = ", ";}
        else {$comma = "";}
        $id_select.="$comma";
    }
    Hope this helps.

    (Note: Not tested for your purposes, but if you go to http://phphost.smackum.com/test/test.php, you can see what how I came up with this code. (The source is at http://phphost.smackum.com/test/test.phps))

    //EDIT: Sorry, I was typing as you posted. Glad to hear you worked it out.
    Last edited by thetestingsite; 08-19-2007 at 10:38 PM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks mate - your code is perhaps a bit more condensed but essentially the same? My error was apparently the typical == error - and the wrong if statement ..

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Neither of you have done any validation on that -- they'll own your server before you can say "SQL injection."
    Code:
    $id_select = implode(', ', array_filter($_GET['id'], 'is_numeric'));
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •