New data on same page!?!?

Discussion in 'PHP' started by jmansa, Dec 4, 2005.

  1. #1
    I have a DB table with info about persons. To get the info on my page I use following script:

    
    <?php
    
    $dbhost = 'my_host';
    $dbuser = 'my_username';
    $dbpass = 'my_password';
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
    
    $dbname = 'my_db';
    mysql_select_db($dbname);
    
    $query="SELECT * FROM my_table WHERE id=1";
    $result=mysql_query($query);
    
    $num=mysql_numrows($result);
    
    mysql_close();
    
    
    $i=0;
    while ($i < $num) {
    
    $name=mysql_result($result,$i,"name");
    $adress=mysql_result($result,$i,"adress");
    $zipcode=mysql_result($result,$i,"zipcode");
    $country=mysql_result($result,$i,"country");
    $email=mysql_result($result,$i,"email");
    ?>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><a href="info1">name1</a></td>
        <td><a href="info2">name2</a></td>
        <td><a href="info3">name3</a></td>
        <td><a href="info4">name4</a></td>
        <td><a href="info5">name5</a></td>
      </tr>
    </table>
    
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><?php echo "<b>$name</b>";?></td>
      </tr>
      <tr>
        <td><?php echo "<b>$adress</b>";?></td>
      </tr>
      <tr>
        <td><?php echo "<b>$zipcode</b>";?></td>
      </tr>
      <tr>
        <td><?php echo "<b>$country</b>";?></td>
      </tr>
      <tr>
        <td><?php echo "<b>$email</b>";?></td>
      </tr>
    </table>
    <?php
    $i++;}
    ?>
    
    Code (markup):
    My question is: Instead of creating a new page for every person i got in my DB, is it possible to get the same info on just one page. So if I hit name 2, the page refreshes, but with the info for person 2 (ID2)? Any help is wanted!!!

    //jmansa
     
    jmansa, Dec 4, 2005 IP
  2. Pange

    Pange Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You definitely can get all the information you need from one PHP script instead of making a new one for each user. What you could do is find out what person you want information about dynamically through the URL. (ex: page.php?user=1)

    Before your $query line, find out from the URL what user you want information about using the following line of code:

    $userid = $_GET["user"];
    
    if(empty($userid)) { $userid = "1"; } // defaults to 1 if nothing in url indicates number
    Code (markup):
    Change your $query line from what you have to the following:

    $query="SELECT * FROM my_table WHERE id ='$userid'";
    Code (markup):
    By changing those lines, you can view your php page in your webbrowser as "page.php?user=5", and the script would lookup user number 5, or if you change the number, a different user would appear.

    Then, you would just have to change the urls in the table to reflect the new ID structure:

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><a href="page.php?user=1">name1</a></td>
        <td><a href="page.php?user=2">name2</a></td>
        <td><a href="page.php?user=3">name3</a></td>
        <td><a href="page.php?user=4">name4</a></td>
        <td><a href="page.php?user=5">name5</a></td>
      </tr>
    </table>
    Code (markup):
    If you were feeling adventurous, you could using a query to find out how many users are in the table, and automatically list out the names, that way you wouldn't have to do it manually. It would save you a lot of time in the long run. =)
     
    Pange, Dec 5, 2005 IP