Sample of search engine for multiple tables
Searching a string within a table is quite easy, but searching within multiple tables in a database need a little trick. I know it’s not the good one, perhaps everybody who read this can share yours too π
Example: We have 3 tables with different content and structures for 3 different pages (News, Articles, Products)
< ?php
$string = $_POST['what'];
# 1. Search for News and store it into array
$search_news = mysql_query("SELECT * FROM table_news WHERE news_title LIKE '%$string%' OR news_content LIKE '%$string%' ");
while($result=mysql_fetch_array($search_news)){
$array_results[] = array($result['news_title'], "news.php?id=".$result['news_id']);
}
# 2. Search for Articles and store it into array
$search_articles = mysql_query("SELECT * FROM table_articles WHERE articles_title LIKE '%$string%' OR articles_content LIKE '%$string%' ");
while($result=mysql_fetch_array($search_articles)){
$array_results[] = array($result['articles_title'], "articles.php?id=".$result['articles_id']);
}
# 3. Search for products and store it into array
$search_products = mysql_query("SELECT * FROM table_products WHERE products_name LIKE '%$string%' OR products_content LIKE '%$string%' ");
while($result=mysql_fetch_array($search_products)){
$array_results[] = array($result['products_name'], "products.php?id=".$result['products_id']);
}
// all result now is in the array $array_results. index [0] is for Title, and index [1] is for the URL
echo "
";
foreach($array_results as $search_result){
echo "
";
}
echo "";
?>
You may also like
11 comments
Leave a ReplyCancel reply
Archives
- April 2025
- March 2025
- February 2025
- November 2023
- January 2023
- October 2022
- August 2022
- April 2022
- March 2022
- January 2022
- July 2021
- October 2020
- August 2020
- June 2020
- January 2020
- November 2019
- July 2019
- December 2018
- October 2018
- September 2018
- August 2018
- May 2018
- March 2018
- February 2018
- December 2017
- September 2017
- June 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- July 2015
- June 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- May 2014
- April 2014
- March 2014
- February 2014
- November 2013
- October 2013
- September 2013
- July 2013
- June 2013
- January 2013
- December 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- June 2006
- March 2006
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | |||||
nice tutorial dude, keep on writing! ^_*
kalo di gabung piye? pake join atau union.
Binun, mang bisa yah ampe dua table gitu? knp gak dibikinin viewnya ajah?
baguuus baguuuss… π tapi ga sebagus postingan ttg friendster yg kemaren..
*kabuur*
@linoxs
MVC maksudnya ? kalau gitu harusnya dah diselesaiin sama model. Miyabi ruless..!!
*halah* π
@ agung,
PLETAAAAK !!!
*sambit agung pake termos
The problem came when user only enter a single character. π
Nice tips n e y.
hehehe ngetes command dari consule FreeBSD, iso rak yaoooo
same thing you did, only shorter and extendable:
// get trimed filter from request and type-cast it to string
$filter = (string)trim($_REQUEST[‘filter’]);
// set default array for results
$arr_results = array();
// set array of categories/db tables we wanna work with
$arr_categories = array(
‘news’,
‘articles’,
‘products’
);
// function to retrieve array data from specific table
//
function get_arr_db_data($p_cat_name, $p_filter = ”)
{
// to make sure we return array
$_arr_db_result = array();
// get data
$sql_data = mysql_query(“SELECT * FROM table_”.$cat_name.” WHERE “.$cat_name.”_title LIKE ‘%”.(string)$p_filter.”%’ OR “.$cat_name.”_content LIKE ‘%”.(string)$p_filter.”%’ “);
while($result=mysql_fetch_array($sql_data)){
$_arr_db_result[] = array(
‘title’ => $result[$cat_name . ‘_title’],
‘url’ => $cat_name.”.php?id=” . $result[$cat_name.’_id’]
);
}
return $_arr_db_result;
}
// loop categories/db tables and retrieve data
// store data from all categories to one array
// category name as array key
//
foreach( $arr_categories AS $cat_name ){
$arr_results[$cat_name] = get_arr_db_data($cat_name);
}
don’t you know joins in mysql