SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Oct 1999
    Location
    New York, NY
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    displaying data from a href link?

    Hi,

    I'm pretty new to php and have a grasp on most of the basics but can't seem to figure out what should be simple...! Let's say I have a database with Name, Gender, Location. How do I create links like "View All Males | View All Females"? I know part of the code is:
    PHP Code:
    $result mysql_query("SELECT * FROM mytable WHERE gender='Male'",$db); 
    But I'd like links to pull them up.

    Thanks.

  2. #2
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this:

    Linking page:

    PHP Code:

    <a href="getsex.php?sex=male">Males</a>
    <
    a href="getsex.php?sex=female">Females</a
    Getsex.php

    PHP Code:

    $sex 
    $_GET['sex'];

    if(isset(
    $sex)) {
         if(
    $sex == 'male'){
               
    // DB query to get male
         
    }
         if(
    $sex == 'female'){
               
    // DB query to get females
         
    }
    } else {
          echo 
    'Error: sex not set';

    Obviously this might not be the best way, just a quick way to show you

    Mark

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    San Diego, CA (USA)
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mark's method is definately the most reasonable way to do this. but, if you wanted to have the option to view both sexes at the same time, you could do this:

    PHP Code:
    // HTML links
    <a href="getsex.php?sex=male">Males</a>
    <
    a href="getsex.php?sex=female">Females</a>
    <
    a href="getsex.php?sex=both">Both</a>


    //getsex.php

    $sex $_GET['sex'];


    switch (
    $sex) {

         case 
    'male':
              
    //query lookup for males
         
    break;

         case 
    'female':
              
    //query lookup for females
         
    break;

         case 
    'both':
              
    //query lookup for both sexes
         
    break;

         default:
              
    //query lookup for whatever you want the default sex or sexes to be (only for if a sex is not specified in the link)


    just building off of mark's idea.

  4. #4
    SitePoint Guru
    Join Date
    Oct 1999
    Location
    New York, NY
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You guys are awesome, thanks a lot. This really helped explain things.


    Thanks.

  5. #5
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by altecex
    mark's method is definately the most reasonable way to do this. but, if you wanted to have the option to view both sexes at the same time, you could do this:

    PHP Code:
    // HTML links
    <a href="getsex.php?sex=male">Males</a>
    <
    a href="getsex.php?sex=female">Females</a>
    <
    a href="getsex.php?sex=both">Both</a>
     
     
    //getsex.php
     
    $sex $_GET['sex'];
     
     
    switch (
    $sex) {
     
    case 
    'male':
    //query lookup for males
    break;
     
    case 
    'female':
    //query lookup for females
    break;
     
    case 
    'both':
    //query lookup for both sexes
    break;
     
    default:
    //query lookup for whatever you want the default sex or sexes to be (only for if a sex is not specified in the link)
     

    just building off of mark's idea.
    Nice one Altecex!

    You should probably check that $sex is actually set first and if not terminate the script rather than not checking and letting it run anyway.

    Mark

  6. #6
    Non-Member St.'s Avatar
    Join Date
    Nov 2004
    Location
    Cloud #9
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [offtopic]
    .oO( <a href="getsex.php?sex=other">Other</a> ) 8)
    [/offtopic]

  7. #7
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by St.
    [offtopic]
    .oO( <a href="getsex.php?sex=other">Other</a> ) 8)
    [/offtopic]
    HAHA ...


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
  •