my issue is that I have a database with values like the ones below:\
|Entries|Name|
|12 |Bob |
|14 |Bob |
|15 |Bob |
|27 |Kevin|
|19 |Kevin|
|29 |Dale|
|18 |Dale|
|23 |Dale|
|13 |Dale|
I would like to have pagination where each page lists the entries for each name. So there would be a page for bob, then the next page would have kevins, and then the next would have dales. I know I must use limits to achieve this, but my question is how I would limit each page when the names(Bob,Kevin,Dale) all have different amount of entries.
Below is some php I used in the page where I just wanted 12 entries per page, This may or may not help, I just wanted to add it in here just in case.
$limit = 12;
$pages = ceil($count/$limit);
//check to see if pages is smaller than
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
$offset = ($page-1) * $limit;
$query = "SELECT `actual_quote`,`poster`,`formtype`,`id` FROM `data` WHERE `formtype` = 'Inspirational' LIMIT 13 OFFSET :offset";
$stmt = $db->prepare($query);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
To show the pages you can simply select the distinct names:
$query = "SELECT DISTINCT `Name` FROM `my_table` ORDER BY Name ASC";
$stmt = $db->prepare($query);
$stmt->execute();
$rows = $stmt->fetchAll();
for ($i = 1; $i <= count($rows); $i++) {
$row = $rows[$i - 1];
echo '<a href="?page=' . $i . '&name=' . $row['Name'] . '">Page ' . $i . ' (' . $row['Name'] . ')</a>';
}
On each page you can use the name
parameter to get only the results for that name.
I provide just a sample strategy.
First you need to GROUP BY
your name in your table.
SELECT name, COUNT(*) as cnt
FROM YourTable
GROUP BY name
With this results you know the amount of pages / tabs you need. You need to refer to the names to each page tab. On each page tab you can now select the matching entries for each name:
The first record you get, contains the name 'Kevin'. Then you can do this to select the entries:
SELECT column1, column2, ...
FROM YourTable
WHERE name = 'Kevin'
You need to continue this approach for all different names you get by the base query above.
I hope I could help you.
Use the DISTINCT
keyword which will return the entries without duplicates.
SELECT DISTINCT column_name,column_name
FROM table_name;