How to get PHP to open a link that changes?

Discussion in 'PHP' started by Ipodman79, Jan 28, 2014.

  1. #1
    So say I have a list,

    Pet ID;

    1. Jimmy
    2. Alvin
    3. Spot
    4. Snowy
    5. Bob

    How do I make PHP know the id of the pet and then open that file. So if the user clicked on the number 1 it would open that profile. NOTE: These numbers can change so doing it with HTML isn't an option, I can't do <a href="includes/profile_1">1. Jimmy</a>

    How do I do this?
     
    Ipodman79, Jan 28, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    where do you get the IDs from? They must come from somewhere? Just use the id-variable in the link?
    Or, better, rethink your project, and rethink the way you pull information.
     
    PoPSiCLe, Jan 28, 2014 IP
  3. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    Ok, sorry. I should have gone into more depth.

    Basically I'm displaying information that I have in a database. I want to be able to allow the user to click on the id number and then it'll open a page displaying the pets info.

    So, I'd have a database of pet's. I'd then display them on a page. If a user wants to click on a pet if it would bring up a profile page about the pet.

    Does that explain it better?
     
    Ipodman79, Jan 28, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Yes, that was sort of what I figured - but I'm failing to see how the IDs will change?
    What you need to do is pull the information from the database for the pets in question - whether or not that's the user's pets, or just a collection of all pets, or whatever. These pets will have a unique ID (or, at least, they should), in the database - just use that in the link? Something like this:
    
    //assuming that you're pulling the info and running this with a while-loop
    echo '<ul>';
    while ($row = $stmt->fetch()) {
        $id = $row['id'];
        $petname = $row['petname'];
    
    echo '<li><a href="includes/profile_'.$id.'">'.$petname.'</a></li>';
    }
    echo '</ul>';
    
    Code (markup):
    That should do exactly what you want, really.
     
    PoPSiCLe, Jan 28, 2014 IP
  5. Murugesan.quadraincorp

    Murugesan.quadraincorp Greenhorn

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    23
    #5
    Hi.
    This code dn't have database. Only pass Id and get your Page... try this code

    <a href="index.php?id=1"/>1. Jimmy</a>
    <?php
    if($_GET['id']==1)
    {
    ?>
    <a href="includes/profile_1"/>Jimmy</a>
    <?php
    }
    ?>
     
    Murugesan.quadraincorp, Jan 28, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Jeez... How about:
    
    <a href="index.php?page=1">1. Jimmy</a>
    <?php
      if (isset($_GET['page'])) {
         include ('includes/profile_'.$_GET['page'].'');
      }
    ?>
    
    PHP:
    which would actually GET the content of the file, instead of printing another link to something ...
     
    PoPSiCLe, Jan 28, 2014 IP