Search page's result

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • th1982
    New Member
    • Feb 2008
    • 51

    Search page's result

    HI All
    I have a search page' s result which view 3results/per page,but my "next" link to view next page is not working.
    Here is my code :


    [PHP]<?php
    // Get the search variable from URL
    $string = @$_GET['q'] ;
    $trimmed = trim($string); //trim whitespace from the stored variable
    // rows to return
    $limit=3;
    // check for an empty string and display a message.
    if ($trimmed == "")
    {
    echo "";
    exit;
    }
    // check for a search parameter
    if (!isset($string ))
    {
    echo "<p>We dont seem to have a search parameter!</p>";
    exit;
    }
    //connect to your database
    mysql_connect(" localhost","pur pasia_CMS","CMS "); //(host, username, password)

    //specify database
    mysql_select_db ("purpasia_VFM" ) or die("Unable to select database"); //select which database we're using

    // Build SQL Query
    $query = "select * from en_c1 where t1 like \"%$trimmed% \"
    order by t1"; // EDIT HERE and specify your table and field names for the SQL query

    $numresults=mys ql_query($query );
    $numrows=mysql_ num_rows($numre sults);

    // If we have no results, offer a google search as an alternative

    if ($numrows == 0)
    {
    echo "<h4>Result s</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>";
    }else

    // 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($qu ery) 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 "Results";
    $count = 1 + $s ;

    // now you can display the results returned
    while ($row= mysql_fetch_arr ay($result)) {
    $title = $row["t1"];

    echo "$count.)&nbsp; $title" ;
    $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 "&nbsp;<a href=\"$PHP_SEL F?s=$prevs&q=$s tring\">&lt;&lt ;
    Prev</a>&nbsp&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%$limi t) {
    // 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 "&nbsp;<a href=\"$PHP_SEL F?s=$news&q=$st ring\">Next&gt; &gt;</a>";
    }

    $a = $s + ($limit) ;
    if ($a > $numrows) { $a = $numrows ; }
    $b = $s + 1 ;
    echo "<p>Showing results $a of $numrows</p>";

    ?>[/PHP]

    Any idea why is this problem?
    Here is the output : http://www.purpleasia.com/clients/VF....php?s=1&q=vfm
    thanks so much
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You do realise the difference between php and html? Use the correct code tags.

    [php]
    <?php
    // Get the search variable from URL
    $string = @$_GET['q'] ;
    $trimmed = trim($string); //trim whitespace from the stored variable
    // rows to return
    $limit=3;
    // check for an empty string and display a message.
    if ($trimmed == "")
    {
    echo "";
    exit;
    }
    // check for a search parameter
    if (!isset($string ))
    {
    echo "<p>We dont seem to have a search parameter!</p>";
    exit;
    }
    //connect to your database
    mysql_connect(" localhost","pur pasia_CMS","CMS "); //(host, username, password)

    //specify database
    mysql_select_db ("purpasia_VFM" ) or die("Unable to select database"); //select which database we're using

    // Build SQL Query
    $query = "select * from en_c1 where t1 like \"%$trimmed% \"
    order by t1"; // EDIT HERE and specify your table and field names for the SQL query

    $numresults=mys ql_query($query );
    $numrows=mysql_ num_rows($numre sults);

    // If we have no results, offer a google search as an alternative

    if ($numrows == 0)
    {
    echo "<h4>Result s</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>";
    }else

    // 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($qu ery) 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 "Results";
    $count = 1 + $s ;

    // now you can display the results returned
    while ($row= mysql_fetch_arr ay($result)) {
    $title = $row["t1"];

    echo "$count.)&nbsp; $title" ;
    $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 "&nbsp;<a href=\"$PHP_SEL F?s=$prevs&q=$s tring\">&lt;&lt ;
    Prev</a>&nbsp&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%$limi t) {
    // 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 "&nbsp;<a href=\"$PHP_SEL F?s=$news&q=$st ring\">Next&gt; &gt;</a>";
    }

    $a = $s + ($limit) ;
    if ($a > $numrows) { $a = $numrows ; }
    $b = $s + 1 ;
    echo "<p>Showing results $a of $numrows</p>";

    ?>
    [/php]

    Comment

    • rpnew
      New Member
      • Aug 2007
      • 189

      #3
      Hi,
      It seems that you are not setting the variable 'S' while reloading the page.
      You need to do something like this.
      [PHP]
      if(isset($_GET['s']))
      {
      $s=$_GET['s'];
      }
      $string = @$_GET['q'] ;
      $trimmed = trim($string);
      [/PHP]

      So even if you are clicking NEXT you are still with s=0.
      Let me know if i'm wrong or if it helps...

      Regards,
      RP

      Comment

      • th1982
        New Member
        • Feb 2008
        • 51

        #4
        Thanks rpnew
        Now it s working
        Pls help me next time if i have question
        Thanks again
        Best regards!!

        Comment

        • rpnew
          New Member
          • Aug 2007
          • 189

          #5
          Originally posted by th1982
          Thanks rpnew
          Now it s working
          Pls help me next time if i have question
          Thanks again
          Best regards!!
          Hi,
          Glad that you got it working.
          Post back to forum whenever you've any problem or questions...

          Regards,
          RP

          Comment

          • anjar666
            New Member
            • Aug 2010
            • 1

            #6
            although this thread has been long but this is very helpful thank you...

            Comment

            Working...