Results 1 to 4 of 4

Thread: PHP search page

  1. #1
    Join Date
    Apr 2006

    PHP search page

    Hi, I admit I'm a novice at PHP, having a mild form of dyslexia doesn't help, anyway...

    I have borrowed some code for searching my database, which it does the problem is he paging doesn't work, I have searched thru the code but I cant figure it out Im probably being really dumb. heres the code. Many thanks for any help

    HTML Code:
    <form name="form" action="<? $PHP_SELF; ?>" method="get">
      <input type="text" name="q" />
      <input type="submit" name="Submit" value="Search" />
      // Get the search variable from URL
      $var = @$_GET['q'] ;
      $trimmed = trim($var); //trim whitespace from the stored variable
    // rows to return
    // check for an empty string and display a message.
    if ($trimmed == "")
      echo "<p>Please enter a search...</p>";
    // check for a search parameter
    if (!isset($var))
      echo "<p>We dont seem to have a search parameter!</p>";
    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect("localhost","root","mysql"); //(host, username, password)
    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("database") or die("Unable to select database"); //select which database we're using
    // Build SQL Query  
    $query = "select * from table where name like \"%$trimmed%\"  OR othername  like  \"%$trimmed%\"   
      order by field"; // EDIT HERE and specify your table and field names for the SQL query
    // If we have no results, offer a google search as an alternative
    if ($numrows == 0)
      echo "<h4>Results</h4>";
      echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
    // google
     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>";
    // next determine if s has been passed to script, if not use 0
      if (empty($s)) {
    // 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: &quot;" . $var . "&quot;</p>";
    // begin to show results set
    echo "<p>Results  </p>";
    $count = 1 + $s ;
    // now you can display the results returned
      while ($row= mysql_fetch_array($result)) {
       $name = $row["name"];
      $othername = $row["detail"];
      $id = $row["id"];
      echo 	$count .  $name . " <a href=inddex.php?id=" . 
      			$id . " style=text-transform:capitalize; >" . 
      			$name  .   " - " . $othername .  "</a> <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
      print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
      Prev 10</a>&nbsp&nbsp;";
    // calculate number of pages needing links
    // $pages now contains int of pages needed unless there is a remainder from division
      if ($numrows%$limit) {
      // has remainder so add one page
    // check to see if last page
      if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
      // not last page so give NEXT link
      echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
    $a = $s + ($limit) ;
      if ($a > $numrows) { $a = $numrows ; }
      $b = $s + 1 ;
      echo "<p>Showing results $b to $a of $numrows</p>";

  2. #2
    Join Date
    Aug 2006
    Hmm try this, and also post the errors that you get

    PHP Code:
    <form name="form" action="<?php echo $PHP_SELF?>" method="get">
    <input type="text" name="q" />
    <input type="submit" name="Submit" value="Search" />



    // Get the search variable from URL
    $var = @$_GET['q'] ;
    $trimmed trim($var); //trim whitespace from the stored variable

    // rows to return

    // check for an empty string and display a message.
    if ($trimmed == "")
    "<p>Please enter a search...</p>";

    // check for a search parameter
    if (!isset($var))
    "<p>We dont seem to have a search parameter!</p>";

    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect("localhost","root","mysql"); //(host, username, password)

    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("database") or die("Unable to select database"); //select which database we're using

    // Build SQL Query  
    $query "select * from table where name like \"%$trimmed%\"  OR othername  like  \"%$trimmed%\"   
        order by field"
    // EDIT HERE and specify your table and field names for the SQL query


    // If we have no results, offer a google search as an alternative
    if ($numrows == 0)
    "<p>Sorry, your search: &quot;" $trimmed "&quot; returned zero results</p>";

    // google
    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>"

    // next determine if s has been passed to script, if not use 0
    if (empty($s)) {

    // 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: &quot;" $var "&quot;</p>";

    // begin to show results set
    echo "<p>Results  </p>";
    $count $s ;

    // now you can display the results returned
    while ($rowmysql_fetch_array($result)) {
    $name $row["name"];
    $othername $row["detail"];
    $id $row["id"];

    $count .  $name " <a href=index.php?id=" 
    $id " style=text-transform:capitalize; >" 
    $name  .   " - " $othername .  "</a> <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
    "&nbsp;<a href=\"$page?s=$prevs&q=$var\">&lt;&lt; 
            Prev 10</a>&nbsp&nbsp;"

    // calculate number of pages needing links

    // $pages now contains int of pages needed unless there is a remainder from division
    if ($numrows%$limit) {
    // has remainder so add one page

    // check to see if last page
    if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
    // not last page so give NEXT link

    "&nbsp;<a href=\"$page?s=$news&q=$var\">Next 10 &gt;&gt;</a>";

    $a $s + ($limit) ;
    if (
    $a $numrows) { 
    $a $numrows 

    $b $s ;

    "<p>Showing results $b to $a of $numrows</p>";

    Last edited by Phill Pafford; 10-02-2008 at 01:57 PM.

  3. #3
    Join Date
    Sep 2008
    Tel Aviv, Berlin, L.A.

    premature question

    Hi there,

    the code itself is not as error-prone as it looks like.

    However, you should NOT query the database twice, you should NOT evaluate all those $_GET stuff thru 15 lines of code, or so, and you should get rid of that fatal error (division by zero) limit/s, and so on. I'm in the middle of a thing. If it is still not working next time I drop by, I'll send you the code.

    later and best.
    programming is understanding

    my most recent baby The Beatles - Fab Four beatlescomplete.com (now with content!)
    if u need help with utf, watch this first Hebrew/American Chars from same table (not very nice, but a working real life example)

  4. #4
    Join Date
    Apr 2006
    Thanks guyz
    When, i try Phils code it does the same as before, it doesnt show any error messages, when you click the next button that I could give you, it literally does nothing apart from refresh the page and display: ?s=10&q=e after the url in the address bar

    Thanks Im very grateful and know Im a dunce :S

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center