PHP - Search Engine For My Site
I have a search bar that searches my databases for information. It works perfectly on my site,. I want to know how to make it modular so that other people can place my search box on their site and get results from my databse without having to give out all my site infomration.
Similar TutorialsPhp search engine for my site I have a Romanian based language site: http://www.firmelavedere.com with informations about business companies. I implemented a simple php search engine for my website on pages like this on the top right corner: http://www.firmelave...aj-Zalau-15.php I want to add a new radio box field to select language for displayed text. Is there a possibility to integrate Google Site Search with more languages? How can i do that? I need some code suggestions, to make this script run fast. Thanks all. I want to know how to display results from mysql database by filling in a form. but i have found a tutorial which shows typing in a text box which displays results. if i follow this tutorial will it help me to understand and create php coding to display results for my form? I require a page to be added to my website. This page will facilitate a refined search via Google, Bing and Yahoo search engine simultaneously , show the top 5 of each engine. Is this possible using php ? Thank's Hello, Does anyone know a tutorial I can follow to create my own search engine over the items I have in my SQL database? I find a lot of tutorials but they are all 'one word searches' which means if you type two words you will get all results that contain either word (too many hits). If the search engine tutorial displays result with AJAX even better. Thanks for help. df Friends, I want to extract the Search Keyword from the URL, a visitor came from. I am using a PHP CMS and want to show the Keyword on my Blog. So if they search "abcd" from google, i want to extract "abcd" and echo on my blog. Here is the coding i could got hold of, but its not working, not echoing anything <?php function pk_stt2_function_get_delimiter($ref) { $search_engines = array('google.com' => 'q', 'go.google.com' => 'q', 'images.google.com' => 'q', 'video.google.com' => 'q', 'news.google.com' => 'q', 'blogsearch.google.com' => 'q', 'maps.google.com' => 'q', 'local.google.com' => 'q', 'search.yahoo.com' => 'p', 'search.msn.com' => 'q', 'bing.com' => 'q', 'msxml.excite.com' => 'qkw', 'search.lycos.com' => 'query', 'alltheweb.com' => 'q', 'search.aol.com' => 'query', 'search.iwon.com' => 'searchfor', 'ask.com' => 'q', 'ask.co.uk' => 'ask', 'search.cometsystems.com' => 'qry', 'hotbot.com' => 'query', 'overture.com' => 'Keywords', 'metacrawler.com' => 'qkw', 'search.netscape.com' => 'query', 'looksmart.com' => 'key', 'dpxml.webcrawler.com' => 'qkw', 'search.earthlink.net' => 'q', 'search.viewpoint.com' => 'k', 'mamma.com' => 'query'); $delim = false; if (isset($search_engines[$ref])) { $delim = $search_engines[$ref]; } else { if (strpos('ref:'.$ref,'google')) $delim = "q"; elseif (strpos('ref:'.$ref,'search.atomz.')) $delim = "sp-q"; elseif (strpos('ref:'.$ref,'search.msn.')) $delim = "q"; elseif (strpos('ref:'.$ref,'search.yahoo.')) $delim = "p"; elseif (preg_match('/home\.bellsouth\.net\/s\/s\.dll/i', $ref)) $delim = "bellsouth"; } return $delim; } /** * retrieve the search terms from search engine query * */ function pk_stt2_function_get_terms($d) { $terms = null; $query_array = array(); $query_terms = null; $query = explode($d.'=', $_SERVER['HTTP_REFERER']); $query = explode('&', $query[1]); $query = urldecode($query[0]); $query = str_replace("'", '', $query); $query = str_replace('"', '', $query); $query_array = preg_split('/[\s,\+\.]+/',$query); $query_terms = implode(' ', $query_array); $terms = htmlspecialchars(urldecode(trim($query_terms))); return $terms; } /** * get the referer * */ function pk_stt2_function_get_referer() { if (!isset($_SERVER['HTTP_REFERER']) || ($_SERVER['HTTP_REFERER'] == '')) return false; $referer_info = parse_url($_SERVER['HTTP_REFERER']); $referer = $referer_info['host']; if(substr($referer, 0, 4) == 'www.') $referer = substr($referer, 4); return $referer; } $referer = pk_stt2_function_get_referer(); if (!$referer) return false; $delimiter = pk_stt2_function_get_delimiter($referer); if( $delimiter ){ $term = pk_stt2_function_get_terms($delimiter); } echo $term; ?> May someone help? Natasha T I thinking about building a search engine just as a fun project and a way to develop my skills. However I'm not sure where to start... I want it to be a comparison search engine for computer parts so say the user type in "RAM" it would display results from PC World, Ebuyer, etc. I was thinking of using cURL but how do I know where the search results are on each page. For example say I get a html page of results from PC World and store them in a variable how do I then know what to strip out to get the results I want? Would I just have to look at the source code? Also would I then have to work this out for each site I wanted to search from and have a different method of getting the results from search site? If anyone has done anything like this in the past and could give me some advice that would be great. Thanks Hi, I have a script which searches the MySQL database. It outputs the result which is a business name, but I need the business name to be in a link so the user can click on it and go to another page to find out more information. However the business name is not included in the link, but the $value is.
Here is my script
<?php include 'init.php'; // normal search $result_tb = ""; if (!empty($_POST['SEARCH']) && !empty($_POST['search_value'])) { $e = $_POST['search_value']; $query = 'SELECT aquaticCenterName FROM reviews WHERE ' . "aquaticCenterName LIKE '%$e%' OR " . "town LIKE '%$e%' OR " . "county LIKE '%$e%' OR " . "country LIKE '%$e%' OR " . "rating LIKE '%$e%' "; $query_result = $con->query($query); $result_tb = '<div>'; while ($rows = $query_result->fetch_assoc()) { foreach ($rows as $k => $v) { $result_tb .= '<a href="reviewResults.php?aquaticCenterName= . $v . ">' . $v . '</a><br>'; } } $result_tb .='</div><hr>'; $con->close(); } ?> <?php include 'includes/overall/header.php'; include 'includes/logo.php'; ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr> <td> <input type="text" name="search_value" size="30" maxlength="30"/> </td> <td> <input type="submit" name="SEARCH" value="Search"/> </td> </tr> </table> </form> <?php echo $result_tb; ?> <?php include 'includes/overall/footer.php';But when I click on the link, I get this in the address bar reviewResults.php?aquaticCenterName=%20.%20$v%20. How do I get this to read eg, reviewResults.php?aquaticCenterName=PaulsAquaticCenter Many Thanks aquaman Hi, Im trying to do a search engine. However, im facing this errors.. Anyone can help.. Error : Notice: Undefined variable: searching in C:\wamp\www\i-document\s.php on line 17 Thankz in advance. Code: [Select] <h2>Search Form</h2> <form name="search" method="post" action="s.php"> Seach for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="file_name">File Name</option> <Option VALUE="year">Year</option> <Option VALUE="downer">Owner</option> < </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <?php if ($searching =="yes"){ echo "<h3>Search Results</h3><p>"; if ($find == ""){ echo "<p>Please Enter a search term"; exit; } include "config.php"; $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); $query = mysql_query("SELECT * FROM document WHERE upper($field) LIKE'%$find%'"); while($result = mysql_fetch_array($query)){ echo $result['file_ref']; echo " "; echo $result['file_name']; echo "<br>"; echo $result['owner']; echo "<br>"; echo "<br>"; } $matches=mysql_num_rows($query); if ($matches == 0){ echo "Sorry, we can not find an entry to match your query<br><br>"; } echo "<b>Searched For:</b> " .$find; } ?> Ok, I have the code below and it only searches one keyword of my database please could you tell me how I can search multiple keywords? <? /* * search.php * * Script for searching a datbase populated with keywords by the * populate.php-script. */ print "<html><head><title>[Squashy] Search! NOT MESSED UP.</title></head><body>\n"; if( $_POST['keyword'] ) { /* Connect to the database: */ mysql_pconnect("mysql3.000webhost.com","a4580813_dba","censored") or die("ERROR: Could not connect to database!"); mysql_select_db("a4580813_db"); /* Get timestamp before executing the query: */ $start_time = getmicrotime(); /* Execute the query that performs the actual search in the DB: */ $result = mysql_query(" SELECT p.page_url AS url, COUNT(*) AS occurrences FROM page p, word w, occurrence o WHERE p.page_id = o.page_id AND w.word_id = o.word_id AND w.word_word = \"".$_POST['keyword']."\" GROUP BY p.page_id ORDER BY occurrences DESC LIMIT ".$_POST['results'] ); /* Get timestamp when the query is finished: */ $end_time = getmicrotime(); /* Present the search-results: */ print "<h2>[Squashy] Search Results For '".$_POST['keyword']."':</h2>\n"; for( $i = 1; $row = mysql_fetch_array($result); $i++ ) { print "$i. <a href='".$row['url']."'>".$row['url']."</a>\n"; print "(occurrences: ".$row['occurrences'].")<br><br>\n"; } /* Present how long it took the execute the query: */ print "This search took: ".(substr($end_time-$start_time,0,5))." seconds."; } else { /* If no keyword is defined, present the search-page instead: */ print "<form method='post'>[Squashy Search] <input type='text' size='20' name='keyword'>\n"; print "Results: <select name='results'><option value='5'>5</option>\n"; print "<option value='10'>10</option><option value='15'>15</option>\n"; print "<option value='20'>20</option></select>\n"; print "<input type='submit' value='Search [Squashy]'></form>\n"; } print "</body></html>\n"; /* Simple function for retrieving the currenct timestamp in microseconds: */ function getmicrotime() { list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } ?> Thanks Please help Soon! I need some help with a php code I made for a search engine which I will use on my site, but an error code comes up please help me im ok at php but i need some pointers on my code! CODE: Code: [Select] <?php $button = $GET_['submit']; $search = $GET_['search']; if (!$button) echo "Please enter a keyword!"; else { if (strlen($search)<2) echo "search term to short" else { echo "you searched for $search <hr size='1'>" } mysql_connect("ftp.gpcoin.monkeyserve.com","seanhall_seanhall","81834567"); mysql_select_db("seanhall_search"); $search_exploded = explode(" ",$search) foreach($search_exploded as $search_each) { $x++; if ($x==1) $construct .= "keywords LIKE '%$search_each%'"; else $construct .= " OR keywords LIKE '%$search_each%'"; } $construct = "SELECT * FROM searchengine WHERE $construct"; echo $construct; $run = mysql_query($construct); $found = mysql_num_rows($run) if ($found==0) echo "No results Found."; else { echo "$foundnum results found!<p>"; while ($runrows = mysql_fetch_assoc($run)) { $title = $runrows['title']; $desc = $runrows['description']; $url = $runrows['url'] echo " <b>$title</b><br> $desc<br> <a herf='$url'>$url</a><br> "; } } } ?> error: Parse error: syntax error, unexpected T_ELSE, expecting ',' or ';' in /home/seanhall/public_html/search.php on line 11 Help Hi Everyone. Am about to create a search engine for my project using php and mysql. My question is that > Do i have to keep creating tables and entries in the database to contain all the topics to be searched for? Or is there another way to go about it? cos i dont think i can really enter all the search topics and their web address and also their keywords. Thank You. hi frnds need a small favour...... i want to create a page with a simple text box and a submit button and as soon as i click the submit button it should get the data from the database matching to the text what i enterd in the text box and sholud display those data below the text box according to the number of keywords that are matching ..text that matches max keywords should display first...simply to say i need exactly the way google search engine homepage works... Mega thanks in advance pythagoras Hello, I would like to build a php search engine against a mysql database. I am looking for a tutorial but haven't been able to find any good ones. Any ideas of where I can find one? I need one where I can search with several words and if it has the stemmer.class.inc and so on would be great also. thanks, df Good evening, I am a newbie when it comes to programming, and I am yet learning. With several help from this forum, and other websites, I am able to finish my very first PHP project. It worked fine, as well as the search engine. However, it is foreseen that the application will be utilizing huge amount of data which would require pagination when viewing. And so I found a tutorial from php freaks as well. However, after I integrated the codes from my web application, the search engine suddenly stopped working. It is still, when I leave the field blank, which would display all data. However, if I type down a search query on the field, it still displays all data. Can anyone help me on this? Here's my complete code: <link href="add_client.css" rel="stylesheet" type="text/css"> <?PHP include("dbconnection.php"); $query = "SELECT * FROM records"; if(isset($_POST["btnSearch"])) { $query .= " WHERE last_name LIKE '%".$_POST["search"]."%' OR first_name LIKE '%".$_POST["search"]."%'OR territory LIKE '%".$_POST["search"]."%'OR job_title LIKE '%".$_POST["search"]."%'OR title LIKE '%".$_POST["search"]."%'OR employer LIKE '%".$_POST["search"]."%' ORDER BY territory ASC" ; $result = mysql_query($query, $connection) or die(mysql_error()); } ?> <table width="760" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><table width="760" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="199" align="center" valign="top"><a href="login.html"><img src="invent-asia.gif" alt="" width="152" height="58" border="0" /></a> <script type="text/javascript" src="menu.js"></script></td> <td width="176" align="right" valign="bottom"><a href="main.php"><img src="Home.jpg" width="104" height="20" border="0"/></a></td> <td width="130" align="right" valign="bottom"><img src="View.jpg" width="104" height="20" border="0"/></td> <td width="146" align="right" valign="bottom"><a href="add_client.php"><img src="Add.jpg" width="104" height="20" border="0"/></a></td> <td width="109" align="right" valign="bottom"> </td> </tr> </table></td> </tr> <tr> <td><table width="760" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="200" height="3" bgcolor="#1B1C78"><img src="images/topspacerblue.gif" alt="" width="1" height="3" /></td> <td width="560" bgcolor="#0076CC"><img src="images/topspacerlblue.gif" alt="" width="1" height="3" /></td> </tr> </table></td> </tr> <tr> <td height="553" align="center" valign="top" bgcolor="#F3FAFE"><br /> <form name="form" action="view_client.php" method="post"> <br /> <table width="351" border="0"> <tr> <td width="137" align="left" valign="middle">SEARCH RECORD:</td> <td width="144" align="center" valign="middle"><input type="text" name="search" /></td> <td width="56" align="left" valign="middle"><input type="submit" name="btnSearch" value="Search" /></td> </tr> </table> <br /> <table border="0" cellpadding="3" cellspacing="1" bordercolor="38619E" > <tr> <th width="80" align="center" bgcolor="#E0E8F3">Territory</th> <th width="330" align="center" bgcolor="#E0E8F3">Employer</th> <th width="160" align="center" bgcolor="#E0E8F3">Name</th> <th width="80" align="center" valign="middle" bgcolor="#E0E8F3"> </th> </tr> <?php $conn = mysql_connect('localhost','root','') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('invent-asia',$conn) or trigger_error("SQL", E_USER_ERROR); $sql = "SELECT COUNT(*) FROM records"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; $rowsperpage = 10; $totalpages = ceil($numrows / $rowsperpage); if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; } else { $currentpage = 1; } if ($currentpage > $totalpages) { $currentpage = $totalpages; } if ($currentpage < 1) { $currentpage = 1; } $offset = ($currentpage - 1) * $rowsperpage; $sql = "SELECT * FROM records LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); if($result) { for($i=0; $i<mysql_num_rows($result); $i++) { $id = trim(mysql_result($result, $i, "id")); $territory = trim(mysql_result($result, $i, "territory")); $employer = trim(mysql_result($result, $i, "employer")); $first_name = trim(mysql_result($result, $i, "first_name")); $last_name = trim(mysql_result($result, $i, "last_name")); echo "<td>".$territory."</td>"; echo "<td>".$employer."</td>"; echo "<td>".$last_name.", ".$first_name."</td>"; echo "<td><a href='edit_client.php?id=".$id."'>edit</a> | <a href='delete_client.php?id=".$id."'>delete</a> </td>"; echo "</tr>"; } } $range = 3; if ($currentpage > 1) { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; $prevpage = $currentpage - 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { if (($x > 0) && ($x <= $totalpages)) { if ($x == $currentpage) { echo " [<b>$x</b>] "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } } } if ($currentpage != $totalpages) { $nextpage = $currentpage + 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } ?> </table> <p> <br /> </p> </form> <p> </p></td> </tr> <tr> <td height="38"><table width="760" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="200" height="35" align="center" bgcolor="#1B1C78" class=white><img src="images/topspacerblue.gif" alt="" width="1" height="3" /> <a href="disclaimer.html"><font color="#FFFFFF">Legal Disclaimer</font></a> </td> <td width="560" align="center" bgcolor="#0076CC" class=white><img src="images/topspacerlblue.gif" alt="" width="1" height="3" /> Copyright © 2006 - 2010 InventAsia Limited. All rights reserved. </td> </tr> </table></td> </tr> </table> Immediate response is very well appreciated. Thank you very much ^^ Hi, I'm having two issues with my Search engine for my website. It is created to search through my database (it doesn't crawl through pages). The first problem I'm having is when I type in for example: Super Mario Bros and there is a row in my table with a field called Super Mario Brothers this row is not included as a successful result to the search. I need to somehow modify my code to search every word in every table cell in the database. So another example. I have 5 rows, each with one cell, named as follows: One Two Three Four Five If I was to type in the search box: One Two Three Four Five it should display all rows (it obviously doesn't do that right now lol) The second issue has to do with my sql query. It looks like this: $query = "select * from sheets where artist like \"%$trimmed%\" OR title like \"%$trimmed%\" order by artist"; $numresults=mysql_query($query); I need this query to search in the columns artist and title (like it is doing above) AS WELL as search only those rows that have an active status set to 'yes'. I tried to type something like: $query = "select * from sheets where active='yes' && artist like \"%$trimmed%\" OR title like \"%$trimmed%\" order by artist"; but this obviously causes problems. How do i require the query to include rows that are active, but have it look through artist OR title as well? Here is my code below. Any insight appreciated. Will be working on it til someone is available. Thanks <?php include_once('inc/functions.php'); // Get the search variable from URL $var = @mysql_safe($_GET['q']) ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10000; // check for an empty string and display a message. if ($trimmed == "") { $error = "<tr><td colspan='2' style='text-align: center; border-style: solid; border-color: #f43636; background-color: #fe6a6a;'><strong>Type In A Sheet To Search For</strong></td></tr>"; } // check for a search parameter if (!isset($var)) { $error = "<tr><td colspan='2' style='text-align: center; border-style: solid; border-color: #f43636; background-color: #fe6a6a;'><strong>Type In A Sheet To Search For</strong></td></tr>"; } // Build SQL Query $query = "select * from sheets where artist like \"%$trimmed%\" OR title like \"%$trimmed%\" order by artist"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results if ($numrows == 0) { // If search was not found $error = "<tr><td colspan='2' style='text-align: center; border-style: solid; border-color: #f43636; background-color: #fe6a6a;'><strong>Unfortunately that sheet was not found, however, please request it by clicking below</strong></td></tr><tr><td colspan='2' style='text-align: center; border-left-style: solid; border-bottom-style: solid; border-right-style: solid; border-color: #f43636; background-color: #f5f5f5;'><a href='request.php'>Request A Sheet Here</a></td></tr>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s, $limit"; $result = mysql_query($query) or die("Couldn't execute query"); $search = " "; $break = "<br />"; if($var!=""){ $search = "Search:"; $break = ""; } ?> <br /><div id='headsearch'></div> <div style="width: 210px; margin-left: auto; margin-right: auto; text-align: center;"> <form name="form" action="search.php" method="get"> <div style="float: left;"><input type="text" name="q" /></div> <div style="float: right;"><input type="image" src="img/search.png" alt="Search" name="Submit" value="Search" /></div> </form> </div> <?php // display what the person searched for echo "<center><div style='width: 210px; margin-left: auto; margin-right: auto; text-align: center;'>$search <span style='color: #6aa504; margin-left; auto; margin-right: auto;'>" . stripslashes($var) . "</span></div></center>"; ?> <?php // begin to show results set $count = 1 + $s ; $greenboxleft = "greenboxleft"; $greenboxright = "greenboxright"; $grayboxleft = "grayboxleft"; $grayboxright = "grayboxright"; $colorvalue = 0; echo "$break<table width='700px' align='center' style='border-collapse:separate; border-spacing:0px;'><th style='background-color: #93DB70; border-bottom-style: solid; border-color: #6aa504;'>Artist</th><th style='background-color: #93DB70; border-bottom-style: solid; border-color: #6aa504;'>Title</th>"; if($error==""){ // now you can display the results returned while ($row= mysql_fetch_array($result)) { $artist = $row["artist"]; $title = $row["title"]; if(($colorvalue%2)==0){ $styleleft = $greenboxleft; $styleright = $greenboxright; } else{ $styleleft = $grayboxleft; $styleright = $grayboxright; } echo "<tr>"; echo "<td align='center' width='350px' id='$styleleft'><div id='songsboxleft'><strong>". ucwords($row['artist']). "</strong></div></td>"; echo "<td align='center' width='350px' id='$styleright'><div id='songsboxright'><a target='_blank' name='downloadclick' href='download.php?sheet=".$row['url']."&artist=".$row['artist']."&title=".$row['title']."'>" .ucwords($row['title']). "</a></div></td>"; echo "</tr>"; $colorvalue++; } } else{ echo $error; } echo "</table>"; ?> Can anyone please help me with my code. I would like to display the data from another column instead of the id in the url generated by the scirpt. Code: [Select] <?php include('/home/arts/public_html/shows/includes/config.db.php'); if ($link){ $rpp = 10; // results per page $adjacents = 4; $page = intval($_GET["page"]); if(!$page) $page = 1; $reload = $_SERVER['PHP_SELF']; $sql = "select * from craft_shows WHERE venue_state='MD' order by start_date "; $result = mysql_query($sql) or die(mysql_error()); // count total number of appropriate listings: $tcount = mysql_num_rows($result); $date = strftime("%b %d, %Y", strtotime($row['start_date'])); echo "<table class='table7' width='100%' cellpadding='5' cellspacing='1'>"; echo "<tr><td> </td><td>Date</td><td>City</td><td>Show Name</td><td>Attendance</td></tr>"; echo "<tr><td colspan='7'><hr></td></tr>"; // count number of pages: $tpages = ($tcount) ? ceil($tcount/$rpp) : 1; // total pages, last page number $date = strftime("%b %d, %Y", strtotime($row['start_date'])); $count = 0; $i = ($page-1)*$rpp; while(($count<$rpp) && ($i<$tcount)) { mysql_data_seek($result,$i); $row = mysql_fetch_array($result); $id = $row['id']; echo "<tr><td>"; echo "<a href=\"/show_submits/show_detail.php?id=$id\">Details</a>"; echo "</td><td>"; echo strftime('%b %d, %Y', strtotime($row['start_date'])); echo "</td><td>"; echo $row['venue_city']; echo "</td><td>"; echo $row['show_name']; echo "</td><td>"; echo $row['num_visitors']; echo "</td></tr>"; $i++; $count++; } echo "</table>"; echo "<br>"; // call pagination function: include("pagination1.php"); echo paginate_one($reload, $page, $tpages, $adjacents); } ?> I'm in the process of build a network and need to create a search that lets people search by both first and last names. I have tailored the code below to search for first names, but I don't know how to tailor it to search for both first and last names. Code: [Select] <?php //get data $button = $_GET['search']; $search = $_GET['search']; if (!$button) echo "You didn't submit a keyword."; else { if (strlen($search)<1) echo "Search term too short."; else { echo "You searched for $search<hr>"; require('connect.php'); //explode our search term $search_exploded = explode(" ",$search); foreach($search_exploded as $search_each) { //contruct query $x++; if ($x==1) $construct .= "firstname LIKE '%$search_each%'"; else $construct .= " OR firstname LIKE '%$search_each%'"; } //echo out construct $construct = "SELECT * FROM users WHERE $construct"; $run = mysql_query($construct); $foundnum = mysql_num_rows($run); if($foundnum==0) echo "No results found!<p>"; else { echo "$foundnum results found!<p>"; while ($runrows = mysql_fetch_assoc($run)) { //get data $firstname = $runrows['firstname']; $lastname = $runrows['lastname']; echo " $firstname $lastname "; } } } } ?> This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=322620.0 Can someone help me with my code. Used a tutorial as aid but still struggling. Basically I have a table called student. I created a search engine so that if a user searches for either the sname,fname or sno of a student then that student would appear. Apart from some notices I got it working and displaying students when searching for only their sname. Now, I want to be able to make it possible to find a student by searching either sname,fname or sno and I'm getting errors. Can anyone have a look at my code: Code: [Select] <?php //get data $button = $_GET ['submit']; $search = $_GET ['search']; if (!$button) echo "You didn't submit a term."; else { if (strlen($search)<=0) echo "search term too short."; else { echo "You searched for <b>$search</b><hr size='1'>"; include 'includes/config.php'; include 'includes/functions.php'; connect(); //explode search term $search_exploded = explode(" ",$search); foreach($search_exploded as $search_each) { //construct query $x++; //Line 30 if ($x==1) $construct .= "sname,fname,sno LIKE '%$search_each%'"; //Line 32 else $construct .= "OR sname,fname,sno LIKE '%$search_each%'"; } $construct = "SELECT * FROM student WHERE $construct"; $run = mysql_query($construct); $foundnum = mysql_num_rows($run); //Line 44 if ($foundnum==0) echo "No results found."; else { echo"$foundnum results found!<p>"; while ($runrows = mysql_fetch_assoc($run)) { //get data $sname = $runrows['sname']; $fname = $runrows['title']; $sno = $runrows['sno']; echo " <b>$fname</b> $sname $sno"; } } } } ?> Errors:Notice: Undefined variable: x in C:\Program Files\EasyPHP-5.3.3\www\Project\search.php on line 30 Notice: Undefined variable: construct in C:\Program Files\EasyPHP-5.3.3\www\Project\search.php on line 32 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\Project\search.php on line 44 No results found. First time attempting this, so any tips on what I did wrong would be great. Thank you in advance. |