Trouble With Next Page :(

Discussion in 'PHP' started by scottlpool2003, Mar 12, 2009.

  1. #1
    I can't work it out:

    http://youronlinemovies.net/search.php?q=the

    Showing 1-10 of 42, but the next page doesn't work :S

    Here's the coding:

    <?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 ** EDIT REQUIRED HERE **
    mysql_connect("*****","*******","*******"); //(host, username, password)
    
    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("youronli_onlinemovies") or die("Unable to select database"); //select which database we're using
    
    // Build SQL Query  
    $query = "SELECT * FROM films WHERE id"; // EDIT HERE and specify your table and field names for the SQL query
    
     $numresults=mysql_query($query);
     $numrows=mysql_num_rows($numresults);
    // Build SQL Query  
    $query = "SELECT * FROM films WHERE title LIKE \"%$trimmed%\" order by title"; // EDIT HERE and specify your table and field names for the SQL query
     $numresults=mysql_query($query);
     $numrows=mysql_num_rows($numresults);
    
    // 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)) {
      $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 = 1 + $s ;
    
    // now you can display the results returned
      while ($row= mysql_fetch_array($result)) {
      $title = $row["1st_field"];
      $title = $row["title"];
      $img = $row["img"];
      $id = $row["id"];
      $views =$row["views"];
     $released =$row["released"];
        echo "<table bgcolor='#f2f8f8' width='50%' align='center' style='border-bottom:2px solid #414141;border-left:2px solid #414141;border-top:2px solid #414141;border-right:2px solid #414141;'><tr>";
    	echo "<tr><td align='left' width='60%' style='border-bottom:1px solid silver;'>TITLE</td> <td width='10%' style='border-bottom:1px solid silver;border-left:1px solid silver;' align='center'>VIEWS</font></td><td width='10%' style='border-bottom:1px solid silver;border-left:1px solid silver;' align='center'>RELEASED</font></td><td width='15%' align='CENTER' style='border-bottom:1px solid silver;border-left:1px solid silver;'>PLAY</td></tr>";
    echo "<tr><td width='73%' align='left' style='border-bottom:1px solid silver;'><a href='http://youronlinemovies.net/preview.php?id=$id' title='$title'><img src='http://www.youronlinemovies.net/includes/public/images/movies/$img' width='90px' height='119px' style='border:2px solid #414141;'><br>$title</a></td><td width='10%' style='border-bottom:1px solid silver;border-left:1px solid silver;' align='center'><font color=red> $views</font></td><td width='10%' style='border-bottom:1px solid silver;border-left:1px solid silver;' align='center'><font color=red> $released</font></td><td width='15%' valign='middle' style='border-bottom:1px solid silver;border-left:1px solid silver;'><a href='' title='' style='font-size:12px;color:blue;'><a href='http://youronlinemovies.net/preview.php?id=$id' title='$title'><img src='/play.gif' border='0' height='100px' width=150px' alt=''></a></td></tr>";
        echo "</table>";			
      $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);
      echo "<center>";
      print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
      Prev 10</a>&nbsp&nbsp;";
      echo "</center>";
      }
    
    // 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 + 1 ;
      echo "<p>Showing results $b to $a of $numrows</p>";
      
    ?>
    
    PHP:
    I've been at it for hours and STILL can't work it out!

    Thankyou so much in advance
     
    scottlpool2003, Mar 12, 2009 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    I dont see where you assign $s from the query string. So it will always show the first page.
     
    shallowink, Mar 12, 2009 IP
  3. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #3
    $query .= " limit $s,$limit";
    PHP:
    Come on - what you want to get as an output ?
     
    ActiveFrost, Mar 12, 2009 IP
  4. Gonzo4u

    Gonzo4u Well-Known Member

    Messages:
    410
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Hello scottlpool2003,

    You will need to fetch the offset value of "s" so add this line on top below your other $_GET statement and you are done.

    $s = @$_GET['s'];

    Regards,
    Gonzo
     
    Gonzo4u, Mar 12, 2009 IP
  5. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #5
    Thanks guys, you really don't understand how much I love you hahaha!
     
    scottlpool2003, Mar 12, 2009 IP
  6. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #6
    Sorry I took out a few things you should add back in, but I changed it up to work with pages instead of entry's. I hope you will understand how it works after looking at it.

    <?php
    
      function clean($varvalue)
      {
         
          if (empty($varvalue))
          { $varvalue = null; }
          else
          {
              $varvalue = htmlentities($varvalue, ENT_QUOTES);
              $varvalue = strip_tags($varvalue);
              $varvalue = stripslashes($varvalue);
              $varvalue = str_remove($varvalue,
                          array('SELECT',
                                'UNION',
                                'UPDATE',
                                'DELETE',
                                'WHERE',
                                'films',
                                '\r')
                          );
              $varvalue = trim($varvalue);
          }
          return $varvalue;
      }
    
     
      $limit = 10;
      $page = intval($_GET['page']);
      if (empty($page)) { $page = 0; }
      $onrow = $page * $limit;
      $trimmed = clean($_GET['q']);  
    
    
      $query = "SELECT * FROM `films` WHERE `title` LIKE \"%$trimmed%\" Order by `title` asc";
      $numresults = mysql_query($query);
      $numrows = mysql_num_rows($numresults);
    
      $totalpages = ceil($numrows / $limit);
    
      if($page > $totalpages || $page <= 0) { return; }
    
      if ($numrows == 0) { echo "nothing to show"; return; }
    
      // get results
      $query .= " limit $onrow,$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 and echo "Results"; */
      $count = $onrow;
      
      // now you can display the results returned
      while ($row = mysql_fetch_array($result))
      {
          $title = $row["1st_field"];
          $title = $row["title"];
          $img = $row["img"];
          $id = $row["id"];
          $views = $row["views"];
          $released = $row["released"];
          echo "stuff here";
          $count++;
      }
      
     
      //break before paging
      echo "<br />";
      
      // next we need to do the links to other results
      /* only need to show prev if it is on page 2 or above. not page 1. */
      if ($page >= 2)
      {
          // bypass PREV link if s is 0
          $prevpage = $page - 1;
          print "<center><a href=\"$PHP_SELF?s=$prevpage&q=$var\">&lt;&lt; Prev 10</a>&nbsp&nbsp;</center>";
      }
      
    
      
     
      // check to see if last page
      // example: 40 + 10 = 50 / 10 == 5.. if !(5 == $pages) and $pages are not just 1 page then do.
      if ($page == $totalpages && $pages != 1)
      {
          $nextpage = $page + 1;
          echo "&nbsp;<a href=\"$PHP_SELF?s=$nextpage&q=$var\">Next 10 &gt;&gt;</a>";
      }
      
      $a = $onrow;
      if ($a > $numrows)
      {
          $a = $numrows;
      }
      $b = $onrow + 1;
      echo "<p>Showing results $b to $a of $numrows</p>";
    ?>
    PHP:
     
    exodus, Mar 12, 2009 IP