tags:

views:

309

answers:

3

I am trying to implement searching functionality in my website. I have following type of data.

Title                Descr  
-----                ----- 
World News           World news, news from across the world, all world news events covered
World Sports         World sports news, for all of sports people, sports is a famous world

Now I want if the user searched for "world news" then result will be displayed like following,

World News (title)
... all world news events covered ... (descr)

Basically I want to display some phrases on random basis. Like when user searching for "world news" then some times he'll get result like above and sometimes like below,

World News (title)
World news, news from across the world, ... (descr)

Please tell me how we can get this kind of functionality using mysql queries, or if its not directly possible through mysql queries then how is it possible using PHP.

Google does the same thing sometimes, google displayes phrases form the middle and sometimes it displayes the starting text.

Thanks.

A: 

If you have the titles and description stored in your mysql table, try something like this:

SELECT descr FROM yourTable WHERE title='World News' ORDER BY RAND()

The ORDER BY RAND() condition will randomly select from one of the rows where the condition of title being World news matches

Click Upvote
I think you're not getting my question properly. I dont want random rows. I want randomly selected phrases from the particular row.
Prashant
+1  A: 

Not sure if there is a MySQL method to achieving what you are after. I would pull the field from the database and then use PHP's explode() function to randomize a phrase:

$desc_field_result = "World news, news from across the world, all world news events covered"; // The "Desc" field from your MySQL Query

$desc_field_array = explode(",",$desc_field_result);

$selected_desc = $desc_field_array[rand(0,sizeof($desc_field_array)-1)];

echo $selected_desc;
StudioKraft
A: 

Google doesn't just return some random sentence. It returns a snippet (or two) that contain the words you searched for. MySQL cannot do that for you. You will either need to do this manually in PHP (manually search for the keywords in the returned database row and display a relevant snippet) or use a search engine that can do this for you (such as Xapian, Sphinx or Apache Lucene).

Sander Marechal