Jump to content

Recommended Posts

Hey guys im new to the site and im having really bad trouble with pagnation :(.

I have paganation on my search results page, i want it to show 5 results a page, i have 6 items in my database with the keyword song, so i search song, page 1 comes up with 5 results as it should BUT their is no page 2 with the 6th results. PLEASE HELP! :(

 

here is my code:

 

<?php
ob_start();
  session_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<title>All About The Music [ Search ]</title>
<style type="text/css">
<!--
body {
background-color: #FF9900;
background-image: url(images/pagebg.png);
}
html {scrollbar-face-color: black;
scrollbar-shadow-color: orange;
scrollbar-highlight-color: orange;
scrollbar-3dlight-color: black;
scrollbar-darkshadow-color: black;
scrollbar-track-color: orange;
scrollbar-arrow-color: orange;
}
div.scroll {
height: 360px;
width: 880px;
overflow: auto;
padding: 8px;
}
.style1 {color: #FFFFFF}
-->
</style> </head>
<body>
<table width="950" border="0" align="right">
  <tr>
    <td><div align="right">
<?php
//include 'top.php';
?>
    </div></td>
  </tr>
</table>
<p> </p>
<p> </p>
<p> </p>
<table width="950" height="592" border="0" align="center">
  <tr>
    <td height="588" background="images/searchbg.png"><table width="900" height="536" border="0" align="center">
      <tr>
        <td colspan="3"><p align="center"><img src="images/header.png" width="421" height="65" /></p>          <form action='search.php' method='GET'>
            <div align="center">
              <input type='text' size='40' name='search' /> 
              <input type='submit' name='submit' value='Search' />
              </div>          
          </form></td>
        </tr>
      <tr>
        <td height="385" colspan="3"><div class='scroll' valign='top'>
          <?php
	  
mysql_connect("localhost","root","");
mysql_select_db("site");

//get data
$button = $_GET['submit'];
$search = $_GET['search'];
$start = $_GET['start'];

$per_page = 5;

$query = mysql_query("SELECT * FROM music ORDER BY ID ASC");
$record_count = mysql_num_rows($query);

if ($record_count >= 1)
  {

  if (!$start)
  $start = 0;

  


  
if (!$button)
echo "<div id='plaintext'>Your didnt submit a keyword</div>";
else
{
    if (strlen($search)<=2)
       echo "<div id='plaintext'>Search term too short.</div>";
    else 
    {
       echo "<div id='plaintext'>You searched for <b>$search</b></div>";
       
        mysql_connect("localhost","root","");
        mysql_select_db("site");

          //explode search term
          $search_exploded = explode(" ",$search);
          
          foreach($search_exploded as $search_each)
          {

            //construct query
            $x++;
            if ($x==1)
              $construct = "SELECT * FROM music WHERE keywords LIKE '%$search_each%' ORDER BY ID DESC LIMIT $start, $per_page";
            else
              $construct = "SELECT * FROM music WHERE OR keywords LIKE '%$search_each%'  ORDER BY ID DESC LIMIT $start, $per_page";

          }


        $run = mysql_query($construct);




        $count_results = mysql_query("SELECT * FROM music WHERE keywords LIKE '%$search_each%' ORDER BY ID DESC");
        $foundnum = mysql_num_rows($count_results);

	$max_pages = ceil($foundnum / $per_page) + 1; //may be a decimal

        if($foundnum==0)
           echo "<div id='plaintext'>No results found.</div>";
             else
             {
               echo "<div id='plaintext'>$foundnum results found!<p><hr COLOR='orange'><p></div>";

              while($runrows = mysql_fetch_assoc($run))
              {
                $name = $runrows['name'];
                $whouploaded = $runrows['uploadedby'];
			$location = $runrows['location'];
                
                echo "<table width='880px' style='border: 1px solid orange; padding: 3px;' border='0'>
			<tr>
			<td width='370px'>
			<div id='plaintext'>$name</div>
			</td>
			<td width='200px'>
			<script language='JavaScript' src='player/audio-player.js'></script>
<object type='application/x-shockwave-flash' data='player/player.swf' id='audioplayer1' height='24' width='200'>
<param name='movie' value='player/player.swf'>
<param name='FlashVars' value='playerID=audioplayer1&soundFile=songs/$location'>
<param name='quality' value='high'>
<param name='menu' value='false'>
<param name='wmode' value='transparent'>
</object>
			</td>
			<td>
			<div id='plaintext'>Uploaded By: $whouploaded</div>
			</td>
			</tr>
			</table><p>";
              }

             }

    }
}

?>
</div></td>
        </tr>
      <tr>
        <td width="262" height="21"><?php
//previous and next variables
$prev = $start - $per_page;
$next = $start + $per_page;

if (!($start<=0))
echo "
<a href='search.php?search=$search&submit=Search&start=$prev'><b>Back</b></a> ";

//show numbers

//set variable for first page
$i=1;

for ($x=0;$x<$max_pages;$x=$x+$per_page)
{
if ($start!=$x)
echo " <a href='search.php?search=$search&submit=Search&start=$x'>$i</a> ";
else
echo " <a href='search.php?search=$search&submit=Search&start=$x'><u><b>$i</b></u></a> ";
$i++;
}

//show next
if (!($start>=$max_pages-$per_page))
echo " <a href='search.php?search=$search&submit=Search&start=$next'><b>Next</b></a> </center>";

}

?></td>
        <td width="421"> </td>
        <td width="262"> </td>
      </tr>
    </table>
    </td>
  </tr>
</table>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/203345-paganation/
Share on other sites

You need at most 2 queries with paganation.  You have way more than that, and are even running queries inside a foreach() loop.  I've changed the page a good bit, you may want to run this script without over writing your current page. 

 

class.pagAnate.php

class pagAnate {

private $numberPerPage;

private $sql;
public $page;
private $count;
private $numberOfPages;
public $PageNav;
public $limit;

function __construct($sql, $perPage, $url = NULL) {
	$this->page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
	$this->numberPerPage = $perPage;
	$this->url = $url;  //sets the first part of the url, ONLY if you are using mod_rewrite.
	$this->sql = $sql;
	$this->getPages();
}

private function getPages() {
	if(isset($this->sql)) {
		$q = mysql_query($this->sql);
			if(mysql_num_rows($q) > 0) {
				$r = mysql_fetch_row($q);
				$this->count = $r[0];				
			}
	}

	if(isset($this->count)) {
		$this->numberOfPages = ceil($this->count/$this->numberPerPage);

	}

	$this->setPageNumber();
}

private function setPageNumber() {
	$this->page = ($this->page > $this->numberOfPages) ? $this->numberOfPages : $this->page;
	$this->page = ($this->page < 1) ? 1 : $this->page;

	$this->setLimit();
}

private function setLimit() {
	$this->limit = 'LIMIT ' .($this->page - 1) * $this->numberPerPage .',' .$this->numberPerPage;

	$this->setNav();
}

private function setNav() {
	$this->url = ($this->url == NULL) ? '?page=' : $this->url . '/page/';
	if($this->count > $this->numberPerPage) {
		$nav = ($this->page == 1) ? ' FIRST    PREV '."\n" : '<a href="' . $this->url . '1">FIRST</a>    <a href="' . $this->url . ($this->page - 1) . '">PREV</a>' . "\n";
		$nav .= ' ( Page ' . $this->page . ' of ' . $this->numberOfPages . ' ) ';
		$nav .= ($this->page == $this->numberOfPages) ? " NEXT    LAST " :  '<a href="' . $this->url . ($this->page + 1) . '">NEXT</a>    <a href="' . $this->url . $this->numberOfPages . '">LAST</a> ';
		$this->PageNav = $nav;
	}
	else {
		$this->PageNav = NULL;
	} 		
}
}

 

Your page

<?php
ob_start();
  session_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<title>All About The Music [ Search ]</title>
<style type="text/css">
<!--
body {



background-color: #FF9900;



background-image: url(images/pagebg.png);
}
html {scrollbar-face-color: black;
scrollbar-shadow-color: orange;
scrollbar-highlight-color: orange;
scrollbar-3dlight-color: black;
scrollbar-darkshadow-color: black;
scrollbar-track-color: orange;
scrollbar-arrow-color: orange;
}
div.scroll {
height: 360px;
width: 880px;
overflow: auto;
padding: 8px;
}
.style1 {color: #FFFFFF}
-->
</style> </head>
<body>
<table width="950" border="0" align="right">
  <tr>
    <td><div align="right">
<?php
//include 'top.php';
?>
    </div></td>
  </tr>
</table>
<p> </p>
<p> </p>
<p> </p>
<table width="950" height="592" border="0" align="center">
  <tr>
    <td height="588" background="images/searchbg.png"><table width="900" height="536" border="0" align="center">
      <tr>
        <td colspan="3"><p align="center"><img src="images/header.png" width="421" height="65" /></p>          <form action='search.php' method='GET'>
            <div align="center">
              <input type='text' size='40' name='search' /> 
              <input type='submit' name='submit' value='Search' />
              </div>          
          </form></td>
        </tr>
      <tr>
        <td height="385" colspan="3"><div class='scroll' valign='top'>
          <?php





  
mysql_connect("localhost","root","");
mysql_select_db("site");
include('class.pagAnate.php');
//get data
$button = $_GET['submit'];
$search = $_GET['search'];
$start = $_GET['start'];
if (!$button)
echo "<div id='plaintext'>Your didnt submit a keyword</div>";
else
{
    if (strlen($search)<=2)
       echo "<div id='plaintext'>Search term too short.</div>";
    else 
    {
       echo "<div id='plaintext'>You searched for <b>$search</b></div>";
       
        mysql_connect("localhost","root","");
        mysql_select_db("site");

          //explode search term
          $search_exploded = explode(" ",$search);
          $construct = 'SELECT * FROM music WHERE';
          foreach($search_exploded as $search_each)
          {
              $keyword[] = " keywords LIKE '%$search_each%'";   

          } 
	  $construct .= implode(' OR ',$keyword) . 'ORDER BY ID DESC';
	  
        $pages = new pagAnate(str_replace('*','COUNT(ID)',$construct),5);
        if($pages->count==0)
           echo "<div id='plaintext'>No results found.</div>";
             else
             {
               echo "<div id='plaintext'>{$pages->count} results found!<p><hr COLOR='orange'><p></div>";
			$run = mysql_query($construct . $pages->limit);
              while($runrows = mysql_fetch_assoc($run))
              {
                $name = $runrows['name'];
                $whouploaded = $runrows['uploadedby'];
$location = $runrows['location'];
                
                echo "<table width='880px' style='border: 1px solid orange; padding: 3px;' border='0'>
<tr>
<td width='370px'>
<div id='plaintext'>$name</div>
</td>
<td width='200px'>
<script language='JavaScript' src='player/audio-player.js'></script>
<object type='application/x-shockwave-flash' data='player/player.swf' id='audioplayer1' height='24' width='200'>
<param name='movie' value='player/player.swf'>
<param name='FlashVars' value='playerID=audioplayer1&soundFile=songs/$location'>
<param name='quality' value='high'>
<param name='menu' value='false'>
<param name='wmode' value='transparent'>
</object>
</td>
<td>
<div id='plaintext'>Uploaded By: $whouploaded</div>
</td>
</tr>
</table><p>";
              }

             }

    }
}

?>
</div></td>
        </tr>
      <tr>
        <td width="262" height="21">
	<?php echo str_replace('page','search=$search&submit=Search&page',$pages->PageNav); ?>
	</td>
        <td width="421"> </td>
        <td width="262"> </td>
      </tr>
    </table>
    </td>
  </tr>
</table>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/203345-paganation/#findComment-1065420
Share on other sites

Number of pages:

$numberOfPages = ceil($record_count / $per_page);

 

Calculate start:

$pageNumber = $_GET['page'];
$startIndex = ($pageNumber - 1) * $per_page;

for ($i = $startIndex; $i < $record_count && $i < ($startIndex + $per_page); ++$i) {
  //..
}

 

@jcbones does that code contain fragments from my Paginator class I posted here a few times?

Link to comment
https://forums.phpfreaks.com/topic/203345-paganation/#findComment-1065422
Share on other sites

I hope not, I wrote that myself about a year ago.  Although, I looked at around 1,000 (it seems) paganate scripts before I got my head wrapped around it.  So if something is similar, I'm sorry.  Not intentional in the least.

 

But, I would like to see it, have a link?

Link to comment
https://forums.phpfreaks.com/topic/203345-paganation/#findComment-1065433
Share on other sites

Ok sorry, i completely didnt understand last night what todo ive done it but i get this error on search.php

 

Parse error: syntax error, unexpected $end in C:\xampp\htdocs\site\scripts\class.pagAnate.php on line 3

 

as u can see the path to class.paganate is different but its include scripts/class.paganate in search.php so its all good.

copying code from here is weird, if you was to code it yourself thur would be loads of lines but as you can see theirs not im going to try a different text editor but please reply for quick fix.

Link to comment
https://forums.phpfreaks.com/topic/203345-paganation/#findComment-1065613
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.