[RESOLVED] another post about search + pagination
Page 1 of 5 123 ... LastLast
Results 1 to 15 of 75

Thread: [RESOLVED] another post about search + pagination

  1. #1
    Senior Member
    Join Date
    Feb 2012
    Posts
    125

    resolved [RESOLVED] another post about search + pagination

    Having a problem with my script. What is happening is when you enter your search term and hit search page refreshes and only displays the search form NO results. I then added a line to check to see if search term ( $var = $_GET['SearchTerm'] ) was being passed from search form to processing script and it was. Also connection to database checked out. I have searched Google and this forum for days which got me as far as i have gotten, but am still not seeing what is wrong. Any help would be wonderful.

    $query is suppose to select column named links and keywords and display data in link column as result. Based on my searching of the script i think the problem is somewhere in the $query any ideas?
    Search Form

    HTML Code:
    <form name="form" action="search.php" method="GET">
    <div align="center">
    <p>
      <input name="SearchTerm" type="text" value="Enter Your Search Term........." size="50" />
      <input type="submit" name="Submit" value="Search" />
    <br>
      Quick Link: View Whole Grain products, click <a href="wg-products.php">here</a></p>
    </div>
    </form>
    and processing script

    PHP Code:

    <?php

      
    // Get the search variable from URL

      
    $var $_GET['SearchTerm'] ;
      echo 
    "This is the search query: "$var//just checking to see if search term is being passed

      
    $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 == "")
      {
      exit;
      }

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

    // Connect to the database.
    require_once ('includes/connections/mysql_nfacts.php'); 

    // Build SQL Query  
    $query "SELECT link, keywords FROM products WHERE keywords like \"%$trimmed%\"  
      order by keywords"


     
    $numresults=mysql_query($query);
     
    $numrows=mysql_num_rows($numresults);

    if (
    $numrows == 0)
      {
      echo 
    "<h4>Results</h4>";
      echo 
    "<p>Sorry, your search: &quot;" $trimmed "&quot; returned zero results.Please try again. If you need help please <a href='../help/search-help.php'>click here.</a></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: &quot;" $var "&quot;</p>";

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

    // now you can display the results returned
      
    while ($rowmysql_fetch_array($result)) {
      
    $title $row["link"];

      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_SELF?s=$prevs&q=$var\">&lt;&lt; 
      Prev 10</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%$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 
    "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
      }

    $a $s + ($limit) ;
      if (
    $a $numrows) { $a $numrows ; }
      
    $b $s ;
      echo 
    "<p>Showing results $b to $a of $numrows</p>";
      }
    ?>
    Last edited by darkangel2001lv; 11-18-2012 at 11:59 PM.

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    All of the code which appears to be processing the results of the SQL query are contained within an if() statement that only evaluates to true if the query returned no rows. In other words, the logic that dictates whether all of your code gets executed doesn't make much sense.

    Also: your code is vulnerable to SQL injection attacks and/or just plain SQL errors. Reference: security.database.sql-injection

    And finally: Stop Using the MySQL Extension!

  3. #3
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    Quote Originally Posted by bradgrafelman View Post
    All of the code which appears to be processing the results of the SQL query are contained within an if() statement that only evaluates to true if the query returned no rows. In other words, the logic that dictates whether all of your code gets executed doesn't make much sense.

    Also: your code is vulnerable to SQL injection attacks and/or just plain SQL errors. Reference: security.database.sql-injection

    And finally: Stop Using the MySQL Extension!
    Ok understand SQL injection, however this script is going on a internal server on a network with no web access so SQL injection is not an issue. Will read up on that tho for my other projects.
    Based on your reply worked with script some and now will display results, however is not passing results pass page 1.
    As well is displaying (code below) reguardless if there is a result or not..... ??
    PHP Code:

    if ($numrows == 0); 
      {
      echo 
    "<h4>Results</h4>";
      echo 
    "<p>Sorry, your search: &quot;" $trimmed "&quot; returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a></p>"
    Full Reworked Script

    PHP Code:


      
    // Get the search variable from URL

      
    $var $_GET['SearchTerm'] ;
      echo 
    "<p>You searched for: &quot;" $var "&quot;";   // display what the person searched for
      
    $trimmed trim($var); //trim whitespace from the stored variable

    // rows to return
    $limit=8

    // check for an empty string and display a message.
    if ($trimmed == "")
      {
      exit;
      }

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

    // Connect to the database.
    require_once ('includes/connections/mysql_nfacts.php'); 

    // Build SQL Query  
    $query "SELECT link, keywords FROM products WHERE keywords like \"%$trimmed%\"  
      order by keywords"


     
    $numresults=mysql_query($query);
     
    $numrows=mysql_num_rows($numresults);

    if (
    $numrows == 0); 
      {
      echo 
    "<h4>Results</h4>";
      echo 
    "Sorry, your search: &quot;" $trimmed "&quot; returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a><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");

    // begin to show results set
    // now you can display the results returned
      
    while ($rowmysql_fetch_array($result)) {
      
    $title $row["link"];

      echo 
    "$title <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 
    "<br /><a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
      Prev 8</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%$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 
    "<br /><a href=\"$PHP_SELF?s=$news&q=$var\">Next 8 &gt;&gt;</a>";
      }

    $a $s + ($limit) ;
      if (
    $a $numrows) { $a $numrows ; }
      
    $b $s ;
      echo 
    "<p><br />Showing results $b to $a of $numrows</p>";
      } 
    Last edited by darkangel2001lv; 11-19-2012 at 02:26 AM.

  4. #4
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    I have spent the whole day searching books i have, web and this forum with not much luck, maybe i am searching for the wrong thing. I have figured out that problem lies with search var not being passed to the other pages but can not see where exactly the problem is. Can anyone shine some light on this ?

    Second issue is stumping me as well why would this echo even tho result results are being displayed. this one i don't know where to begin searching for a solution. Any help would be welcome.
    PHP Code:
    if ($numrows == 0); 
      {
      echo 
    "<h4>Results</h4>";
      echo 
    "<p>Sorry, your search: &quot;" $trimmed "&quot; returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a></p>"

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,723
    Do the other pages use the same code? Is the variable $_GET['q'], or $_GET['SearchTerm']?
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  6. #6
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    Quote Originally Posted by dalecosp View Post
    Do the other pages use the same code? Is the variable $_GET['q'], or $_GET['SearchTerm']?
    variable is $_GET['SearchTerm']

    Here is my setup: Updated Scripts after working with them

    search.php <----- form

    HTML Code:
    <form name="form" action="process.php" method="GET">
    <div align="center">
    <p>
    <input name="SearchTerm" type="text" value="Enter Your Search Term........." size="50" />
    <input type="submit" name="Submit" value="Search" />
    <br>
    Quick Link: View Whole Grain products, click <a href="wg-products.php">here</a></p>
    </div>
    </form>

    and

    page two ( process.php )

    PHP Code:

    <?php

      
    // Get the search variable from URL

      
    $var $_GET['SearchTerm'] ;
      echo 
    "<p>You searched for: &quot;" $var "&quot;";   // display what the person searched for
      
    $trimmed trim($var); //trim whitespace from the stored variable

    // rows to return
    $limit=8

    // check for an empty string and display a message.
    if ($trimmed == "")
      {
      exit;
      }

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

    // Connect to the database.
    require_once ('includes/connections/mysql_nfacts.php'); 

    // Build SQL Query  
    $query "SELECT link, keywords FROM products WHERE keywords like \"%$trimmed%\"  
      order by keywords"


     
    $numresults=mysql_query($query);
     
    $numrows=mysql_num_rows($numresults);

    if (
    $numrows == 0); 
      {
      echo 
    "<h4>Results</h4>";
      echo 
    "Sorry, your search: &quot;" $trimmed "&quot; returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a><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");

    // begin to show results set
    // now you can display the results returned
      
    while ($rowmysql_fetch_array($result)) {
      
    $title $row["link"];

      echo 
    "$title <br />" ;
      
      }

    $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 
    "<br /><a href=\"search.php?s=$prevs&q=$var\">; 
      Prev 8</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 
    "<br /><a href=\"search.php?s=$news&q=$var\">Next 8</a>";
      }

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

  7. #7
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    For one, this:
    PHP Code:
    if ($numrows == 0); 
    is the same as this:
    PHP Code:
    // do absolutely nothing 
    due to the semicolon after the if() statement.

    Second, you still have the same problem I mentioned - all of your code is inside the if() statement that checks if there are ZERO results. So even if you get rid of the semicolon that doesn't belong, your code will execute none of your code after you execute the SQL query and store the number of rows inside $numrows.

  8. #8
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    Quote Originally Posted by bradgrafelman View Post
    For one, this:
    PHP Code:
    if ($numrows == 0); 
    is the same as this:
    PHP Code:
    // do absolutely nothing 
    due to the semicolon after the if() statement.

    Second, you still have the same problem I mentioned - all of your code is inside the if() statement that checks if there are ZERO results. So even if you get rid of the semicolon that doesn't belong, your code will execute none of your code after you execute the SQL query and store the number of rows inside $numrows.
    ok found it and fixed i think is this right?
    PHP Code:
    // Connect to the database.
    require_once ('includes/connections/mysql_nfacts.php'); 

    // Build SQL Query  
    $query "SELECT link, keywords FROM products WHERE keywords like \"%$trimmed%\"  
      order by keywords"


     
    $numresults=mysql_query($query);
     
    $numrows=mysql_num_rows($numresults);

    if (
    $numrows == 0)
      {
      echo 
    "<h4>Results</h4>";
      echo 
    "Sorry, your search: &quot;" $trimmed "&quot; returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a><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"); 
    I get the first 8 results displayed correctly . This output shows me that there are more than 8 results
    PHP Code:
    echo "<p><br />Showing results $b to $a of $numrows</p>"
    When i click link to see the next 8 results the page is empty. I have been searching last two whole days everywhere on how to add pagination, use pagination any thing on pagination and can't figure it out.

    Problem is in the links or the whole pagination part of script, i think.. at this point i am just guessing. Gonna go work at it some more. Any help to get on the right track welcomed.
    Last edited by darkangel2001lv; 11-20-2012 at 04:16 PM.

  9. #9
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    Update:


    found error in links to old searchterm var. Changed that however now when I click to see the next 8 results next page displays the same results not the next 8. hmmmm...


    Revised Script
    PHP Code:
    <?php

      
    // Get the search variable from URL

      
    $var $_GET['SearchTerm'] ;
      echo 
    "<p>You searched for: &quot;" $var "&quot; <br />";   // display what the person searched for
      
    $trimmed trim($var); //trim whitespace from the stored variable

    // rows to return
    $limit=8

    // check for an empty string and display a message.
    if ($trimmed == "")
      {
      exit;
      }

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

    // Connect to the database.
    require_once ('includes/connections/mysql_nfacts.php'); 

    // Build SQL Query  
    $query "SELECT link, keywords FROM products WHERE keywords like \"%$trimmed%\"  
      order by keywords"


     
    $numresults=mysql_query($query);
     
    $numrows=mysql_num_rows($numresults);

    if (
    $numrows == 0)
      {
      echo 
    "<h4>Results</h4>";
      echo 
    "Sorry, your search: &quot;" $trimmed "&quot; returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a><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");

    // begin to show results set
    // now you can display the results returned
      
    while ($rowmysql_fetch_array($result)) {
      
    $title $row["link"];

      echo 
    "$title <br />" ;
      
      }

    $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 
    "<br /><a href=\"search.php?s=$prevs&SearchTerm=$var\">; 
      Prev 8</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 
    "<br /><a href=\"search.php?s=$news&SearchTerm=$var\">Next 8 ;</a>";
      }

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

  10. #10
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,787
    You don't set $s = $_GET['s'] anywhere, so it will always be empty in your if statement.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  11. #11
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,723
    Quote Originally Posted by darkangel2001lv View Post
    Update:


    found error in links to old searchterm var.
    Me, too

    Changed that however now when I click to see the next 8 results next page displays the same results not the next 8. hmmmm...
    PHP Code:
    if (empty($s)) { 
    So, ask yourself ... will $s BE empty? Where is it coming from? I'll bet you can $_GET a clue from that
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  12. #12
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    thanks guys and dalecosp thanks made me laugh needed that. let me try and remember why that darn $s is even there......

  13. #13
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,905
    Quote Originally Posted by darkangel2001lv
    Ok understand SQL injection, however this script is going on a internal server on a network with no web access so SQL injection is not an issue.
    Very dangerous attitude to take, that.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  14. #14
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,723
    Quote Originally Posted by Weedpacket View Post
    Very dangerous attitude to take, that.
    Hee hee, only somewhat related, but it reminds me of a story from the board here, where someone explained how they got their username ...
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  15. #15
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    Quote Originally Posted by dalecosp View Post
    Me, too

    PHP Code:
    if (empty($s)) { 
    So, ask yourself ... will $s BE empty? Where is it coming from? I'll bet you can $_GET a clue from that
    Ok logic tells me that the $s should never be empty, due to it should be the string that came back from $query based on $_GET['SearchTerm'] am i correct? If this is the case found this while searching changed it to what my search var. something like this,


    PHP Code:
    if(isset($_GET['SearchTerm']))
    {
    $s=$_GET['SearchTerm'];
    }
    $string = @$_GET['SearchTerm'] ;
    $trimmed trim($string); 

    if so where do i place it in current script? My thoughts are right at the top of script where

    PHP Code:
    // Get the search variable from URL

      
    $var $_GET['SearchTerm'] ;
      echo 
    "<p>You searched for: &quot;" $var "&quot;";   // display what the person searched for

      
    $trimmed trim($var); //trim whitespace from the stored variable 
    But if i do that then $var is not defined???? Am I on the right path ?

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
  •