PHP in a link?

Forum
Last Post
Threads / Messages

Saphira

Member
Member
Joined
Jan 8, 2009
Messages
89
Points
0
Mysidian Dollar
3,860
Okay, I've created a new user profile page for the script, but I'm having some issues linking it up. How can I create a link, where half of it depends on the users ID? For example, www.yourdomain.com/viewprofile.php?id=1 will direct you to the users page who has the user ID of 1. But I have no idea how to set this up, so that if a user clicks a profile link, it will echo their ID from the database into the ?id=* bit.

I hope this makes sense, because it's a lot harder to explain than I thought it'd be. Heh.

Cheers!
 
I am assuming you have another application that you are trying to integrate into my script somehow?

If you know the username of a user you can easily find the ID and vise versa, assuming both scripts are running on a shared database. So, say you know the username of a user and you want to get their ID number, which is what I assume you want to do.

You would do:

PHP:
$username = "Username Goes Here";
$username = mysql_real_escape_string($username); //Prevent against SQL injection

//SQL query to get the ID number based on a username

$query = "SELECT * FROM users WHERE username = '".$username."'";
$result = mysql_query($query);
$num = mysql_numrows($result);

//Loop out code
$i=0;
while ($i < 1) {

$uid=@mysql_result($result,$i,"uid");


$i++;
}

//Now we have the user's ID number, so we make a link...
echo "<a href='viewprofile.php?id=".$uid."'>Click Here</a>";

I hope this helps. If this isn't what you need please give me more info / examples of what you want to do and I'll try and help as best I can.

Brandon
 
Not quite lol.

Here is the page that I have created:

PHP:
<?php

// Easy Adoptables Script by Brandon Rusnak
// Get our includes out of the way

include("inc/functions.php");
include("inc/levels.php");
include("inc/config.php");
include("inc/settings.php");
include("inc/nbbc.php");  // BBCODE Parser
$bbcode = new BBCode;

// Connect to our database

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql database!');
mysql_select_db($dbname);


//Set up our login info...
$username = "";
$password = "";

//Check for cookie

if (isset($_COOKIE['adoptu']) and isset($_COOKIE['adoptp'])){

$username = $_COOKIE['adoptu'];
$password = $_COOKIE['adoptp'];

$username = preg_replace("/[^a-zA-Z0-9\\040.]/", "", $username);
$password = preg_replace("/[^a-zA-Z0-9s]/", "", $password);

//Run login operation
$query = "SELECT * FROM users WHERE username = '$username'";
$result = mysql_query($query);
$num = mysql_numrows($result);

//Loop out code
$i=0;
while ($i < 1) {

$luser=@mysql_result($result,$i,"username");
$lpass=@mysql_result($result,$i,"password");

$i++;
}

	if($username == $luser and $password == $lpass){
		$isloggedin = "yes";
	}
	else{
	if (isset($_COOKIE['adoptu'])){
	$past = time() - 10; 
	setcookie("adoptu",$username,$past);
	}

	if (isset($_COOKIE['adoptp'])){
	$past = time() - 10; 
	setcookie("adoptp",$password,$past);
	}
	$isloggedin = "no";
	}

}
else
{
//User is not logged in
$isloggedin = "no";

} 

//Begin our content for the page
$id = $_GET["id"];
$id = preg_replace("/[^a-zA-Z0-9s]/", "", $id);


//Begin the level up procedure

if($id != ""){
//Adoptable ID is not empty, continue...
	//Check that the adoptable does in fact exist...
	$query = "SELECT * FROM users WHERE uid = '$id'";
	$result = @mysql_query($query);
	$num = @mysql_numrows($result);

	//Loop out code
	$i=0;
	while ($i < 1) {

	$uname=@mysql_result($result,$i,"username");
	$imageurl=@mysql_result($result,$i,"imageurl");
	$realname=@mysql_result($result,$i,"realname");
        $email=@mysql_result($result,$i,"email");
        $location=@mysql_result($result,$i,"location");
        $aim=@mysql_result($result,$i,"aim");
        $msn=@mysql_result($result,$i,"msn");
        $birthday=@mysql_result($result,$i,"birthday");



	$i++;
	}
	if($uname != ""){
	$article_title = "Profile of ".$uname.".";
	$article_date = date('Y-m-d');
	$article_content = "<img src='".$imageurl."' border='0'><br><br>
	<b>Real Name: </b>".$realname."<br>
        <b>Email: </b>".$email."<br>
	<b>Location: </b>".$location."<br>
	<b>AIM: </b> ".$aim."<br>
        <b>MSN: </b> ".$msn."<br>
        <b>Birthday: </b> ".$birthday."<br>

	Like the sound of ".$username."?  <b><a href='addfriend.php?id=".$id."'>Click here to add them to your friend's list!</a></b>";
	}
	else {
	$article_title = "Invalid Adoptable ID";
	$article_date = date('Y-m-d');
	$article_content = "The adoptable ID specified does not exist in our system.  If you would like your own adoptable you can
	<a href='adopt.php'>get one free here</a>.";
	}


}
else{
$article_title = "No Adoptable Specified";
$article_date = date('Y-m-d');
$article_content = "You did not specify an adoptable ID to show.  If you would like your own adoptable you can
<a href='adopt.php'>get one free here</a>.";
	

}



// Define our Template File

$file = $current_theme;

$article_title = stripslashes($article_title);
$article_content = stripslashes($article_content);



// Should we show the extra pages in the nav bar?

$link1 = "";
if($show_extra_page1 == "yes"){
$link1 = "<li><a href='$extra_page1_link'>$extra_page1_name<span class='tab-l'></span><span class='tab-r'></span></a></li>"; 
}

$link2 = "";
if($show_extra_page2 == "yes"){
$link2 = "<li><a href='$extra_page2_link'>$extra_page2_name<span class='tab-l'></span><span class='tab-r'></span></a></li>"; 
}

// Do the template changes and echo the ready template

$template = file_get_contents($file);
$template = replace(':SITETITLE:',$site_title,$template);
$template = replace(':SITENAME:',$site_name,$template);
$template = replace(':ARTICLETITLE:',$article_title,$template);
$template = replace(':ARTICLEDATE:',$article_date,$template);
$template = replace(':ARTICLECONTENT:',$article_content,$template);
$template = replace(':LINK1:',$link1,$template);
$template = replace(':LINK2:',$link2,$template);
$template = replace(':LINK3:',$link3,$template);

//Get the featured adoptable...
$featured = getfeatured();
$template = replace(':FEATURED:',$featured,$template);

//Ad Management
$header = @file_get_contents("ads/header.txt");
$footer = @file_get_contents("ads/footer.txt");
$tower = @file_get_contents("ads/tower.txt");

$header = stripslashes($header);
$footer = stripslashes($footer);
$tower = stripslashes($tower);

$template = replace(':HEADERAD:',$header,$template);
$template = replace(':FOOTERAD:',$footer,$template);
$template = replace(':TOWERAD:',$tower,$template);


//Is the user logged in?
//$isloggedin = "no";
if ($isloggedin == "yes"){
$template = replace(':WELCOMEORREGISTER:','<u>Welcome Back:</u>',$template);
$template = replace(':LOGINORACCT:', 'Welcome back '.$username.'.  <br><br><a href="logout.php">Log Out</a>' ,$template);
}
else{

//User is not logged in
$template = replace(':WELCOMEORREGISTER:','<u>Member Login:</u>',$template);
$loginform = "<form name='form1' method='post' action='login.php'>
  <p>Username: 
    <input name='username' type='text' id='username'>
</p>
  <p>Password: 
    <input name='password' type='password' id='password'>
</p>
  <p>
    <input type='submit' name='Submit' value='Submit'>
  </p>
  <p>Don't have an account?<br>
  <a href='register.php'>Register Free</a>  </p>
</form>
";
$template = replace(':LOGINORACCT:', $loginform ,$template);
}

echo $template;
?>

As you can tell, I've just modified one of your pages (well, started to mod), to create a more detailed profile page. The only thing that I'm having trouble with is linking to this page. I want to replace the current account page with this one. So the "My Account" link, will link to this page. I'm just not sure how to go about putting php in a link to add the users ID onto the end of the link.

I'm definitely donating to help support this project when I get paid a week tomorrow. Thanks Brandon!
 
Well, I'm a bit confused now since you seem to be saying you want to replace the My Account page with the page above. The My Account page is for editing user-specific settings whereas the page you have here appears to be used to add a user to the friends list of another user.

I think I know what you want to do though and I think I have a better idea of what you are saying. I think what you are saying is when you have an adoptable shown somewhere on the site, how can you have the owner of that adoptable linked in to this page to add another user as a friend? Right?

Take this example: Say we have a page like this one where you have the adoptable and the owner's name. Now I am assuming that you want to link the owner's name or ID number to the page you have created, right?

Well, in the file show.php we would find:

PHP:
if($name != ""){
	$article_title = "Profile of ".$name.".";
	$article_date = date('Y-m-d');
	$article_content = "<img src='".$imageurl."' border='0'><br><br>
	<b>".$name."'s Level: </b>".$currentlevel."<br>
	<b>Total Clicks: </b>".$totalclicks."<br>
	<b>".$name." is owned by:</b> ".$owner."<br><br>
	Like ".$name."?  <b><a href='levelup.php?id=".$id."'>Click Here to level them up!</a></b>";
	}

That code outputs the user's adoptable so anyone can view it. Now, for the line:

PHP:
<b>".$name." is owned by:</b> ".$owner."<br><br>

Change To:

PHP:
<b>".$name." is owned by:</b> <a href='yourpage.php?id=".$owner."'>".$owner."</a><br><br>

That makes a clickable link to the page you created. When a user clicks on the user's name then it will show their profile page that you created, well almost...

In your file, change:

PHP:
 $query = "SELECT * FROM users WHERE uid = '$id'";

to

PHP:
 $query = "SELECT * FROM users WHERE username = '$id'";

You could add an extra step in the show.php to convert the username to an ID number, but I don't think this is necessary.

Really, how you link a user's username to your show profile page depends on the file you want to link from. I hope this has helped some and feel free to let me know if you have any questions.

Brandon
 
Hmm, I'm not trying to change anything in my file. I want to change the <a href=account.php> My Account </a> link in the template file to <a href=viewprofile.php?id=[logged in user's ID]> My Account </a>. I tried using <a href=viewprofile.php?id=".$id."> My Account <a> but that just linked to viewprofile.php?id=".$id." instead of replacing the ".$id." with the logged in user's ID.

Does this make any more sense?
 
Ok, try this:

In the template file, just go ahead and remove the My Account link all together.

Then, in each php file find:

PHP:
$link2 = "";
if($show_extra_page2 == "yes"){
$link2 = "<li><a href='$extra_page2_link'>$extra_page2_name<span class='tab-l'></span><span class='tab-r'></span></a></li>"; 
}

Add Below:

PHP:
if($username != ""){

$link3 = "<li><a href='viewprofile.php?id=".$username."'>My Account<span class='tab-l'></span><span class='tab-r'></span></a></li>"; 
}

That should make it where if the user is logged in a link will appear at the top of the page that says My Account. When they click on it they will be taken to viewprofile.php?id=USERNAME_HERE.

Try this edit on one file and see if it works and if so then apply it to all the PHP files. :)

Brandon
 
Okay, that works to perfection. But how would I go about making it display the uid instead of the username? Replacing it username with id doesn't display the number. It just displays ?id=
 
If you want to use the ID number, you have to use this code before your link to convert the username to its id number:

PHP:
$query = "SELECT * FROM users WHERE username = '".$username."'";
$result = mysql_query($query);
$num = mysql_numrows($result);

//Loop out code
$i=0;
while ($i < 1) {

$id=@mysql_result($result,$i,"uid");


$i++;
}

Brandon
 
So, the complete code I need to add after the second link, is:

PHP:
$query = "SELECT * FROM users WHERE username = '".$username."'";
$result = mysql_query($query);
$num = mysql_numrows($result);

//Loop out code
$i=0;
while ($i < 1) {

$id=@mysql_result($result,$i,"uid");

if($username != ""){

$link3 = "<li><a href='viewprofile.php?id=".$username."'>My Account<span class='tab-l'></span><span class='tab-r'></span></a></li>"; 
}

Cheers!
 
Missing a few things...

PHP:
$query = "SELECT * FROM users WHERE username = '".$username."'";
$result = mysql_query($query);
$num = mysql_numrows($result);
//Loop out code
$i=0;
while ($i < 1) {

$id=@mysql_result($result,$i,"uid");

$i++;
}

if($username != ""){

$link3 = "<li><a href='viewprofile.php?id=".$username."'>My Account<span class='tab-l'></span><span class='tab-r'></span></a></li>"; 
}

The way you had was an infinite loop that would never complete. :)
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,119
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Latest Posts

Top