Hi there,
I need to get help in a module on my site. i am working on searching profile site. I have a scenario i need to get matching results from user's profile messages. I want to fetch records of users having at least one common words in their messages. Message is a area in user's profiles where a user can enter some text message.
I need to have a procedure or routine in my php code which can pull out the records of those users who have at least one common word or keyword in their text message. Can anybody help me to guide in this concern. Any sort of help would highly be appreciated.
Thank you,
Allan
PHP Developer
How to get common words in user's profile message
Moderator: General Moderators
-
shahnawaz_rao
- Forum Newbie
- Posts: 1
- Joined: Wed Aug 18, 2010 2:37 am
- MindOverBody
- Forum Commoner
- Posts: 96
- Joined: Fri Aug 06, 2010 9:01 pm
- Location: Osijek, Croatia
Re: How to get common words in user's profile message
I wrote this function for you, hope it will help you in way you want. I wrote comment for every step of function so i think it will be easy to follow what is happening and eventualy adjust it to your needs. Idea was to load all users, and check theirs profile messages for exact keyword given by function parameter.
Cheers!
Cheers!
Code: Select all
// Param: Search Keyword
// Return: array of users with keyword in their profile message
// else returns 0;
// Tip: Function uses defines for User_ID ( USER_ID_COLUMN_NAME ) and User_Profile_Message ( USER_PROFILE_MESSAGE_COLUMN_NAME ) datasbe table column name;
// Also for Users_Table name ( USERS_TABLE_NAME )
function SearchCommonUsers ( $Keyword ) {
// check if Keyword param is not null
if ( is_null( $Keyword ) == true ) return 0;
// make array for all of users
$Users = array();
// Final users; Tho ones that contain search keyword
$FinalUsers = array();
// get all users form db, I assumed you use MySQL db
// and you are already connected to it
$Result = mysql_query( "SELECT " . USER_ID_COLUMN_NAME . ", " . USER_PROFILE_MESSAGE_COLUMN_NAME . " FROM " . USERS_TABLE_NAME );
// to be shure, check if query found users
if ( mysql_num_rows( $Result ) != 0 ){
// for every user...
while ( $Data = mysql_fetch_object( $Result ) ){
// ... push his data to Users array
array_push( $Users, $Data );
}
}
// Now foreach user in Users array do magic
foreach ( $Users as $Candidate ) {
// make array for Users profile message words
$MessageWords = array();
// explode Profile message to array of containing words
$MessageWords = explode( " ", $Candidate -> USER_PROFILE_MESSAGE_COLUMN_NAME );
// now for each word in MessageWords array check if it is equal
// to search keyword ( case-insensitive )
// tip: i used this way becouse i assume you are searching for while word
// but you can ecit this to search for a substring with substr_count() function
foreach( $MessageWords as $Word ){
if ( strtolower( $Word ) == strtolower( $Keyword ) ) {
// if user have our keyword in his profile message
// add his ID to array of $FinalUsers
array_push( $FinalUsers, $Candidate -> USER_ID_COLUMN_NAME );
}
}
}
// check if final user array have members
if ( count( $FinalUsers ) > 0 ) {
// if yes, return final user array
return $FinalUsers;
// ... else return 0
} else return 0;
}