Jump to content

coding issue? any help apreciated


Highland3r

Recommended Posts

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>&nbsp ";
  }

// 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>";
  
?>

Link to comment
https://forums.phpfreaks.com/topic/204662-coding-issue-any-help-apreciated/
Share on other sites

$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 ;)

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>');  }}
  
?>

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>');  }}
  
?>

$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>');
  }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.