Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP Pagination

pagination

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 21 July 2013 - 05:28 AM

How do I paginate outputted queries? For example, how would I paginate 10 queries per page?

<?php

$dsn='mysql:host=localhost;dbname=test';
$username="root"; // Mysql username
$password=""; // Mysql password
$tbl_name="testaa"; // Table name

try {
    $db = new PDO($dsn,$username,$password);
} catch (PDOException $e) {
    die('Connection Failed: '.$e->getMessage());
}

$statement = $db->exec("SELECT * FROM $tbl_name");

echo '
    <table class="userList">
        <caption>List data from mysql</caption>
        <thead>
            <tr>
                <th scope="col">Name</th>
                <th scope="col">Lastname</th>
                <th scope="col">Email</th>
                <th scope="col">Update</th>
            </tr>
        </thead><tbody>';
        
while ($row = $statement->fetch()) {
    echo '
            <tr>
                <th scope="row">',$row['name'],'</th>
                <td>',$row['lastname'],'</td>
                <td>',$row['email'],'</td>
                <td><a href="up2.php?id=',$row['id']',">search</a></td>
            </tr>';
}

echo '
        </tbody>
    </table>';
    
?>

Edited by Roger, 21 July 2013 - 11:06 AM.


#2 JasonKnight

JasonKnight

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 312 posts

Posted 22 July 2013 - 02:27 AM

The query command you are looking for is called LIMIT.

/*
	'emulated' SQL prepares are broken buggy *** since they prevent you
	from passing values in prepared queries to LIMIT, and can also open
	possible security flaws, just to support old drivers/SQL databases
	you probably shouldn't be using in the first place, so let's turn that
	garbage off!
*/
$db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false);

$statement = $db->prepare('
	SELECT * FROM '.$tbl_name.'
	LIMIT :start, :count
');
$statement->execute([
	':start' = 30,
	':count' = 10
]);
Would show records 30..39 of the result set. Starting at record 30, show ten of them. It's often handy to pull a count of the result set first so you know what the maximum is -- that way you can set up your pagination links accordingly.
The only thing about Dreamweaver that can be considered professional grade tools are the people promoting it's use.

#3 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 22 July 2013 - 12:15 PM

thanks so much






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download