Advanced Search

Results 1 to 4 of 4

Thread: PHP Index html link to populate other page

  1. #1
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default PHP Index html link to populate other page

    Hey i currently have an index page , within this index page i have a navigation bar.

    What i would like to do is when a button is pressed , for example whe the button called "Fiction" is pressed i would like my page to redirect to my other page book_category_list.php.

    On this page i would like all the books with the category list called "Fiction" to populate.

    But if i pressed the button called "Horror" i would like the page book_category_list.php to populate the page with the categorys "Horror".

    my current solution is to have a number of different pages and have this line of code on there.

    this code is on the book_category_list.php page
    PHP Code:
    $sql mysql_query("SELECT * FROM books WHERE category='Fiction' ORDER BY date_added DESC"); 
    so i would like where it says 'Fiction" to grab the value of the html link from the index.php page.

    is this possible?

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    You could do this a few ways, not sure how many categories you have...

    index.php
    PHP Code:
    <a href="book_category_list.php?cat=fiction">Fiction</a>
    <
    a href="book_category_list.php?cat=Horror">Fiction</a
    book_category_list.php
    PHP Code:
    <?php
    if (!empty($_GET['cat'])) {
        switch (
    $_GET['cat']) {
            case 
    'fiction':
                
    $sql mysql_query("SELECT * FROM books WHERE category='Fiction' ORDER BY date_added DESC"); 
            break;
            case 
    'Horror':
                
    $sql mysql_query("SELECT * FROM books WHERE category='Horror' ORDER BY date_added DESC"); 
            break;
            default:
                echo 
    "Please provide a valid category";
            break;
        }
    }
    ?>
    This is untested let me know any issues you have.


    You could also do..

    PHP Code:
    <?php
    if (!empty($_GET['cat'])) {
    $cat mysql_real_escape_string($_GET['cat']);
    $sql mysql_query("SELECT * FROM books WHERE category='$cat' ORDER BY date_added DESC"); 
    }
    ?>
    Corrections to my coding/thoughts welcome.

  3. The Following User Says Thank You to bluewalrus For This Useful Post:

    faketales (10-29-2011)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    Note that if you do use a variable category, you'd want to verify that it actually exists, either by checking the category itself or confirming that there is at least one match.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #4
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    it works thank you very much
    Last edited by faketales; 10-29-2011 at 08:04 PM.

Bookmarks

Posting Permissions

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