SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Carrying over variables?

    Hi,

    I'm new to PHP and I was wondering if there was a way to carry over variables from page to page (I don't even know if I'm using the right terminology but hopefully my explanation will get the point across).

    Here's what I'm looking to do. In my DB, I have a table named artist which has 2 fields in it - artist_name, and artist_id. On my index page, I have a query that displays every artist in my DB in a list with the artist_id set as a variable (once again, not sure if I'm using the correct terminology) for each artist. I want for people to be able to click on an artists name, be taken to a url that looks similar to this - mysite.com/artists?id=2(artist_id number) which will be like a profile page for that particular artist where I can display a bio, etc. (when I add more fields to the artist table). I figured that somehow you could carry that "id=2" over into that artists.php file and use it in a query to bring back that particular artist.

    Sorry if my explanation is a little hard to comprehend, it was hard to write . Am I even approaching this situation the right way?

    Thanks in advance for the help.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Michigan
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are accessing the artists.php file the following way:

    yoursite.com/artists.php?id=2

    Then you only need to use $_GET['id'] to get the artist's ID number.

    You can then validate that and plug it into your database query.

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trhynard
    If you are accessing the artists.php file the following way:

    yoursite.com/artists.php?id=2

    Then you only need to use $_GET['id'] to get the artist's ID number.

    You can then validate that and plug it into your database query.
    Thanks for the follow-up trhynard, is this anywhere close?

    PHP Code:
    <?php

    // Perform MySQL query on only the current page number's results
    $sql mysql_query("SELECT $id FROM artist");
    $id $_GET['id'];
    $artist_id 'id';

    // Get rows from database (hint: fetch)
    while($row mysql_fetch_assoc($sql)){
        
    // Build your formatted results here.
       
    $artist_id $row['artist_id'];
       
    $artist_name $row['artist_name'];
       echo 
    '<a href="' .
           
    'artists.php?id=' $artist_id '">' $artist_name '</a><br>' .
    '';
    }

    ?>
    I did some searching (on these forums) and this is the best I could come up with. I know it's probably wayyyyyyyyy off but I have to say I gave it my best .

  4. #4
    SitePoint Evangelist kooshin.com's Avatar
    Join Date
    Dec 2003
    Location
    http://kooshin.com
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See this tutorial which deals with displaying articles with using IDs http://www.php-mysql-tutorial.com/cm...658ce24ce8f9d5 . I think it will help you a lot since it is just want you want .

    Hope that helps

    Regards;
    Kooshin


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
  •