Log in

View Full Version : Resolved Variable in the URL



X96 Web Design
04-21-2009, 03:02 PM
Hi All,

I'm trying to make a members page, which has personalized content, depending on what's in the URL: members.php?username_here

Just a simple IF statement would suffice...

Thanks,
X96

Schmoopy
04-21-2009, 10:32 PM
This won't be a simple IF statment I don't think. Unless you're planning on only having 2 - 3 members. You'll need to query a database to find if said member ID exists, then display relevant data depending on the records returned from the query...

If however you do only plan to have a couple of members then it's pretty simple:



if(isset($_GET['member'])) {

if($_GET['member'] == 1) {
// All relevant code for this member here, example:

echo "<h1>Welcome to {$membername}'s page!</h1>";
}

elseif($_GET['member'] == 2) {
// All relevant code for this member
}

// And so on...

else {
echo "Member does not exist.";
}
}


This will be static, and mean that if you want to update the information on a members page you'll need to go and hardcode it in again. =/

Sorry about the formatting, you know how it is when trying to indent online >.<

X96 Web Design
04-21-2009, 11:03 PM
I have a working registration page and log in set up, so all the database stuff is there, I just need to pull in different content from different tables depending on what the $_GET is set to...

Something like in Wordpress, where it has /index.php?page_id=53 and it displays a page depending on the page_id variable.

Sorry if I'm not making it very clear...

Thanks,
X96

Schmoopy
04-21-2009, 11:24 PM
Oh I see, you do have a database...

Well then it would be a bit like this:



<?php
if(isset($_GET['member'])) {

$member = mysql_real_escape_string($_GET['member']); // Make safe for query

// I'm going to assume you have a table called members

$memberq = mysql_query("SELECT * FROM members WHERE `id` = '$member'");

if(mysql_num_rows($memberq)) { // If any rows are returned

$row = mysql_fetch_row($memberq); // Since Id will be / is a primary key, only one record will be returned
/* So we use mysql_fetch_row as we don't need a while loop to go through multiple records
Only drawback is that the row comes back as a numerical array so we have to use [0] to address columns
We'll say the setup is like this:

[0] = id (Unique identifier)
[1] = name
[2] = bio (biography)
[3] = location
[4] = email

Obviously these are just test columns and you can add different ones if you want
I just don't know what data you'll want on the member's page
Break out of PHP to output HTML
*/
?>

<div id="memberheader"><?php echo $row[1] . '\'s profile.'; ?></div>
<div id="memberinfo">
<h3>About <?php echo $row[1]; ?></h3>
Biography: <?php echo $row[2]; ?>
<p>Location: <?php echo $row[3]; ?></p>
<p>Email: <?php echo $row[4]; ?></p>
</div>


<?php
}

else {
echo "The member you're looking for doesn't exist.";
}

}
?>


That's the structure of what I think you're looking for, of course you can build on this with more advanced functions and you'll need to make it look presentable :p

Hope this helps you out though.

X96 Web Design
04-22-2009, 02:16 AM
Thank you so much Schmoopy! It works exactly like I wanted it to! I've been trying to make something like that for hours - I'm not that great at writing PHP yet...

Cheers,
X96 WD

Schmoopy
04-22-2009, 02:20 AM
No problem, now you've got the structure there it's pretty simple to add more columns, so have fun with it :D