View Full Version : Passing value of html link to SELECT query

Len Whistler
01-20-2007, 07:03 AM
How can I pass the value of a html link to a SELECT query? In the example below I would like the $gallery variable to change depending on the link selected.

mysql_query("SELECT COUNT(*) as Num FROM images WHERE image_cat = '$gallery'"),0);

01-20-2007, 09:37 AM
well... not quite sure which link you mean.

The requested page you can get through $_SERVER['REQUEST_URI'] for example. You could then extract the portion that you need.

But still I don't know if this is what you're asking about.

01-20-2007, 05:18 PM
In a straight-forward simple situation I would take advantage of a url's query string. That's the stuff you see on a url after the question mark (i.e. http://codingforums.com/newreply.php?do=newreply&noquote=1&p=525383).

Just have your link include a query string, then read the query string in using $_GET.

<a href="viewgallery.php?gid=12345>link to gallery 12345</a><br />
<a href="viewgallery.php?gid=23456>link to gallery 23456</a><br />
<a href="viewgallery.php?gid=34567>link to gallery 34567</a><br />
<a href="viewgallery.php?gid=45678>link to gallery 45678</a><br />

Now in viewgallery.php:

if (!empty($_GET['gid'])) {
$gallery = $_GET['gid'];
else {
$gallery = '';

You'll want to also perform some checks on $_GET['gid'] to make sure someone doesn't try to inject sinister code into your query.

Len Whistler
01-20-2007, 10:52 PM
Ok thanks for the replies. I will try to be more clear.

Fumigator in your example I think once a gallery is selected the next gallery, or previous gallery, can also be viewed by selecting the page number or next link with php pagination. I have something similiar on my Stubby site.

In my database I have a image_cat column which has enteries like Panoramics, Black & White, Travel, etc. Travel could be entered 50 times or so, Panoramics maybe 35 times.

I then have a menu like:

<a href="">Black & White</a>
<a href="">Panoramics</a>

I would like to pass the link selected to the $gallery variable below displaying only images in that category and not have other galleries show up in the next link and previous links that pagnation generates.

mysql_query("SELECT COUNT(*) as Num FROM images WHERE image_cat = '$gallery'"),0);

01-20-2007, 11:15 PM
I think Fumigator is right that it would be easier if the galleries like 'Black & White', 'Panoramics' in addition to these descriptive names also had a numeric identifier. You could then just pass this value through the URL.

If it is not possible to add such a column, you will have to pass the title itself through for example the URL. It is a bit clumpsy, but something like

$gallery = urlencode('Black & White');
echo '<a href="gallery.php?gallery='.$gallery.'>Black & White</a>';

Then the gallery title is available as

$gallery = $_GET['gallery'] // equals 'Black & White'

Len Whistler
01-21-2007, 03:53 AM
Ok thanks for the replies. This is what I have ended up doing, for every gallery created a new php file and link to it.

include "db_connect.php";
$gallery = "rail";
$title = "Railway images - CNR - CPR";
include "header.php";
include "gallery.php";
include "footer.php";

The only variables I change are $gallery for the proper MySQL data and $title for page title.