5 Replies Latest reply on Nov 5, 2009 12:48 PM by curiousdave

    Passing multiple ID's in URL variable

      How can I show results from a mysql database from selected ID's using URL variables?
      Showing results from one ID works fine:
      mypage.php?id=1
      But,
      My table has 10 rows and I want to show 3 of those rows:
      mypage.php?id=1&id=3&id=4

      Thanks for a point in the right direction.
        • 1. Re: Passing multiple ID's in URL variable
          Günter Schenk Level 4
          Hi,

          this won´t work, because you can´t reuse the same "id" parameter for multiple values, means you need to rename them like this:

          mypage.php?id_1=1&id_2=3&id_3=4

          On "mypage.php" you´ll have to modify the regular query by concatenating those 3 parameters using AND:

          SELECT * from your_table WHERE id = $_GET['id_1'] AND id = $_GET['id_2'] id = $_GET['id_3']

          Please don´t take the mentioned query syntax literally -- it´s just *how* you´d have to do it

          Günter Schenk
          Adobe Community Expert, Dreamweaver
          • 2. Re: Passing multiple ID's in URL variable
            Level 1
            Günter_Schenk@adobeforums.com wrote:

            > SELECT * from your_table WHERE id = $_GET['id_1'] AND id = $_GET['id_2'] id = $_GET['id_3']

            Although those ANDs should be ORs:
            SELECT * from your_table WHERE id = $_GET['id_1'] OR id = $_GET['id_2']
            OR id = $_GET['id_3']

            Another option is to not use a query string, but rather store the IDs in
            a sessions like $_SESSION['ids']='(1,2,3)'. Then use an IN clause in the
            SQL:

            SELECT * from your_table WHERE id IN $_SESSION['ids']

            Unfortunately, since the DW 8.01 update (and in CS3) the DW recordset
            does not support this, so you'd need to do some hand coding.
            --
            Alec
            Adobe Community Expert
            • 3. Re: Passing multiple ID's in URL variable
              Level 1
              Hi,
              Thank you for your reply.
              I need for this to be dynamic.
              another url may be:
              mypage.php?id=7&id=2&id=8&id=10
              or
              mypage.php?id=2&id=1&id=9&id=7

              I would like to be able to pass them as an array.
              maybe like this?
              mypage.php?id[]=7&id[]=2&id[]=8&id[]=10

              I just don't know how to setup the recordset to do this

              I need to be able to pull selected rows from one table.
              Thanks for any help.

              I posted this question in the Dreamweaver support form and got the following answer.
              I haven't tried it because I would like to do this using the tools available in DW8 and Developers toolbox if possible.

              .oO(RGNelson)

              >Thanks for working with me on this!
              > Where do I put the brackets?
              > I'm not using a form.

              Ah, OK. But doing that by hand can be a bit ... fiddly ...

              > I'm using a link;
              > mypage.php?id=1&id=3&id-4
              >
              > These don't work for me:
              > mypage.php?id[]=1&id[]=3&id[]=4
              > mypage.php?id=1[]&id=3[]&id=4[]

              The brackets have to be witten after the variable name and be URL-
              encoded, %5B%5D should do it (a simple hex-encoding of ASCII-codes).
              You also have to encode the ampersands in your HTML. So a link to the
              target page should look something like

              link

              Now the PHP script should be able to receive the data and correctly
              store it as an array in $_GET['id'].

              > Do I need to put the brackets in my recordset?

              If you have to rely on DW's behaviours, then someone else has to jump
              in - I always write my own code.

              Here's a little code snippet for doing the dirty work, maybe you can
              make some use of it:

              // check if there are any IDs at all in the URL
              if (!empty($_GET['id'])) {
              // fetch all submitted IDs as numeric values, which is an
              // easy way to prevent SQL-injection in this specific case;
              // the array_diff() call filters out 0-IDs
              $ids = array_diff(array_map('intval', $_GET['id']), array(0));
              // if there's something left, saddle up for the DB query
              if (!empty($ids)) {
              // turn the ID-array into a comma-separated string
              $ids = implode(', ', $ids);
              // build the query, use the IN operator to match the selected IDs
              $query = "
              SELECT someField, anotherField
              FROM yourTable
              WHERE recordId IN ($ids)";
              // execute query, fetch result, print it out, have a break
              // ...
              }
              }

              Micha
              • 4. Re: Passing multiple ID's in URL variable
                Please I need help !
                how can I display file size ?
                I use ADDT - PHP Application Development.
                Hynnox
                • 5. Re: Passing multiple ID's in URL variable
                  curiousdave

                  Are any of you original participants in this thread still around? I'm a bit of a "Jack of all trades, master of none" and I'm having a hell of time. I'm pretty sure this post relates to what I'm trying to do, but I'm just starting in dynamic site building with PHP and I can't seem to translate this situation into my situation.

                   

                  What I'm trying to do is set up a series of menu items for categories of merchandise (rugs), with subcategories for size. I've succeeded in making links where the user can choose ALL sizes within a category and (separately) I've succeeded in making links where the user can choose a size within a specific category, but I'm not able to create links where the user can choose a specific size OR see all sizes out of a single menu.

                   

                  Here's what I've got:

                   

                  3 tables: products, categories & size
                  The size table has 2 columns - sizeName, and SizeID - the sizeID column is an integer and it links to products table.
                  The categories table also has 2 columns - categoryName and categoryID - the categoryID column is also an integer which links with the product table

                   

                  I also built a menu with categories, and each category has a drop down menu where the viewer can choose a size.
                  Each size link has a parameter which includes the categoryID as well (in following example, all the sizes are within one category):


                  small - products.php?categoryID=8&sizeID=1
                  medium - products.php?categoryID=8&sizeID=2
                  large - products.php?categoryID=8&sizeID=3

                   


                  On the receiving page (products.php) I created a recordset which looks like this (I'm looking at the recordset dialog box within Dreamweaver CS4):

                   

                  SELECT price, `description`, categoryID, largeImage, smallImage, sizeID
                  FROM products
                  WHERE categoryID = colname AND sizeID = varSize
                  ORDER BY price ASC

                   

                  the variables look like this:

                   

                  Name: colname
                  type: Integer
                  default value: -1
                  Runtime value: $_GET['categoryID'}

                   

                  Name: varSize
                  Type: Integer
                  Default value: 1
                  Runtime value: $_GET['sizeID']

                   

                  (Dreamweaver created colname and I created varSize. I'm not really sure why the default values are as they are).

                   

                   

                  This works, but it doesn't give the "all sizes" option. So I've tried applying what I saw in the thread, by creating this link instead:

                   

                  products.php?categoryID=8&sizeID=1&sizeID=2&sizeID=3

                   

                  In the recordset I created the new variables like this (all as integers with 1 as default value):

                   

                  varSmall = $_GET['sizeID_1']

                  varMedium = $_GET['sizeID_2']

                  varLarge = $_GET['sizeID_3']

                   

                  And I wrote this code:

                   

                  SELECT price, `description`, categoryID, largeImage, smallImage, sizeID
                  FROM products
                  WHERE categoryID = colname AND sizeID = varSize
                  OR
                  WHERE categoryID = colname AND sizeID = varSmall AND sizeID = varMedium AND sizeID = varLarge

                   

                  But within the recordset dialog box, I hit the test button and I got a message about a syntax error.

                   

                   

                  Am I anywhere near the solution? Any help would be greatly appreciated.

                   

                  Thanks,
                  Dave