Skip to main content
Participant
July 25, 2007
Question

Passing multiple ID's in URL variable

  • July 25, 2007
  • 1 reply
  • 538 views
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.
This topic has been closed for replies.

1 reply

Inspiring
July 25, 2007
.oO(RGNelson)

>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

Change the name of the form control from "id" to "id[]". This will turn
$_GET['id'] into an array, where you can find all selected IDs.

Micha
Inspiring
July 25, 2007
.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

<a href="mypage.php?id%5B%5D=1&id%5B%5D=3&id%5B%5D=4">link</a>

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
RGNelsonAuthor
Participant
July 26, 2007
Yes, thanks, your right.
I am trying to do this with Adobe behaiviors.
I thught it might be easy to do this with the tool box or straight DW8.
Thank you for your details.