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.
$_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:
Code:// 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
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.
$_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 10,000 different steps.
A textual description can be only part of your question, be sure to provide sample results, errors and your platform in the appropriate forums while asking.
you can easily also put an url as a normal link using get... like this:
which alsoc of course could be generated by php: (assuming the user's db info is already fetched in $row)HTML Code:<a href="index.php?user=381752">Link to user's profile</a>
Code:echo '<a href="index.php?user='.$row['id'].'">'.$row['name'].'</a>';
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
Ah got it working now. Thank you both!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks