Search <a href=''>

Ask about a PHP problem here.
Post Reply
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Search <a href=''>

Post by TehCrayz »

i Have a search function, and on it. i want it to create a link on the users name, to their Specific profile
profile.php?uid=(right here, i want it to show their number)

This is kind of hard to explain...

Basically, when they search for a name, it comes up with the info of the person (done) but i want the name to be click able and take the user to the profile of the person they JUST searched.

http://gyazo.com/ae5bfb2a87b8ac9a2583e7371954ee95
So the name pikachu, would take them to pikachu's profile.

I've tried '<a href="../Profiles/profile.php?uid= $_GET['uid']'"> $result['Username'] </a>' but i think thats wrong. I'm not good at php.....yet...
<?php
include 'func.inc.php';  
?>

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
var message="Function Disabled!";
///////////////////////////////////
function clickIE() {if (document.all) {alert(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {alert(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")
// --> 
</script>
<title>Search</title>
<body>
<span style="color:#FFFFFF">
<h2>Search</h2>
<form action="" method="POST">
	<p>
		<input type="text" name="keywords" /> <input type="submit" value="Search" />
	</p>
</form>
<?php

if (isset($_POST['keywords'])) {
	$suffix = "";
	$keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));

	$errors = array();
	
	if (empty($keywords)) {
		$errors[] = 'Please enter a search term.';
	} 	else if (strlen($keywords)<3) {
		$errors[] = 'Your search term must be <b>three</b> or <b>more</b> characters.';
	} else if (search_results($keywords) === false) {
		$errors[] = 'Your search for '.$keywords.' returned no results.';
	}
	
	if (empty($errors)) {
		$results = search_results($keywords);
		$results_num = count($results);
		
		$suffix = ($results_num !=1) ? 's' : '';
		
		echo '<p>Your search for <strong>', $keywords, '</strong> returned <strong>', $results_num,'</strong> result', $suffix, '</p>';
		
		foreach($results as $result) {
			echo '<p> <strong>ID: </strong>', $result['ID'], '<br>', $result['Username'], '<strong> <br>Fullname: </strong> ', $result['user_firstname'], ' ', $result['user_lastname']. '<br><strong>About: </strong>', $result['user_about'], '</br></p>';
		}
		
		} else {
			foreach($errors as $error) {
			echo $error, '</br>';
			}
		}
	

	
}

?>
</body>
</html>
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Search <a href=''>

Post by TehCrayz »

Also, http://gyazo.com/e812aa6a8463911b23187aff44d0eceb
There's a button, and i would like the user who is logged in to be able to click that button and will take him to HIS profile. Thanks.
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Search <a href=''>

Post by ExtremeGaming »

Is there a reason why you can't just gather the id from the database?
<?php echo "<a href='profile.php?uid=" .$result['uid']. "'>" .$result['Username']. "</a>"; ?>
<?php while(!$succeed = try()); ?>
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Search <a href=''>

Post by TehCrayz »

ExtremeGaming wrote:Is there a reason why you can't just gather the id from the database?
<?php echo "<a href='profile.php?uid=" .$result['uid']. "'>" .$result['Username']. "</a>"; ?>

:O i didn't see it like that! but when i do add ^^^^^^^^^
i get this error


Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\Namenotimportant\Search\index.php on line 55
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Search <a href=''>

Post by ExtremeGaming »

There is no error in the way you used it unless you are using the php tags within php tags like:
<?php

some code...

<?php echo "<a href='profile.php?uid=" .$result['uid']. "'>" .$result['Username']. "</a>"; ?>

some code...

?>
If you aren't, post lines 50-60
<?php while(!$succeed = try()); ?>
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Search <a href=''>

Post by TehCrayz »

I've wrote it correctly, yet it still doesn't work. Plus, the last '?>' isn't the color its ment to be, i think i have done an error..
<?php
include 'func.inc.php';  
?>

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
var message="Function Disabled!";
///////////////////////////////////
function clickIE() {if (document.all) {alert(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {alert(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")
// --> 
</script>
<title>Search</title>
<body>
<span style="color:#FFFFFF">
<h2>Search</h2>
<form action="" method="POST">
	<p>
		<input type="text" name="keywords" /> <input type="submit" value="Search" />
	</p>
</form>
<?php

if (isset($_POST['keywords'])) {
	$suffix = "";
	$keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));

	$errors = array();
	
	if (empty($keywords)) {
		$errors[] = 'Please enter a search term.';
	} 	else if (strlen($keywords)<3) {
		$errors[] = 'Your search term must be <b>three</b> or <b>more</b> characters.';
	} else if (search_results($keywords) === false) {
		$errors[] = 'Your search for '.$keywords.' returned no results.';
	}
	
	if (empty($errors)) {
		$results = search_results($keywords);
		$results_num = count($results);
		
		$suffix = ($results_num !=1) ? 's' : '';
		
		echo '<p>Your search for <strong>', $keywords, '</strong> returned <strong>', $results_num,'</strong> result', $suffix, '</p>';
		
		foreach($results as $result) {
			echo '<p> <strong>ID: </strong>', $result['ID'], '<br>',<?php echo "<a href='profile.php?uid=" .$result['uid']. "'>" .$result['Username']. "</a>"; ?>'<strong> <br>Fullname: </strong> ', $result['user_firstname'], ' ', $result['user_lastname']. '<br><strong>About: </strong>', $result['user_about'], '</br></p>';
		}
		
		} else {
			foreach($errors as $error) {
			echo $error, '</br>';
			}
		}
	

	
}

?>
</body>
</html>
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Search <a href=''>

Post by TehCrayz »

For some reason, the line you told me to add does something that screws it up.
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Search <a href=''>

Post by ExtremeGaming »

These lines have a couple things wrong with them, however the main thing is that you are putting php tags within php which will provide the error you stated. Change all this:
		foreach($results as $result) {
			echo '<p> <strong>ID: </strong>', $result['ID'], '<br>',<?php echo "<a href='profile.php?uid=" .$result['uid']. "'>" .$result['Username']. "</a>"; ?>'<strong> <br>Fullname: </strong> ', $result['user_firstname'], ' ', $result['user_lastname']. '<br><strong>About: </strong>', $result['user_about'], '</br></p>';
		}
To this, and see what you get.
		foreach($results as $result) {
			echo "<p> <strong>ID: </strong>". $result['ID']. "<br> <a href='profile.php?uid=" .$result['uid']. "'>" .$result['Username']. "</a><strong> <br>Fullname: </strong>".$result['user_firstname']." ".$result['user_lastname']."<br><strong>About: </strong>". $result['user_about']. "</br></p>";
		}
<?php while(!$succeed = try()); ?>
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Search <a href=''>

Post by TehCrayz »

ignore this, this problem has been solved
Last edited by TehCrayz on Sun Dec 16, 2012 4:37 pm, edited 1 time in total.
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Search <a href=''>

Post by TehCrayz »

Ok, i had some problems which i was going to post, but i solved them. But now, when i search something. i get a uid error.

Your search for Crayz returned 2 results

Notice: Undefined index: uid in C:\xampp\htdocs\GameCrayz\Search\index.php on line 55

Profile.php
    <?php
     
    include('core/init.inc.php');
     
    $user_info = fetch_user_info($_GET['uid']);
     
     
    ?>
    <?php
    mysql_connect("localhost","root","Notimportant:P");
    mysql_select_db("commentbox");
    $name=strip_tags(@$_POST['name']);
    $comment=strip_tags(@$_POST['comment']);
    $submit=@$_POST['submit'];
     
    $dbLink = mysql_connect("localhost", "root", "Notimportant:P");
            mysql_query("SET character_set_client=utf8", $dbLink);
            mysql_query("SET character_set_connection=utf8", $dbLink);
     
    if($submit)
    {
    if($name&&$comment)
    {
    $insert=mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment') ");
    }
    else
    {
    echo "Please fill in <b>all</b> the fields.";
    }
    }
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
    div.ex
    {
    width:220px;
    padding:10px;
    border:5px solid gray;
    margin:0px;
    }
    </style>
    <title><?php echo $user_info['username']; ?>'s Profile - GameCrayz</title>
    </head>
    <body>
    <div>
    <?php
     
    if ($user_info === false){
    echo 'That user does not exist.';
    }else{
    ?>
    <h1><?php echo $user_info['firstname']; ?> <?php echo $user_info['lastname']; ?></h1>
    <p>Username: <?php echo $user_info['username']; ?></p>
    <p>Gender: <?php echo ($user_info['gender'] ==1) ? 'Male' : 'Female'; ?></p>
    <p>Email: <?php echo $user_info['email']; ?></p>
    <p>Location: <?php echo $user_info['location']; ?></p>
    <p>About me:<br><?php echo $user_info['about']; ?></p>
    <a href='edit_profile.php'>Edit profile</a>
    <?php
    }
    ?>
    </div>
    <div class="ex">
    <center>
    <form action="" method="POST">
    <table>
    <tr><td>Name: <br><input type="text" name="name" placeholder='Enter your name...' style="width:150px;height:50px;font-family:cursive;border:double 12px #6DB72C;"/></td></tr>
    <tr><td colspan="2">Comment: </td></tr>
    <tr><td colspan="5"><textarea name="comment" rows="5" cols="50" style="width:200px;height:100px;font-family:cursive;border:double 12px #6DB72C;" placeholder='Enter a comment...'></textarea></td></tr>
    <tr><td colspan="2"><input type="submit" name="submit" value='Comment'></td></tr>
    </table>
    </form>
     
    <?php
    $dbLink = mysql_connect("localhost", "root", "Notimportant:P");
            mysql_query("SET character_set_results=utf8", $dbLink);
            mb_language('uni');
            mb_internal_encoding('UTF-8');
     
    $getquery=mysql_query("SELECT * FROM commenttable ORDER BY id DESC");
    while($rows=mysql_fetch_assoc($getquery))
    {
    $id=$rows['id'];
    $name=$rows['name'];
    $comment=$rows['comment'];
    echo $name . ':<br/>' . '</br>' . $comment . '' . '<br/>' . '<hr size="1"/>'
    ;}
    ?>
     
    </div>
    </body>
    </html>
User.inc.php
<?php
//fetches all of the users from the table
function fetch_users(){
$result = mysql_query('SELECT `ID` AS `id`, `Username` AS `username` FROM `users`');

$users = array();

while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}

return $users;
}
//fetches profile info for the given user
function fetch_user_info($uid){
$uid = (int)$uid;

$sql = "SELECT
`Username` AS `username`,
`user_firstname` AS `firstname`,
`user_lastname` AS `lastname`,
`user_email` AS `email`,
`user_about` AS `about`,
`user_location` AS `location`,
`user_gender` AS `gender`
FROM `users`
WHERE `ID` = {$uid}";

$result = mysql_query($sql);

return mysql_fetch_assoc($result);
}
//updates the current users profile info.
function set_profile_info($email, $about, $location){
	$email	   = mysql_real_escape_string(htmlentities($email));
	$about 	   = mysql_real_escape_string(nl2br(htmlentities($about)));
	$location  = mysql_real_escape_string(htmlentities($location));
	
	$sql = "UPDATE `users` SET
				`user_email` = '{$email}',
				`user_about` = '{$about}',
				`user_location` = '{$location}'
			WHERE `user_id` = {$_SESSION['uid']}";
			
	mysql_query($sql);
}

?>
Index.php(search)
<?php
include 'func.inc.php';  
?>

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
var message="Function Disabled!";
///////////////////////////////////
function clickIE() {if (document.all) {alert(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {alert(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")
// --> 
</script>
<title>Search</title>
<body>
<span style="color:#FFFFFF">
<h2>Search</h2>
<form action="" method="POST">
	<p>
		<input type="text" name="keywords" /> <input type="submit" value="Search" />
	</p>
</form>
<?php

if (isset($_POST['keywords'])) {
	$suffix = "";
	$keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));

	$errors = array();
	
	if (empty($keywords)) {
		$errors[] = 'Please enter a search term.';
	} 	else if (strlen($keywords)<3) {
		$errors[] = 'Your search term must be <b>three</b> or <b>more</b> characters.';
	} else if (search_results($keywords) === false) {
		$errors[] = 'Your search for '.$keywords.' returned no results.';
	}
	
	if (empty($errors)) {
		$results = search_results($keywords);
		$results_num = count($results);
		
		$suffix = ($results_num !=1) ? 's' : '';
		
		echo '<p>Your search for <strong>', $keywords, '</strong> returned <strong>', $results_num,'</strong> result', $suffix, '</p>';
		
		foreach($results as $result) {
                        echo "<p> <strong>ID: </strong>". $result['ID']. "<br> <a href='../Profiles/profile.php?uid=" .$result['uid']. "' target=_blank>" .$result['Username']. "</a><strong> <br>Fullname: </strong>".$result['user_firstname']." ".$result['user_lastname']."<br><strong>About: </strong>". $result['user_about']. "</br></p>";
                }
		
		} else {
			foreach($errors as $error) {
			echo $error, '</br>';
			}
		}
	

}
?>
</body>
</html>
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Search <a href=''>

Post by ExtremeGaming »

You are going to have to post the function search_results(); but that generally means that you aren't selecting it from the database so it's returning an empty result.
<?php while(!$succeed = try()); ?>
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Search <a href=''>

Post by TehCrayz »

ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Search <a href=''>

Post by ExtremeGaming »

You have a function called search_results(); as seen here:
$results = search_results($keywords);
You undefined index notice is a result of there not being a uid being selected from within your function. You are going to have to post the function for more information.
<?php while(!$succeed = try()); ?>
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Search <a href=''>

Post by Helx »

Moved to PHP subforum.
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Search <a href=''>

Post by TehCrayz »

Db.inc.php
<?php

mysql_connect('localhost', 'root', 'Hi....');
mysql_select_db('phplogin');


?>
Func.inc.php
<?php
include 'db.inc.php';

function search_results($keywords) {
	$returned_results = array();
	$where = "";
	
	$keywords = preg_split('/[\s]+/', $keywords);
	$total_keywords = count($keywords);
	
	foreach($keywords as $key=>$keyword) {
		$where .= "`Username` LIKE '%$keyword%'";
		if ($key != ($total_keywords - 1)) {
			$where .= " AND ";
		}
	}
	
	$results = "SELECT `ID`, `Username`, `user_firstname`, `user_lastname`, LEFT(`user_about`, 50) as `user_about` FROM `users` WHERE $where";
	$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;

	if ($results_num === 0) {
		return false;
	} else {
		
		while ($results_row = mysql_fetch_assoc($results)) {
			$returned_results[] = array (
				'ID' => $results_row['ID'],
				'Username' => $results_row['Username'],
				'user_firstname' => $results_row['user_firstname'],
				'user_lastname' => $results_row['user_lastname'],
				'user_about' => $results_row['user_about'],
			);
		}
		
		return $returned_results;
		
	}
	
	
}

?>
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Search <a href=''>

Post by ExtremeGaming »

You aren't selecting uid from your database in your query. You need to add that into your query, then later on add it to your returned results array.
<?php while(!$succeed = try()); ?>
Post Reply