Forum Moderators: coopster

Message Too Old, No Replies

Passing URL Parameters with Multiple IDs

Please do help a newbie out

         

PeteE

12:15 am on Jul 11, 2005 (gmt 0)

10+ Year Member



Hi Guys, Hows it?

I'm pretty new to php and is currently trying to build a fully dynamic website for a clothing company, and now it's forced me to a dead end to what the client want, so if you can help out it would be greatly appreciated! :D

The problem is as follows:

I'll build a table in MySql database containing each individual details for each clothing item, which all item will be displayed in a ordinary repeating thumbnails fashion, where when you click on a certain thumbnail it passes along a URL Parameter(recordID=#) so that the corrosponding page to filters out by the recordID, which works fine(to my knowledge).

Then in the detail page of this certain product, it'll have a dynamic link to stockist.php
this is where I'm stuck at...

The 'stockist.php' will be displaying a table(stockists_table), from the database where it contains ALL stockists details, individual stockists stocks different products. Each stockist in the database will have an unique id to it.

So what I am trying to do is to filter out stockists_table by a url parameter containing 'multiple'(don't know if this is the way to do it or not) id numbers passed along by the 'stockists' link from the detailed product page, so that it only displays the right stockist(s) for this certain item, on the stockists.php page. for e.g. sometimes it will only require stockists with id=2 and id=4 to be shown.

I've tried passing stockists.php?recordID=2, 4, with stockists.php set to filter by recordID
and ofcourse it didn't work.
(yes, I know I'm a newbie)

p.s. Since I'm a newbie I've been doing all these through dreamweaver mx

Thanks in advance! Grin

Cheers

mogenshoj

12:36 am on Jul 11, 2005 (gmt 0)

10+ Year Member



Try using this, i made a few comments for it. So you see whats going on.

If pass the ID's from somepage.php to stockist.php, pass the ID's like this:

stockist.php?sid=1-3-6-8
Where the numbers would be ID's.

In stockist.php put this:
----
$array = split("-", $_GET[sid]); // Removes -, and split the variable in an array
$count = count($array); // count the number of ID's in the array
for ($i=0; $i<$count; $i++) // Get all ID's out
{
$sql_where .= " ¦¦ id='$array[$i]'"; // Put them in a sql statement
}

$sql_where = substr($sql_where, 4); // remove the first " ¦¦ " to prevent sql error

$sql = mysql_query("select * from stockists_table where $sql_where order by id asc"); // sql call

-------
Not the prettiest code, but it should work.

PeteE

1:12 am on Jul 11, 2005 (gmt 0)

10+ Year Member



Hey man, thanks for the fast reply,

I've quickly tried it, it's coming back as 'Query was empty'

Since I'm the newbie, I figured the problem will be where I've put the code, as you can see I'm testing it on an extisting database driven site that I've built previously, code as follows:

<?php require_once('Connections/visionary_connection.php');?>
<?php
mysql_select_db($database_visionary_connection, $visionary_connection);
$single_programmes = mysql_query($query_single_programmes, $visionary_connection) or die(mysql_error());
$row_single_programmes = mysql_fetch_assoc($single_programmes);
$totalRows_single_programmes = mysql_num_rows($single_programmes);

$array = split("-", $_GET[sid]); // Removes -, and split the variable in an array
$count = count($array); // count the number of ID's in the array
for ($i=0; $i<$count; $i++) // Get all ID's out
{
$sql_where .= " ¦¦ id_sp='$array[$i]'"; // Put them in a sql statement
}

$sql_where = substr($sql_where, 4); // remove the first " ¦¦ " to prevent sql error

$sql = mysql_query("SELECT * FROM single_programmes where $sql_where order by id_sp ASC"); // sql call

?>

Sorry if it's messy but I did use Dreamweaver MX for all this :(.

Is there anything that I needed to delete or change before I added your code?

Thanks Heaps! :D

[edited by: jatar_k at 4:47 am (utc) on July 11, 2005]
[edit reason] no urls thanks [/edit]

dreamcatcher

7:54 am on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Split or explode will work fine, but you don`t need a loop to query your database.

>Example URL

url.php?id=1-2-3

>Explode or Split string

$get_ids = explode("-", $_GET['id']);

>Now you have them as an array, use implode:

$sql_ids = implode(",", $get_ids);

>Query the DB

$sql = mysql_query("SELECT * FROM single_programmes WHERE id IN ($sql_ids) order by id_sp ASC")

That should be fine.

dc

PeteE

9:01 pm on Jul 11, 2005 (gmt 0)

10+ Year Member



Thanks heaps you guys! :D

I will give those a ago.

CHeers,

Sorry about the URL in my second post.

jatar_k

9:26 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



not a problem :)