$x=Mysql_query(‘Select blahblah…’)
While($m=Mysql_fetch_assoc($x))
{
displaying the results
}
Now I want to show 10 results per page and display links link 1 2 3 4 NEXT. How to do??
This post has been edited by sarva842003: 03 June 2011 - 12:09 PM




Posted 03 June 2011 - 12:07 PM
$x=Mysql_query(‘Select blahblah…’)
While($m=Mysql_fetch_assoc($x))
{
displaying the results
}
This post has been edited by sarva842003: 03 June 2011 - 12:09 PM
Posted 03 June 2011 - 12:30 PM
Posted 03 June 2011 - 01:56 PM
RPGonzo, on 03 June 2011 - 03:30 PM, said:
$stmt = $mysqli->prepare("SELECT * FROM table LIMIT ?, ?");
$stmt->bind_params("ii", $begin, $amount);
mysql_query("SELECT * FROM table LIMIT 0, 10"); //10 rows starting from row 0
Posted 03 June 2011 - 02:07 PM
Quote
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
/**
* This is our database settings
*/
$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = 'test';
$password = 'test';
/**
* This is our paging options
*/
$_per_page = 10;
$_numeric_index = true;
$_url_param = 'page';
$_cur_page = isset($_GET[$_url_param]) ? $_GET[$_url_param] : 1; // default on first page
try {
$db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
$query = "SELECT * FROM data";
$prepped = $db->prepare($query);
$prepped->execute();
echo "We pulled (" . $prepped->rowCount() . ") rows from the database.<br/>";
echo "But we only want to show (" . $_per_page . ") per page.<br/>";
if ($prepped->rowCount() > $_per_page) {
// do some match for our low and total pages
$_low_limit = $_per_page * ($_cur_page - 1);
$_total_pages = ceil($prepped->rowCount() / $_per_page);
// modify the query to limit
$query .= " LIMIT " . $_low_limit . "," . $_per_page;
// re prepare and execute the query
$prepped = $db->prepare($query);
$prepped->execute();
// now we compile our paging HTML
$_paging_html = '<div class="paging">';
$_url = $_SERVER['SCRIPT_NAME'] . '?';
// if we have arguments in the URL already we need to preserve them
if (preg_match('/[^\/\?](.*)$/', $_SERVER['QUERY_STRING'], $params)) {
$_url .= $params[0];
// if our paging param is there remove it
$_url = preg_replace('/&' . $_url_param . '=[\d]/', '', $_url);
}
$_link = '<a href="' . $_url . '&' . $_url_param . '=%s" target="_self">%s</a>';
// if we want a numeric index
if ($_numeric_index) {
// now the rest of the pages
for ($i=1; $i<=$_total_pages; $i++) {
// if it's the current page we don't show it
if ($_cur_page == $i) continue;
$_paging_html .= sprintf($_link, $i, $i) . " .. ";
}
} else {
// we just want next and prev buttons
if (($_cur_page - 1) > 0) {
$_paging_html .= sprintf($_link, ($_cur_page - 1), '<- Prev') . "..";
}
if (($_cur_page + 1) <= $_total_pages) {
$_paging_html .= sprintf($_link, ($_cur_page + 1), 'Next ->');
}
}
// both ways we show a 'legend'
$_paging_html .= " Page (" . $_cur_page . ") of (" . $_total_pages . ")";
// close our div
$_paging_html .= "</div>";
}
while ($row = $prepped->fetch(PDO::FETCH_OBJ)) {
echo $row->dbid . "<br/>";
}
echo isset($_paging_html) ? $_paging_html : '';
?>
CREATE TABLE `data` ( `dbid` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`dbid`) ) ENGINE=MyISAM AUTO_INCREMENT=64 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; /*Data for the table `data` */ insert into `data`(`dbid`) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26),(27),(28),(29),(30),(31),(32),(33),(34),(35),(36),(37),(38),(39),(40),(41),(42),(43),(44),(45),(46),(47),(48),(49),(50),(51),(52),(53),(54),(55),(56),(57),(58),(59),(60),(61),(62),(63);
This post has been edited by RPGonzo: 03 June 2011 - 02:32 PM
Posted 03 June 2011 - 06:54 PM
Posted 03 June 2011 - 09:09 PM
satis, on 03 June 2011 - 08:54 PM, said:
This post has been edited by RPGonzo: 03 June 2011 - 09:13 PM
Posted 03 June 2011 - 09:19 PM
This post has been edited by noorahmad: 03 June 2011 - 09:20 PM
Posted 03 June 2011 - 10:03 PM
Posted 04 June 2011 - 05:39 PM
Quote
This post has been edited by RPGonzo: 04 June 2011 - 05:57 PM
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
