Alternamaton Posted June 17, 2007 Share Posted June 17, 2007 I was just wondering how a PHP/MySQL-based website usually handles user profile pages. My thought is that there should only be one "profile.php", but that different values should be passed to it from the MySQL database depending on whose homepage it is. I'm just curious as to how one would go about implementing this. Say you're logged into a website such as Myspace, and you're looking at a list of user profiles. When you click on one of the user profiles, "profile.php" opens, and the data for the user whose profile you are trying to view is passed from the MySQL database to "profile.php". Then if you go back and click on a different user's profile, the same page opens, but with different data loaded from the MySQL database. Now, I already know that to log a user in, I can use the $_SESSION array and pass the user's information from the MySQL database as session variables which are then used in "homepage.php". I suppose that I could simply start the session with extra sessoin variables such as $_SESSION["profile_user"], $_SESSION["profile_user_avatar"], etc., and just leave them empty or NULL until "profile.php" is opened, and then pass the correct data to the $_SESSION array depending on whose profile was picked. I guess I understand how to do it, but my only problem is triggering the necessary PHP scripts when an HTML link is clicked. I know that I could create a page called "userlist.php" which queried the database and displayed a list of users, with a form-style SUBMIT button next to each user's name, which ran the necessary PHP scripts and then opened up "profile.php" with that user's information loaded, but I don't want to use submit buttons. And the fact that most large multi-user sites have links for user profiles makes me doubt my conception of how to most efficiently organize user pages. Here's an idea (and now I'm thinking out loud): Could it be as simple as having a list of links to the same website ("profile.php") on a page ("userlist.php" or some such) along with a script that sets session variables (i.e., $_SESSION["user_profile"], etc.) based on the string between the <*a href=> and <*/a> tags of the link that's clicked? I would still need the scripts to be triggered by the act of clicking a specific link. So I guess my question boils down to: what are the PHP functions, if any, for running scripts when a user clicks on an HTML link? And I suppose you could also respond to my whole conception of organizing and displaying user profiles if you think it's hare-brained. Link to comment https://forums.phpfreaks.com/topic/55923-general-question-about-user-homepages/ Share on other sites More sharing options...
trq Posted June 17, 2007 Share Posted June 17, 2007 The process is quite simple. Query the database to make a list of links passing along the id's of each record. eg; listall.php <?php if ($result = mysql_query("SELECT id,uname FROM users")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo "<a href=\"profile.php?id={$row['id']}\">{$row['uname']}</a><br />"; } } } ?> Then, on profile.php use the id to retrieve further information. eg; profile.php <?php if (isset($_GET['id'])) { if ($result = mysql_query("SELECT uname,data FROM users WHERE id = '{$_GET['id']}'")) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); echo "<h1>{$row['uname']}</h1><br />"; echo "<p>{$row['data']}</p>"; } } } ?> Hope this helps. Link to comment https://forums.phpfreaks.com/topic/55923-general-question-about-user-homepages/#findComment-276199 Share on other sites More sharing options...
Alternamaton Posted June 17, 2007 Author Share Posted June 17, 2007 Thank you for the help. Where in the PHP or MySQL manual would I find information about how to format URLs with regard to MySQL records? I think I understand your code but I'm not sure how you came up with the URL \"profile.php?id={$row['id']}\". Also, I don't see where you passed the user id to the $_GET variable. I see where you used it (in profile.php), but not where you actually set it. Link to comment https://forums.phpfreaks.com/topic/55923-general-question-about-user-homepages/#findComment-276205 Share on other sites More sharing options...
trq Posted June 17, 2007 Share Posted June 17, 2007 Also, I don't see where you passed the user id to the $_GET variable. I see where you used it (in profile.php), but not where you actually set it. This line... echo "<a href=\"profile.php?id={$row['id']}\">{$row['uname']}</a><br />"; sets it. For each user retrieved from the database it makes a link with there name, and passing there id via get. Link to comment https://forums.phpfreaks.com/topic/55923-general-question-about-user-homepages/#findComment-276349 Share on other sites More sharing options...
Alternamaton Posted June 17, 2007 Author Share Posted June 17, 2007 Thank you for your help, thorpe! I will go ahead and mark this as solved, but I just want to clarify one thing before I do that. Am I right in thinking that I will only need to use one $_GET variable for every user, and then use that $_GET variable to query the database and get all kinds of other variables for that user? For example, I use a single $_GET variable whenever I need to have a link to someone's profile, but then on profile.php I take that $_GET variable and make repeated queries of the database to get all of that user's other information. If so, then I think I'm well on my way, because I got listusers.php and profile.php to work perfectly. Edit: Just a minor question. In listusers.php, I formatted the link like so: echo "<a href='http://localhost/profile.php?username=" . $row['username'] . "'>" . $row['username'] . "</a><br>"; Are the curly brackets you used just another way to concatenate strings with variables? I wasn't sure about the way you did it so I just concatenated them manually (after some trial and error). I will look in the manual under strings and see if I see anything about curly brackets. I know that they are used as quantifiers in regular expressions, but I'm not sure about how you used them here. Link to comment https://forums.phpfreaks.com/topic/55923-general-question-about-user-homepages/#findComment-276460 Share on other sites More sharing options...
teng84 Posted June 18, 2007 Share Posted June 18, 2007 when u use a curly braces within your variables it like telling the php script that this are a variable with some task to do i dont know if thats the exact explanation but it goes something like that any way you can use those brace if you want and if you dont nothings change in some reading ive done it says that it make the php script faster hope that helps bro////// Link to comment https://forums.phpfreaks.com/topic/55923-general-question-about-user-homepages/#findComment-276498 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.