Highland3r Posted June 13, 2010 Share Posted June 13, 2010 The code below is for generating my website search engine, the problem im having is i want the variable "what my visitors are looking for to highlight within all the text" so when they hit search it highlights the variable now i have tried loads of diffrent methods but i am unable to intergrate into the script below. the script works fine at the moment with no errors this is only an extra. Thanks for any help offerd. <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database mysql_connect("localhost","username","password"); //(host, username, password) //specify database mysql_select_db("My database") or die("Unable to select database"); //select which database we're using // Build SQL Query $query = "select * from cmsarticles where thearticle like \"%$trimmed%\" order by ID"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; } // 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"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["thearticle"]; $link = $row["REF"]; echo "$count.)$title<a href='$link'>Click here to view full document</a><br><br>"; $count++ ; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/ Share on other sites More sharing options...
Highland3r Posted June 13, 2010 Author Share Posted June 13, 2010 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/#findComment-1071559 Share on other sites More sharing options...
ignace Posted June 13, 2010 Share Posted June 13, 2010 $query = trim($_GET['q']); $query = preg_replace('#[^A-Za-z0-9 ]#', '', $query); if (3 < strlen($query)) {//words with less then 3 characters are considered invalid $query = escape($query); $query = 'SELECT thearticle, REF FROM cmsarticles' . " WHERE thearticle = '$query' OR thearticle LIKE '%$query%'" . ' ORDER BY id'; $result = query($query); if (0 < ($row_count = row_count())) { $page = ctype_digit($_GET['p']) ? intval($_GET['p']) : 1; $items_per_page = 20; $number_of_pages = floor($row_count / $items_per_page); $offset = ($page - 1) * $items_per_page; for ($number = 1, $index = $offset; $index < $row_count && $index < $offset + $items_per_page; ++$number, ++$index) { $thearticle = mysql_result($result, $index, 'thearticle'); $ref = mysql_result($result, $index, 'REF'); echo "$number. <a href=\"$ref\">$thearticle</a><br>\n"; } mysql_free_result($result); echo ((1 < $page) ? '<a href="'.build_url(array('p'=>$page-1)).'">< previous</a>' : '<strong>< previous</strong>'), ' ', (($page < $number_of_pages) ? '<a href="'.build_url(array('p'=>$page+1)).'">next ></a>' : '<strong>next ></strong>'); } } A fun exercise Quote Link to comment https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/#findComment-1071595 Share on other sites More sharing options...
Highland3r Posted June 13, 2010 Author Share Posted June 13, 2010 Code is looking great and much more stable but i am unable to get the code to function now as i am probably not putting the database details in correct? <?php //connect to your database mysql_connect("localhost","web225-content","Password"); //(host, username, password) //specify database mysql_select_db("web225-content") or die("Unable to select database"); //select which database we're using $query = trim($_GET['q']); $query = preg_replace('#[^A-Za-z0-9 ]#', '', $query); if (3 < strlen($query)) { //words with less then 3 characters are considered invalid $query = escape($query); $query = 'SELECT thearticle, REF FROM cmsarticles' . " WHERE thearticle = '$query' OR thearticle LIKE '%$query%'" . ' ORDER BY id'; $result = query($query); if (0 < ($row_count = row_count())) { $page = ctype_digit($_GET['p']) ? intval($_GET['p']) : 1; $items_per_page = 20; $number_of_pages = floor($row_count / $items_per_page); $offset = ($page - 1) * $items_per_page; for ($number = 1, $index = $offset; $index < $row_count && $index < $offset + $items_per_page; ++$number, ++$index) { $thearticle = mysql_result($result, $index, 'thearticle'); $ref = mysql_result($result, $index, 'REF'); echo "$number. <a href=\"$ref\">$thearticle</a><br>\n"; } mysql_free_result($result); echo ((1 < $page) ? '<a href="'.build_url(array('p'=>$page-1)).'">< previous</a>' : '<strong>< previous</strong>'), ' ', (($page < $number_of_pages) ? '<a href="'.build_url(array('p'=>$page+1)).'">next ></a>' : '<strong>next ></strong>'); }} ?> Quote Link to comment https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/#findComment-1071607 Share on other sites More sharing options...
Highland3r Posted June 13, 2010 Author Share Posted June 13, 2010 error is not with conection details they are all correct ther is no error only a blank return when i search any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/#findComment-1071611 Share on other sites More sharing options...
ignace Posted June 14, 2010 Share Posted June 14, 2010 Did you define all missing functions? Or replaced them with the appropriate ones and do these return the expected data? Quote Link to comment https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/#findComment-1071754 Share on other sites More sharing options...
Highland3r Posted June 14, 2010 Author Share Posted June 14, 2010 i am not sure what you mean i am not the best t php, im still learning alot. what functions are missing? This is all my code now minus the search form = 'q' <?php //connect to your database mysql_connect("localhost","web225-content","Password"); //(host, username, password) //specify database mysql_select_db("web225-content") or die("Unable to select database"); //select which database we're using $query = trim($_GET['q']); $query = preg_replace('#[^A-Za-z0-9 ]#', '', $query); if (3 < strlen($query)) { //words with less then 3 characters are considered invalid $query = escape($query); $query = 'SELECT thearticle, REF FROM cmsarticles' . " WHERE thearticle = '$query' OR thearticle LIKE '%$query%'" . ' ORDER BY id'; $result = query($query); if (0 < ($row_count = row_count())) { $page = ctype_digit($_GET['p']) ? intval($_GET['p']) : 1; $items_per_page = 20; $number_of_pages = floor($row_count / $items_per_page); $offset = ($page - 1) * $items_per_page; for ($number = 1, $index = $offset; $index < $row_count && $index < $offset + $items_per_page; ++$number, ++$index) { $thearticle = mysql_result($result, $index, 'thearticle'); $ref = mysql_result($result, $index, 'REF'); echo "$number. <a href=\"$ref\">$thearticle</a><br>\n"; } mysql_free_result($result); echo ((1 < $page) ? '<a href="'.build_url(array('p'=>$page-1)).'">< previous</a>' : '<strong>< previous</strong>'), ' ', (($page < $number_of_pages) ? '<a href="'.build_url(array('p'=>$page+1)).'">next ></a>' : '<strong>next ></strong>'); }} ?> Quote Link to comment https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/#findComment-1071755 Share on other sites More sharing options...
ignace Posted June 14, 2010 Share Posted June 14, 2010 escape(): make that mysql_real_escape_string() query(): make that mysql_query() row_count(): make that mysql_num_rows($result) build_url(): make that index.php?' . http_build_url(array_merge($_GET, array('p' => ..)) Quote Link to comment https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/#findComment-1071778 Share on other sites More sharing options...
Highland3r Posted June 14, 2010 Author Share Posted June 14, 2010 were would i place that ? it looks like the start of the php but i am not sure i am still trying to learn thanks for all the help i really appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/#findComment-1071840 Share on other sites More sharing options...
ignace Posted June 14, 2010 Share Posted June 14, 2010 $db = mysql_connect('localhost', 'web225-content', '..'); if (!$db) { trigger_error('..' . mysql_error(), E_ERROR); header('HTTP/1.0 500 Internal Error'); header('Location: 500.html'); exit(0); } if (!mysql_select_db('web225-content')) { trigger_error('..', E_ERROR); header('HTTP/1.0 500 Internal Error'); header('Location: 500.html'); exit(0); } $query = trim($_GET['q']); $query = preg_replace('#[^A-Za-z0-9 ]#', '', $query); if (3 < strlen($query)) {//words with less then 3 characters are considered invalid $query = mysql_real_escape_string($query, $db); $query = 'SELECT thearticle, REF FROM cmsarticles' . " WHERE thearticle = '$query' OR thearticle LIKE '%$query%'" . ' ORDER BY id'; $result = mysql_query($query, $db); if (0 < ($row_count = mysql_num_rows($result))) { $page = ctype_digit($_GET['p']) ? intval($_GET['p']) : 1; $items_per_page = 20; $number_of_pages = floor($row_count / $items_per_page); $offset = ($page - 1) * $items_per_page; for ($number = 1, $index = $offset; $index < $row_count && $index < $offset + $items_per_page; ++$number, ++$index) { $thearticle = mysql_result($result, $index, 'thearticle'); $ref = mysql_result($result, $index, 'REF'); echo "$number. <a href=\"$ref\">$thearticle</a><br>\n"; } mysql_free_result($result); $previous = array_merge($_GET, array('p' => $page - 1)); $next = array_merge($_GET, array('p' => $page + 1)); echo ((1 < $page) ? '<a href="index.php?'.http_build_query($previous).'">< previous</a>' : '<strong>< previous</strong>'), ' ', (($page < $number_of_pages) ? '<a href="index.php?'.http_build_query($next).'">next ></a>' : '<strong>next ></strong>'); } } Quote Link to comment https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/#findComment-1071923 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.