Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts

    Passing value of html link to SELECT query

    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.

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

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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.

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

    Code:
    <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:

    PHP Code:
    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.

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    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:

    PHP Code:
    <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.

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

  • #5
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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
    PHP Code:
    $gallery urlencode('Black & White');
    echo 
    '<a href="gallery.php?gallery='.$gallery.'>Black & White</a>'
    Then the gallery title is available as
    PHP Code:
    $gallery $_GET['gallery'// equals 'Black & White' 

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    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.

    PHP Code:
    <?php
    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.
    Leonard Whistler


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •