How can i limit amount of mysql results per page?

Hi, i need som help with php and mysql. I am creating a search function for my websites, i am using a variable to hold the results of a mysql query, like this: $query = “SELECT * FROM database”. then i am using a while statement to “echo” out the results…

however, i would like to show only 10 results per page, and then have a next button or link to show the next 10 results…

how can this be done? thanks…

[php]
var $PER_PAGE = 10;

function getUserList($page = false) {
$sql_text = // Run your sql query here.
if (!$page) {
$page = 1;
}

$page_start = ($this->PER_PAGE * $page) - $this->PER_PAGE;  
$num_pages = $this->getNumPages($this->getUsersAmount());
if (($page > $num_pages) || ($page < 0)) {  
  $page = 1;
}

$sql_text = $sql_text . " LIMIT ".$page_start.", ".$this->PER_PAGE."";
$data =  // Run your select query here. Using $sql_text
$i=0;
$len = count($data);

$str ='<tr>'."\n";

if ($page > 1)  { 
  $str .= '   <td class="icon_darkbg"><a href="?page='.($page - 1).'"><img src="images/icons/prev.gif" align="absmiddle" alt="" border="0"></a></td>'."\n";
} else {
  $str .= '   <td class="icon_darkbg"><img src="images/icons/prev_na.gif" align="absmiddle" alt="" border="0"></td>'."\n";        
}
if ($page != $num_pages) {
  $str .= '   <td class="icon_darkbg"><a href="?page='.($page + 1).'"><img src="images/icons/next.gif" align="absmiddle" alt="" border="0"></a></td>'."\n";
} else {
  $str .= '   <td class="icon_darkbg"><img src="images/icons/next_na.gif" align="absmiddle" alt="" border="0"></td>'."\n";
}

return $str;
}

function getUsersAmount() {
$rows = Select(“SELECT COUNT(*) as num FROM”; // SQL select here
return $rows[0][“num”];
}

function getNumPages($num_rows) {
if ($num_rows <= $this->PER_PAGE) {
$num_pages = 1;
} else if (($num_rows % $this->PER_PAGE) == 0) {
$num_pages = ($num_rows / $this->PER_PAGE);
} else {
$num_pages = ($num_rows / $this->PER_PAGE) + 1;
}
$num_pages = (int) $num_pages;
return $num_pages;
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service