Page 1 of 2

Search

Posted: Sun Apr 08, 2012 6:00 am
by jaysus7
How do you link your searches????

blog search
<?php
error_reporting(E_ALL);
function search_posts($term){
	$keywords = preg_split('#\s+#', mysql_real_escape_string($trem));
	
	if(empty($keywords)){
		return array();
	}
	
$title_where = "`post_title` LIKE '%" . implode("%'OR `post_title` LIKE '%", $keywords) . "%'";
$body_where = "`post_body` LIKE '%" . implode("%'OR `post_body` LIKE '%", $keywords) . "%'";

$sql = "SELECT
			`post_title` AS `title`
			LEFT (`post_body`, 100) AS `body`
		FROM `posts`
		WHERE {$title_where}
		OR {$body_where}";

$result = mysql_query($sql);

$results = array();

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

return $results;

}


?>

search shows page
 <form action="" method"get"> 
      Viral Search
        <input type="text" size="30" name="term" />
        <input type="submit" value="search" />
    </form>
    <p> </p>
    <p>
	<?php
    
if (empty($_GET['term']) === false){
	$search_results = search_posts($_GET['term']);
	
	if (empty($_GET['term'])){
		echo "Your search came up with 0 results";
		
	}
	
	foreach ($search_results as $result){
	    echo "<h3>{$result['title']}</h3>"; 
		echo "<p>{$result['body']}</p>"; 
		
		
	}
	
}	
	?>


also search for users???


user search
<?php
error_reporting(E_ALL);
function search_users($term){
	$keywords = preg_split('#\s+#', mysql_real_escape_string($trem));
	
	if(empty($keywords)){
		return array();
	}
	
$uid_where = "`user_id` LIKE '%" . implode("%'OR `user_id`` LIKE '%", $keywords) . "%'";
$username_where = "`user_username` LIKE '%" . implode("%' OR `user_username` LIKE '%", $keywords) . "%'";

$sql = "SELECT
			`user_id` AS `uid`
			LEFT (`user_username`, 100) AS `username`
		FROM `users`
		WHERE {$uid_where}
		OR {$username_where}";

$result = mysql_query($sql);

$results = array();

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

return $results;

}

?>
showing user search
    <form action="" method"get"> 
      Prof. Finder
        <input type="text" size="30" name="term" />
        <input type="submit" value="search" />
    </form>
    <p> </p>
    <p>
	<?php
    
if (empty($_GET['term']) === false){
	$search_results = search_users($_GET['term']);
	
	if (empty($_GET['term'])){
		echo "Your search came up with 0 results";
		
	}
	
	foreach ($search_results as $result){
	    echo "<h3>{$result['uid']}</h3>"; 
		echo "<p>{$result['username']}</p>"; 
		
		
	}
	
}	
	?>

Re: Search

Posted: Tue Apr 10, 2012 3:21 pm
by jacek
Call both functions with the same parameters ?

Re: Search

Posted: Wed Apr 18, 2012 4:32 pm
by jaysus7
paramaters??? just user search...and linking the searches???? where do i start on that???

Re: Search

Posted: Thu Apr 19, 2012 3:06 pm
by jacek
Well you have a function that search users based on some terms and one that search posts based on some terms. So just use both of them with what ever input the user gives and that's it :? Or have I misunderstood what you want ?

Re: Search

Posted: Mon Apr 23, 2012 1:23 am
by jaysus7
i need the user names to be searched. how does my recycled code from the blog search look? am i on the right track??

user search back file
<?php
error_reporting(E_ALL);
function search_users($uid){
	$keywords = preg_split('#\s+#', mysql_real_escape_string($uid));
	
	if(empty($keywords)){
		return array();
	}
	
$uid_where = "`user_id` LIKE '%" . implode("%'OR `user_id`` LIKE '%", $keywords) . "%'";
$username_where = "`user_username` LIKE '%" . implode("%' OR `user_username` LIKE '%", $keywords) . "%'";

$sql = "SELECT
			`user_id` AS `uid`
			LEFT (`user_username`, 100) AS `username`
		FROM `users`
		WHERE {$uid_where}
		OR {$username_where}";

$result = mysql_query($sql);

$results = array();

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

return $results;

}

?>

user search

<?php

include ("core/init.inc.php");
include ("core/search proffinder.php");

?>

<form action="" method"get"> 
      Prof. Finder
        <input type="text" size="30" name="term" />
        <input type="submit" value="search" />
    </form>
    <p> </p>
    <p>
	<?php
    
if (empty($_GET['uid']) === false){
	$search_results = search_users($_GET['uid']);
	
	if (empty($_GET['uid'])){
		echo "Your search came up with 0 results";
		
	}
	
	foreach ($search_results as $result){
	    echo "<h3>{$result['uid']}</h3>"; 
		echo "<p>{$result['username']}</p>"; 
		
		
	}
	
}	
?>

Re: Search

Posted: Tue Apr 24, 2012 9:25 pm
by jacek
It looks like ti would work to me, have you tried it ?

Re: Search

Posted: Tue Apr 24, 2012 9:57 pm
by jaysus7
yes it doesn't show any results

Re: Search

Posted: Thu Apr 26, 2012 12:23 pm
by jacek
Actually it looks like you have a syntax error on this line
$uid_where = "`user_id` LIKE '%" . implode("%'OR `user_id`` LIKE '%", $keywords) . "%'";
notice the double `` after user_id

Maybe try
echo mysql_error();
?

Re: Search

Posted: Sun Apr 29, 2012 6:16 pm
by jaysus7
nothing poped up...I am wondering do i use the users table fpr the valid pid or the comments table??

Re: Search

Posted: Mon Apr 30, 2012 4:33 pm
by jacek
Presumably the comments table, since you want to check to see if a comment is vaild don't you ?

Re: Search

Posted: Mon Apr 30, 2012 5:13 pm
by jaysus7
eh, no, i want to search for my users at this point....i got the comments, and blog posts searching fine, but not the users?


also i want it so when youu search them the results are linked to that user, or blog post!

Re: Search

Posted: Thu May 03, 2012 12:11 am
by jacek
jaysus7 wrote:eh, no, i want to search for my users at this point....i got the comments, and blog posts searching fine, but not the users?
Well which ever table you want to check in, use that. What is the problem exactly ? What you have looks more or less like it would work.
jaysus7 wrote:also i want it so when youu search them the results are linked to that user, or blog post!
You have the id from the table, put it into a link.

Re: Search

Posted: Mon May 07, 2012 3:44 am
by jaysus7
ok first issue is that i can't get my users to show up at all when i search. it all looks right to me as well thats why I'm so stump? i feel like there is something small missing, i just am too much of a rookie to know what.

my second issue is that when i try to link the results. it always gives me a syntax error, i realize cuz I'm using html in a php tag and that is wh i have tried to work around it numerous ways but have failed to succeed

Re: Search

Posted: Mon May 07, 2012 3:43 pm
by jacek
Can you post the full code as it is now ? If the query is returning 0 rows all of the time it will be something wrong with the WHERE.

Re: Search

Posted: Tue May 08, 2012 3:11 am
by jaysus7
back file
//Searches users
function search_users($uid){
	$keywords = preg_split('#\s+#', mysql_real_escape_string($uid));
	
	if(empty($keywords)){
		return array();
	}
	
$uid_where = "`user_id` LIKE '%" . implode("%'OR `user_id` LIKE '%", $keywords) . "%'";
$username_where = "`user_username` LIKE '%" . implode("%' OR `user_username` LIKE '%", $keywords) . "%'";

$sql = "SELECT
			`user_id` AS `uid`
			LEFT (`user_username`, 100) AS `users`
		FROM `users`
		WHERE {$uid_where}
		OR {$username_where}";

$result = mysql_query($sql);

$results = array();

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

return $results;

}
?>

search

    <form action="" method"get"> 
      Prof. Finder
        <input type="text" size="30" name="term" />
        <input type="submit" value="search" />
    </form>
    <p> </p>
    <p>
        <?php
   
if (empty($_GET['username']) === false){
        $search_results = search_users($_GET['username']);
       
        if (empty($_GET['username'])){
            echo "Your search came up with 0 results";
               
        }
       
        foreach ($search_results as $result){
            echo "<h3>{$result['username']}</h3>";
               
               
        }
       
}      
?>

Re: Search

Posted: Thu May 10, 2012 12:42 am
by jacek
You have a missing space here
"%'OR
Not sure if that would cause the problem, but it's worth a try.

Your check
if (empty($_GET['username'])){
should be
if (empty($search_results)){
too. Just a typo.

Re: Search

Posted: Fri May 11, 2012 3:32 am
by jaysus7
no man still nothing just a blank ill keep dying though :P

Re: Search

Posted: Fri May 11, 2012 6:54 pm
by jacek
jaysus7 wrote:no man still nothing just a blank ill keep dying though :P
Are you getting a blank page ? That normally indicated a syntax error.

Re: Search

Posted: Fri May 11, 2012 11:22 pm
by jaysus7
yes i am but i don't see any errors the program usually shows me

Re: Search

Posted: Sun May 13, 2012 12:53 pm
by jacek
jaysus7 wrote:yes i am but i don't see any errors the program usually shows me
Could there be something that is killing the script somewhere, maybe in the init file ?

Re: Search

Posted: Wed May 16, 2012 3:13 am
by jaysus7
nope



init
<?php

session_start ();

mysql_connect("??????????????????????","????????????????","?????????????");
mysql_select_db("???????????");

$path = dirname(__FILE__);

include ("{$path}/init/user.inc.php");

if (isset($_COOKIE['username'], $_COOKIE['password']) && isset($_SESSION['username']) === false){
	if (valid_credentials($_COOKIE['username'], $_COOKIE['password'])){
		$_SESSION['username'] = htmlentities($_COOKIE['username']);
	
		setcookie('username', $_COOKIE['username'], time() + 604800);
		setcookie('password', $_COOKIE['password'], time() + 604800);
	
	}

}
if (isset($_SESSION['username'])){
        $_SESSION['uid'] = fetch_current_user_id($_SESSION['username']);
}



?>

user.inc


<?php
error_reporting(E_ALL);
//fetches the current logged in users id
function fetch_current_user_id($username){
        $username = mysql_real_escape_string($username);
       
        $sql = "SELECT `user_id` FROM `users` WHERE `user_username` = '{$username}'";
        $result = mysql_query($sql);
       
        return mysql_result($result, 0);
}
 


//check if given username exsists in the database
function user_exists($user){
	$user = mysql_real_escape_string($user);
	
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}'");
	echo mysql_error();
	return (mysql_result($total, 0) == '1') ? true : false;

}
//check if the given username and password combinations are valid
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = mysql_real_escape_string($pass);
	
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}' AND `user_password` = '{$pass}'");
	
	return (mysql_result($total, 0) == '1') ? true : false;
	
}
//checks to see is user account is active
	function is_active($user){
		$user = mysql_real_escape_string($user);
		echo mysql_error();
		$sql = "SELECT
					COUNT(`activations`.`user_id`)
				FROM `users`
				INNER JOIN `activations`
				ON `users`.`user_id` = `activations`.`user_id`
				WHERE `users`.`user_username` = '{$user}'";
				
	$result = mysql_query($sql);

	return (mysql_result($result, 0) == '0') ? true : false;
		
}
//acctivates the account related to the given activation code
function activate_account($aid){
	$aid = mysql_real_escape_string($aid);
	
	mysql_query("DELETE FROM `activations` WHERE `activation_code` = '{$aid}'");
echo mysql_error();
}

//adds a user to the database
function add_user($user, $email, $pass, $first, $last){
	$user		= mysql_real_escape_string(htmlentities($user));
	$email		= mysql_real_escape_string($email);
	$pass		= sha1($pass);
	$first		= mysql_real_escape_string(htmlentities($first));
	$last		= mysql_real_escape_string(htmlentities($last));
	
	$charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range('0', '9')));
	$aid =implode('', array_rand($charset, 10)); 
	
	$body =	<<<EMAIL
	
	Thank you for signing up with knowquest. To activate your account, please click the link below:
	
	http://www.jasonmassieportfolio.com/act ... ?aid={$aid}
	
EMAIL;
	
	mail($email, 'Your new account at Knowquest.com', $body, 'From: admin@knowquest.com');
	
	mysql_query("INSERT INTO `users` (`user_username`, `user_email`, `user_password`,`user_firstname`,`user_lastname`) VALUES ('{$user}', '{$email}', '{$pass}','{$first}','{$last}')");
	echo mysql_error();
	$user_id = mysql_insert_id();
	
	mysql_query("INSERT INTO `activations` (`user_id`, `activation_code`) VALUES ({$user_id}, '{$aid}')");
echo mysql_error();
}

//fetches all of the users from the table
function fetch_users(){
        $result = mysql_query("SELECT `user_id` AS `id`, `user_username` AS `username` FROM `users`"); 
        
        $users = array();
       
        while (($row = mysql_fetch_assoc($result)) !== false){
                $users[] = $row;
        }
       
        return $users;
}
 
//fetches profile info for given user
function fetch_user_info($uid){
        $uid = (int)$uid;
       
        $sql = "SELECT
                   `user_id` AS `id`,
                   `user_username` AS `username`,
                   `user_email` AS `email`,
                   `user_firstname` AS `firstname`,
                   `user_lastname` AS `lastname`,
                   `user_institution` AS `institution`,
                   `user_department` AS `department`,
                   `user_location` AS `location`,
                   `user_speciality` AS `speciality`,
                   `user_key words` AS `keywords`,
                   `user_research centre` AS `researchcentre`,
                   `user_website` AS `website`,
				   `user_articles published pre` AS `articlespublishedpre`,
                   `user_articles published non pre` AS `articlespublishednonpre`,
                   `user_published books` AS `publishedbooks`,
                   `user_social media` AS `socialmedia`,
				   `user_published chapters` AS `publishedchapters`,
				   `user_confidence proceedings` AS `confidenceproceedings`,
                   `user_non acidemic influences` AS `nonacidemicinfluences`,
                   `user_data sets` AS `datasets`,
                   `user_research methodology perferred` AS `researchmethodologyperferred`,
                   `user_researchers cited in your research` AS `researcherscitedinyourresearch`,
                   `user_memberships` AS `memberships`,
                   `user_acidemic influences` AS `acidemicinfluences`
                 FROM `users`
                 WHERE `user_id` = {$uid}";
                       
          $result = mysql_query($sql);
            	echo mysql_error();
          $info = mysql_fetch_assoc($result);
                      
          $info['avatar'] = (file_exists("{$GLOBALS['path']}/user_avatars/{$info['id']}.jpg")) ? "core/user_avatars/{$info['id']}.jpg" : "core/user_avatars/default.jpg";
                      
          return $info;
}

//updates current user portfolio info
function set_profile_info($email, $institution, $department, $location, $speciality, $keywords, $researchcentre, $website, $articlespublishedpre, $articlespublishednonpre, $publishedbooks, $socialmedia, $publishedchapters, $confidenceproceedings, $nonacidemicinfluences, $datasets, $researchmethodologyperferred, $researcherscitedinyourresearch, $memberships, $acidemicinfluences, $avatar){
        $email                   	 			= mysql_real_escape_string(htmlentities($email));
		$institution                   			= mysql_real_escape_string(htmlentities($institution));
        $department               				= mysql_real_escape_string(htmlentities($department));
        $location              					= mysql_real_escape_string(htmlentities($location));
        $speciality   							= mysql_real_escape_string(htmlentities($speciality));
        $keywords    							= mysql_real_escape_string(htmlentities($keywords));
        $researchcentre                			= mysql_real_escape_string(htmlentities($researchcentre));
        $website                 				= mysql_real_escape_string(htmlentities($website));
        $articlespublishedpre         		    = mysql_real_escape_string(htmlentities($articlespublishedpre));
		$articlespublishednonpre         		= mysql_real_escape_string(htmlentities($articlespublishednonpre));
        $publishedbooks              			= mysql_real_escape_string(htmlentities($publishedbooks));
		$socialmedia             				= mysql_real_escape_string(htmlentities($socialmedia));
        $publishedchapters                  	= mysql_real_escape_string(htmlentities($publishedchapters));
        $confidenceproceedings                  = mysql_real_escape_string(htmlentities($confidenceproceedings));
		$nonacidemicinfluences        	        = mysql_real_escape_string(htmlentities($nonacidemicinfluences));
        $datasets   							= mysql_real_escape_string(htmlentities($datasets));
        $researchmethodologyperferred    		= mysql_real_escape_string(htmlentities($researchmethodologyperferred));
        $researcherscitedinyourresearch         = mysql_real_escape_string(htmlentities($researcherscitedinyourresearch));
        $memberships                  			= mysql_real_escape_string(htmlentities($memberships));
        $acidemicinfluences              		= mysql_real_escape_string(htmlentities($acidemicinfluences));
     
 		 if (file_exists($avatar)){
                $src_size = getimagesize($avatar);
               			
                if ($src_size['mime'] === 'image/jpeg'){
                    $src_img = imagecreatefromjpeg($avatar);
                }else if ($src_size['mime'] === 'image/png'){
                    $src_img = imagecreatefrompng($avatar);
                }else if ($src_size['mime'] === 'image/gif'){
                    $src_img = imagecreatefromgif($avatar);
                }else{
                 $src_img = false;
                }
                       
                if ($src_img !== false){
                    $thumb_width = 200;
                               
               	    if ($src_size[0] <= $thumb_width){
                        $thumb = $src_img;
					}else{
                         $new_size[0] = $thumb_width;
                         $new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;
                                       
                         $thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
                         imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
                    }
					
                    imagejpeg($thumb, "{$GLOBALS['path']}/user_avatars/{$_SESSION['uid']}.jpg");
                
				}
        
		}
       
        $sql = "UPDATE `users` SET
                    `user_email` = '{$email}',
                    `user_institution` = '{$institution}',
                    `user_department` = '{$department}',
                    `user_location` = '{$location}',
                    `user_speciality` = '{$speciality}',
                    `user_key words` = '{$keywords}',
                    `user_research centre` = '{$researchcentre}',
                    `user_website` = '{$website}',
                    `user_articles published pre` = '{$articlespublishedpre}',
					`user_articles published non pre` = '{$articlespublishednonpre}',
                    `user_published books` = '{$publishedbooks}',
                    `user_social media` = '{$socialmedia}',
                    `user_published chapters` = '{$publishedchapters}',
					`user_confidence proceedings` = '{$confidenceproceedings}',
                    `user_non acidemic influences` = '{$nonacidemicinfluences}',
                    `user_data sets` = '{$datasets}',
                    `user_research methodology perferred` = '{$researchmethodologyperferred}',
                    `user_researchers cited in your research` = '{$researcherscitedinyourresearch}',
                    `user_memberships` = '{$memberships}',
					`user_acidemic influences` = '{$acidemicinfluences}'
                WHERE `user_id` = {$_SESSION['uid']}";
              
        mysql_query($sql);
        echo mysql_error();
}

//checks if the given user id is in the table
function valid_uid($uid){
        $uid = (int)$uid;
       
        $total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_id` = {$uid}");
        $total = mysql_result($total, 0);
 
        if ($total != 1){
                return false;
        }else{
                return true;
        }
}

//fetches a single user from the table
function get_user($uid){
        $uid = (int)$uid;
       
        $sql = "SELECT
						`user_id` AS `uid`
                FROM `users`
                WHERE `user_id` = {$uid}";
               
        $post = mysql_query($sql);echo mysql_error();
        $post = mysql_fetch_assoc($post);
       
        $post['comments'] = get_comments($uid);
       
       
        return $post;
}

//fetches all the comments for a given user wall post
function get_wall_comments($uid){
	$uid = (int)$uid;
	
	$sql = "SELECT
				`comment_body` AS `body`,
				`comment_user` AS `user`,
				DATE_FORMAT(`comment_date`, '%d/%m/%Y %H:%i:%s') AS `date`
			FROM `wallcomments`
			WHERE `user_id` = {$uid}";
	
	$comments = mysql_query($sql);
	
	$return = array();
	while (($row = mysql_fetch_assoc($comments)) !== false){
		$return[] = $row;
	}
		return $return;
}

//add a wall comment
function add_wall_comment($uid, $body, $user){
	if (valid_uid($uid) === false){
		return false;
	}
	
	$uid	= (int)$uid;
	$body	= mysql_real_escape_string(nl2br(htmlentities($body)));
	$user	= mysql_real_escape_string(htmlentities($user));
	
	
	mysql_query("INSERT INTO `wallcomments` (`user_id`, `comment_body`, `comment_user`, `comment_date`) VALUES ('{$uid}', '{$user}', '{$body}', NOW())");

	return true;
	
}

//Searches users
function search_users($uid){
	$keywords = preg_split('#\s+#', mysql_real_escape_string($uid));
	
	if(empty($keywords)){
		return array();
	}
	
$uid_where = "`user_id` LIKE '%" . implode("%' OR `user_id` LIKE '%", $keywords) . "%'";
$username_where = "`user_username` LIKE '%" . implode("%' OR `user_username` LIKE '%", $keywords) . "%'";

$sql = "SELECT
			`user_id` AS `uid`
			LEFT (`user_username`, 100) AS `users`
		FROM `users`
		WHERE {$username_where}
		OR {$uid_where}";

$result = mysql_query($sql);echo mysql_error();

$results = array();

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

return $results;

}
?>

search
    <form action="" method"get"> 
      Prof. Finder
        <input type="text" size="30" name="usrename" />
        <input type="submit" value="search" />
    </form>
    <p> </p>
    <p>
        <?php
   
if (empty($_GET['username']) === false){
        $search_results = search_users($_GET['username']);
       
        if (empty($search_results)){
            echo "Your search came up with 0 results";
               
        }
       
        foreach ($search_results as $result){
            echo "<h3>{$result['username']}</h3>";
               
               
        }
       
}      
?>


it looks all fine to me :(