January 10, 200917 yr Morning, I am looking to add a basic website search to a site and wondered if PHP was the way to go? I am thinking about creating a database called 'content' with the following fields: id, page_no, page_title, txt So records could be as follows: 1, 1, 'home', 'some welcome text would go here' 2, 2, 'contact', 'some contact text would go here' 3, 3, 'information', 'information about what we do' Hopefully you get the idea? The search would need to be of every txt column in every row Firstly is PHP the way to go? secondly, how do you do it? and how do you return the results? If someone could help Many thanks Donkeyfourthumbs
January 10, 200917 yr It sounds like a nightmare to maintain by hand that way! Not only will you be creating the pages, but you'll need to update the database too. Perhaps using a CMS which has all the code for the pages in a database already would be better, since it can index and search entire pages easily. Otherwise you might want some form of robot/crawler which can automatically run over all pages and extract the information you want. Typically this would be run as a scheduled task (e.g. a cron job) so it's all automated every day or week for example. After a quick Google search, I couldn't find one, but there probably is one out there. Otherwise you can write one yourself; any programming language you know would be good---while not an obvious choice, you can do it in PHP, use it as a Web page for testing purposes, then use the PHP CLI to execute it in cron. One similar idea I found was GSiteCrawler but it didn't quite offer the features I expected. Regarding the schema, I'm not quite sure why you have "id" and "page_no" which are the same in your example. Isn't page_no unique? If so, why not use it as the primary key and do away with the extra "id" field? Also you might consider adding a keyword field and a description field (from HTML metadata keywords and description) which might speed up searches. The "txt" field could contain the entire text of a crawled page, if you wanted it to.
January 11, 200917 yr Author Connetu_C Thanks again for your advice. Not sure why I was using page_no, so the new table is as follows: id, page_title, page_url, txt And for the most part I have some code that does what I need it to. After a user makes a search it forwards to the results.php page that includes this code: <?php include("search.php"); ?> and here is the code from search.php <?php $var = @$_GET['q']; $trimmed = trim($var); $limit=10; if ($trimmed == "") { echo '<p>'."Please enter a search..."."</p>"; exit; } if (!isset($var)) { echo '<p>'."We dont seem to have a search parameter!"."</p>"; exit; } include("config.php"); $query = "select * from content where txt like \"%$trimmed%\" order BY id asc"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { echo '<p>'."Sorry, your search terms "" . $trimmed . "" returned zero results"."</p>"; echo "<p><a href=\"http://www.google.com/search?q=" . $trimmed . "\" target=\"_blank\" title=\"Look up " . $trimmed . " on Google\">Click here</a> to try the search on Google</p>"; } if (empty($s)) { $s=0; } $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); echo "<p>You searched for: "" . $var . ""</p>"; echo '<p>'."References to your search term can be found on the following pages:"."</p>"; $count = 1 + $s; while ($row= mysql_fetch_array($result)) { $title = '<a href='.$row['page_url'] . ' target="_self">' .$row['page_name'] . '</a>'; echo '<p>'."$count.) $title".'</p>'; $count++; } $currPage = (($s/$limit) + 1); if ($s>=1) { $prevs=($s-$limit); echo '<p>'." <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  ".'</p>'; } $pages=intval($numrows/$limit); if ($numrows%$limit) { $pages++; } if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { $news=$s+$limit; echo '<p>'." <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >>".'</a></p>'; } $a = $s + ($limit); if ($a > $numrows) { $a = $numrows; } $b = $s + 1; echo '<p>'."Showing results $b to $a of $numrows".'</p>'; ?> Like I said, this is all I really need the search to do and is fine apart from a couple of things. If a user enters a blank search the exit; statement stops the results.php page from showing the bottom of the page? And if there are more than 10 records (this is the limit I have set) the link to the next results isn't quite right? Can you help? Cheers Donkeyfourthumbs
Create an account or sign in to comment