Jump to content

Multi-User Profile Pages

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
matrob

matrob

    Newbie

  • Members
  • PipPip
  • 12 posts
I currently have a site that allows users to log and view personal workout information. When the user logs in, they are directed to their profile page. Currently, users have zero access to information by other users, so when they are directed to .../profile.php, the page is built using information from $_SESSION['user']. However, I am trying to change my site to allow users to view limited profiles of other users, with the eventual ability to add friends, send messages, etc., much like most forums or social networking sites. I see the url's in these sites being something similar to .../profile.php?user=381752 with the number being that user's user_id. This also seems similar to the php $_GET function, but as of yet I have not been able to figure out how to direct the user to the desired page... Any help would be appreciated.

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
$_GET is not a function, but a global array containing all parameters sent through the URL. so if you have as your example, $_GET['user'] would contain the number 381752. then just query your database to show that user's info instead. something which could look like this:

// try if a user number is provided by URL

if ($_GET['user'] != "") {

  // If so, query that user and present limited info

  $result = mysql_query("SELECT * FROM users WHERE id = '"

    .mysql_real_escape_string($_GET['user'])."'");

  $limited = true;

} else {

// If not, show the users own profile...

  $result = mysql_query("SELECT * FROM users WHERE id = '"

    .mysql_real_escape_string($_SESSION['user'])."'");

 $limited = false;

}

// check if a user with that ID was found

if ($row = mysql_fetch_array($result) {

  // it was, output whatever

  echo $row['username']."s profile";

  echo $row['username']." is ".$row['age']." years old";

  if (!$limited) {

  // this is only on users own profile

    echo "account is valid until ".$row['paid-until'];

  }

} else {

  // no user with that id was found

  echo "No such user was found";

}


__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
matrob

matrob

    Newbie

  • Members
  • PipPip
  • 12 posts
Thank you. I think what I'm confused about is how to reach the user profile page. I know I could have a textbox in a form with a method="get" and use $_GET['textbox name'] but I don't know how else to do it. Obviously, I'm new to php.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
$_GET is purely accessable through the URL sent to the PHP script.

profile.php?user=381752 will fill the variable $_GET['user'] with the value 381752 in the PHP script. You can then use Orjan's example and access a user's profile if the $_GET is set. If not then display their own profile.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
you can easily also put an url as a normal link using get... like this:
<a href="index.php?user=381752">Link to user's profile</a>
which alsoc of course could be generated by php: (assuming the user's db info is already fetched in $row)
echo '<a href="index.php?user='.$row['id'].'">'.$row['name'].'</a>';

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
matrob

matrob

    Newbie

  • Members
  • PipPip
  • 12 posts
Ah got it working now. Thank you both!