Twister1004 Posted October 19, 2008 Share Posted October 19, 2008 Hey everyone! I am creating a new thing on my webpage to where I have made a profile page! Now what I'm trying to do is, Objective: Allow Logged in people to view other people's profile. However, I don't know how i would go about that. As you can tell in my sig, I'm not a very advanced coder. I'm basically newborn. Could someone explain to me and also help me with this? O_o. I've all ways like to progress in what I do, and this to me seems like a good goal =D. If any code is needed at all, I will provide it. Any hints or tips is accepted =D Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 19, 2008 Share Posted October 19, 2008 Here's how things like this are usually approached: [*]You pass the ID of the user whose profile you want to view in the URL (this is usually done automatically through a link generated in your script). [*]The script then takes the information in $_GET['id'] and uses it in the query to get a specific person's data. [*]The data is displayed. Tell me if you need any elaboration. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 20, 2008 Author Share Posted October 20, 2008 Ok, but how about being able to just type in the user's ID into the link as well? Would that be possible, or would you have to just let the script run while you click on the profile? My next question is, would it just be easier to have everyone's name then link the name to their page? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 20, 2008 Share Posted October 20, 2008 You can always manually change the link and put in the user's ID, yeah. And user lists are good too, because you can link their name to their profile with their ID. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 20, 2008 Author Share Posted October 20, 2008 curious, would you be able to show me an example. I never used the $_GET command before. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 20, 2008 Share Posted October 20, 2008 $_GET isn't a function, it's actually a variable that PHP automatically populates with parameters in the URL. Let's say you had profile.php and you also had a userlist page that would give links like profile.php?id=12 to see people's profiles. Then, on profile.php, you could do like... <?php //pretend we're connected to mysql and everything $id = (int) $_GET['id']; if (!($id > 0)) { $id = $_SESSION['user_id']; //display their own profile if they decide to mess with the query string too much } $query = "SELECT * FROM users WHERE id=$id"; $result = mysql_query($query) or die(mysql_error()); //display results however you want That's just a short example, but hopefully you get the idea. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 20, 2008 Author Share Posted October 20, 2008 I get the idea, its just not making since to me right now. If you saw how i did my Geometry work, you would see how and why, lol. I showed everyone how i did it they were like "Omg dont show me that! Wth is that!" So, yeah lol. Grr, I really hate asking this, just because so many people have asked me and stuff, But would you mind if i add you to MSN? It would probably help me more and faster if it was an IM. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 20, 2008 Share Posted October 20, 2008 Go for it. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 20, 2008 Author Share Posted October 20, 2008 Umm, The code makes since. It just doesnt make since to my brain. o.o. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 20, 2008 Author Share Posted October 20, 2008 Ok, so say like i didn't make a userlist page. Ummm, let me see if i can get this... <?php //Mysql is connected $id = (int) $_GET['id']; //the ID is a interger. This is what i dont get. where will it get the ID from? if (!($id > 0)) {//if the ID is above 0 (Or 1 meaning the profile exists) $id = $_SESSION['user_id']; //display their own profile if they decide to mess with the query string too much //what? how would they do that? lol } $query = "SELECT * FROM users WHERE id=$id";//selects the ID of the user from the database from $id $result = mysql_query($query) or die(mysql_error()); //Goes to result as a query or it dies if not successful. //code of the profile is setup by arrays and SQL Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted October 20, 2008 Share Posted October 20, 2008 @twister: You almost got it... <?php //Mysql is connected $id = (int) $_GET['id']; //the ID is a integer. This is what i dont get. where will it get the ID from? if (!($id > 0)) {//if the ID is above 0 (Or 1 meaning the profile exists) $id = $_SESSION['user_id']; //display their own profile if they decide to mess with the query string too much //what? how would they do that? lol } $query = "SELECT * FROM users WHERE id=$id";//selects the ID of the user from the database from $id $result = mysql_query($query) or die(mysql_error()); //Goes to result as a query or it dies if not successful. //code of the profile is setup by arrays and SQL The integer comes from the URL (that's what $_GET does...) Little example: let's say I have an URL like: www.example.com/index.php?profile=1&user=2 You can see that I use 2 variables in the URL: profile and user <?php echo $_GET['profile']; echo $_GET['id']; //Would output: 12 ?> The if(!$id > 0) doesn't mean that the profile exists...Usually when working with databases, the id is aan auto increment value..Meaning that the database automatically assigns the value. This means that the value can't be 0, because this isn't used...So id 0 will always be a wrong number... However, you should still check if the profile exists.... Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 20, 2008 Author Share Posted October 20, 2008 Ok, so basically how i set up the webpage for the profiles, i have a pages that is speciflly for the login details and variables. So, umm.... I guess the main part i was confused about, is how would i be able to just get a specific profile without having to put a page just to get on the person's profile? Maybe I'm just confusing myself. I understand the $_GET now. Ummmm, god i'm just confused about how to get to their profiles without giving a page with all the users and linking it to there. ._. Could you possiably explain that? I believe once I understand that, the rest will just fit in. Until then, I will go play with the codes! (God help me, lol) Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 20, 2008 Share Posted October 20, 2008 Have a profile.php that dynamically takes the user id out of the URL and then generates the profile page. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 20, 2008 Author Share Posted October 20, 2008 I have a profile page named cp.php. Do i need to post the $_GET at the top of the page before it loads? (I honestly think there is no help for me, lol) ugh, Ill see what I can but I dont know how you would take someone out the the URL o.o Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 21, 2008 Author Share Posted October 21, 2008 OMg, i did something on accident and i got the $_GET command to work! However it was an accident. It was in a form. So, now i figured that out, can i just tell it to $_GET the variables anywhere? Or does it have to be in just once place? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 21, 2008 Share Posted October 21, 2008 They can be used anywhere, and you don't need a form for GET. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted October 21, 2008 Share Posted October 21, 2008 $_GET is a superglobal (i.e., always accessible) variable that contains the values of the query string passed to the script. What is a query string? Have you ever seen a URI with a structure along the lines of: http://www.somesite.com/contact.php?id=2&post=438 The part after the '?' is the query string. So, let's look at this fictional contact.php script. In order to get to the information in the query string, you simply have to use the $_GET array, like so: $myId = $_GET['id']; $myPost = $_GET['post']; Like DarkWater said, no form is necessary in order to use $_GET. What is necessary is a query string. The reason it works with a form is that when a form is created with its method attribute set to get, the info submitted by the form is sent as a query string to the script that handles the form. In other words, say you have a simple form like: <form name="myForm" action="handleForm.php" method="get"> <input name="name" type="text" value="bubba" /><br /> <input name="email" type="text" value="test" /><br /> <input name="submit" type="submit" value="submit" /> </form> When that form is submitted, the info is passed along as a query string in the URI: http://www.somesite.com/handleForm.php?name=bubba&email=test PHP also has a $_POST array designed to handle form data sent via the post method. The main difference between post and get is that info passed along using post is done behind the scenes rather than being visible in the URI. Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted October 21, 2008 Share Posted October 21, 2008 What most designers usually do is get the variable at the top of the page, sanitize it, and store it... Like: <?php $intProfileId = mysql_real_escape_string($_GET['profile']); ?> [code] Now $intProfileId has the id that was in the url, but it's also sanitized using mysql_real_escape_string so you can check it in a database... Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 21, 2008 Author Share Posted October 21, 2008 I now understand it somewhat better now! But my only problem is I just can't seem to get it to call correctly. I'm getting notices (I'm using error_reporting(E_ALL) ). saying there is no index for it. (Yes i changed inside the "[]") Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 21, 2008 Author Share Posted October 21, 2008 This is how my profile is setup right now. The header.php has all the Sessions and login information within it. However, I can't seem to get the $_GET working correctly. I'm getting notices or errors. He is my Profile page: Now with that all said, how would i be able to call a $_GET since It doesnt like me, lol. <?php session_start(); ?> <?php error_reporting(E_ALL);?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link href="style.css" rel="stylesheet" type="text/css" /> <style> body,td,th { font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #FFFFFF; } body { background-color: #000000; background-image: url(images/bg-soft.PNG); background-repeat: repeat; } #loggedin_info { position:absolute; left:159px; top:100px; width:218px; height:400px; z-index:1; margin:auto; padding:0px; } #header{ position:absolute; background:#000000; top:2px; left:100px; } #notloggedin{ position:absolute; left: 405px; top: 198px; width: 444px; height: 32px; } #content_loggedin{ position:absolute; top:99px; left:381px; width: 528px; height: 399px; } #side_rename{ position:absolute; top:99px; left:920px; width: 189px; height: 401px; } #nav{ position:absolute; left:4px; top:102px; width:145px; height:388px; color:white; background:#4682B4; } #button_account{ position:absolute; top:473px; left:209px; width: 150px; } #content_account{ position:absolute; top:472px; left:586px; } #unknown_account{ position:absolute; top:474px; left:955px; } </style> </head> <body> <div id = login> <?php include('header.php'); $result = mysql_query("SELECT about,content,unknown FROM `profile` WHERE `accountid` = {$_SESSION['login_id']}"); if (!$result) { echo 'Could not run query: ' . mysql_error(); exit(); } $row = mysql_fetch_row($result); ?> </div> <?php if (isset($_SESSION['login_user'])){ ?> <div id="loggedin_info"> <p align = center> <br/><br/> About me, <br/><?php echo htmlentities($row[0]); ?></p> </div> <div id = content_loggedin> <p align = center>About your self,<br/> <?php echo htmlentities($row[1]); ?></p> </div> <div id = side_rename> <p align = center>Personal Use <br/><?php echo htmlentities($row[2]); ?></p> </div> <?php } else{ ?> <div id = notloggedin> <center> <strong>You must be loggedin to view your profile!</strong><br/> The profiles is currently, not done, or functional. Please wait while we get this online, ASAP!<br/> Thank you!</center></div> <?php } ?> <div id = nav> <center> <hr /> <a href = "./index.php">Home</a><br/><hr /> <a href = "./faq.php">FAQ</a><br/><hr /> <a href = "./about.php">About Us</a><br/><hr /> <a href = "./index.php">Projects</a><br/><hr /> </center> </div> </body> </html> I know this is a double post, but i can't edit O_o. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 22, 2008 Author Share Posted October 22, 2008 Hummmm.... maybe I'm still a blond, or I get it but don't understand how to use it X_x. I guess my best luck is to research it. Thanks you all of your help everyone! Quote Link to comment Share on other sites More sharing options...
keeps21 Posted October 22, 2008 Share Posted October 22, 2008 Say you have a page with a list of users and by clicking on their name you can view their profile. This is how you could do it with $_GET users.php - lists users <html><head><title>Profile list</title></head> <body> <a href="view_profile.php?profile_id=1" > Profile of user 1</a> <a href="view_profile.php?profile_id=2" > Profile of user 2</a> <a href="view_profile.php?profile_id=3" > Profile of user 3</a> </body> </html> Notice the links, they have ?profile_id=1 and so on. When you click on that link you will be taken to a page called view_profile.php with the profile_id variable appended onto the end of the url. In this next page, to show the profile you need to 'get' that variable from the url. view_profile.php - view the profile of selected user. <?php // Get profile ID to show $profile_id = $_GET['profile_id']; // No error checking is done here so do not use this code as is. // you'll need to change the values here to suit your db $query = "SELECT profile_id, name, email FROM users WHERE profile_id = $profile_id"; $result = mysql_query($query) or die ("There was an error"); $row = mysql_fetch_row($result); ?> <html><head><title>Profile of User <?php echo $profile_id; ?></title></head> <body> <h1>Profile - <?php echo $row['id'] . ' - ' . $row['name'] ;?> </h1> Email - <?php echo $row['email'] ;?> </body> </html> You need to change the $row[''] to suit the db field value you want to show. $row['name'] shows data from the name column in your db $row['email'] shows data from the email column in your db I hope this clears things up a little for you. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted October 22, 2008 Author Share Posted October 22, 2008 oooh! oooh! ooh~~~ So my next question is, does the variables and the stuff after the "?" in the link have to be there before I use the $_GET? Or is it supposed to be there when the $_GET is used? Quote Link to comment Share on other sites More sharing options...
keeps21 Posted October 23, 2008 Share Posted October 23, 2008 The $_GET needs to appear on the page which needs the information from the url. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.