id link in the URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

id link in the URL

Post by mikes1471 »

Hi Everyone

I have this page currently displaying various information of users within a database:

Code: Select all

<?php
 
include('connect.php');
include('design/header.php');
 
 
//max displayed per page
$per_page = 36;
 
//get start variable
$start = $_GET['start'];
 
//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM users"));
 
$query_result = mysql_query($sql);
 
$display_per_row = 4; //amount you need to display per row
$x = 0;
 
//count max pages
$max_pages = $record_count / $per_page; //may come out as decimal
 
if (!$start)
   $start = 0;
 
$sql = "SELECT * FROM users WHERE gender='male' OR gender='female'";
$query_result = mysql_query($sql); 
 
$display_per_row = 4; //amount you need to display per row
$x = 0;
 
echo "<table border='1' align='center' width='100%'>";
// loop through users
while ($row = mysql_fetch_assoc($query_result))
{
    $x++;
    
    if ($x == 1) {
      echo "<tr height='10'></tr><tr>";
    }
    
    $username = $row['username'];
    $yob = $row['yob'];
    $mob = $row['mob'];
    $dob = $row['dob'];
    $age = getage($yob, $mob, $dob);
    $location = $row['location'];
    $gender = $row['gender'];
    $status = $row['status'];
    $city = $row['city'];
 
    echo "
    <td align='center' width='120'><img src=store/$location width='120' border='0'></td><td>
    <b><font face='arial' size='3'>
    $username<br>
    <font color='red'>Age:</font> $age<br>
    <font color='red'>Gender:</font> $gender<br>
    <font color='red'>Status:</font> $status<br><br>
    $city<br>
    <a href='messageuser.php'><font color='green'>Message me!</font></a>
    </b>
    </td>";
    
    if ($x == $display_per_row) {
      echo "</tr>";
      $x=0;
    }
 
}
echo '</table>';
 
function getage($year,$month,$day) {
 
    $cyear = date('Y');
    $cmon = date('m');
    $cday = date('d');
    
    $age = $cyear - $year;
    
    if($cmon <= $month && $cday <= $day) {
        $age--;
    }
     
     return $age;
 
} 
 
//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;
 
//show prev button
if (!($start<=0))
       echo "<a href='index.php?start=$prev'>Prev</a> ";
 
//show page numbers
 
//set variable for first page
$i=1;
 
for ($x=0;$x<$record_count;$x=$x+$per_page)
{
if ($start!=$x)
    echo " <a href='index.php?start=$x'>$i</a> ";
else
    echo " <a href='index.php?start=$x'><b>$i</b></a> ";
$i++;
}
 
//show next button
if (!($start>=$record_count-$per_page))
       echo " <a href='index.php?start=$next'>Next</a>";
 
 
?>
And currently theres no way to allow a visitor to click a link and go to the users profile page - profile.php?id=7 for example so I wonder how I could define this somewhere in my code?

Ive got as far as finding this code which does exactly that:

Code: Select all

<?php
// connect to database
mysql_connect("localhost", "root", "") or die("Connection Error");
mysql_select_db("my_tuts");
 
// declare variables
$members = array();
$error_feedback = "";
 
// create query and query database
$sql = "SELECT `username`, `id` FROM `users`";
$query_result = mysql_query($sql) or die("Query Error");
 
// check there is a result
if($query_result != FALSE && mysql_num_rows($query_result) > 0) {
       
  // loop through results
  while($member = mysql_fetch_assoc($query_result)) {
               
    // store the member details in an array for later use
    array_push($members, $member);
  }
} else {
  // create an error message
  $error_feedback = "No members found.";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title><?php echo $title; ?></title>
</head>
<body>
  <h1>Our Members</h1>
  <?php
    foreach($members as $member) {
      echo "<a href=\"profile.php?id=" . $member['id'] . "\">" . $member['username'] . "</a><br />";
    }
  ?>
</body>
</html>
Which is great but I cant seem to integrate this code with mine or visa versa, can anyone help with this please?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: id link in the URL

Post by superdezign »

The only place in your code that has access to any individual user's ID is in your loop that displays them. So, in that loop, display the link, just like your "Message Me" link. Although, I have no idea how your messaging system works without passing the user ID to the messaging script, anyway.
Post Reply