I want to query a database of urls to images and then display them using php. So far I haven't been able to find any examples without using javascript (dont want to use since it loads all the images on the same page and the javascript controls just what you see).
Other pagination examples just simply don't allow you to output the results like this....
image1 image2 image3 image4 image5
image6 image7 image8 image9 image10
1 2 3 4 5 Next
...without doing away with pagination via php and having to hardcode the pagination. Which doesn't make much sense because the images may be more or less at any given time.
Anyone know if this is possible?
For example...
In this script, the loop can only control the amount of records shown per page. I need to be able to break the td after a specified number of records have been output.
<?php
require_once'connect.php';
$query = $db->query("SELECT COUNT(id) FROM table");
$rows = $query->fetchColumn();
$items_per_page = 25;
$last_page = $rows/$items_per_page;
if(isset($_GET['p']) && ctype_digit($_GET['p']) && $_GET['p'] > 0) {
$page = $_GET['p'];
}
else {
$page = '1';
}
if($page > $last_page) {
$page = $last_page;
}
$page_controls = '<p class="page_controls">';
$start = ($page -1) * $items_per_page;
if($items_per_page > $rows) {
$page_controls .= '<span>1</span>';
}
else {
if($page > 1) {
$prev = $page - 1;
$page_controls .= '<a href="?p='. $prev .'">Prev</a>';
for($i = ($page - 3); $i < $page; $i++) {
if($i > 0) {
$page_controls .= '<a href="?p=' . $i .'">' . $i . '</a>';
}
}
}
$page_controls .= '<span>'. $page .'</span>';
if($page < $last_page) {
for($i = $page+1; $i <= $last_page; $i++) {
$page_controls .= '<a href="?p=' . $i . '">' . $i . '</a>';
if($i >= $page+3) {
break;
}
}
$next = $page + 1;
$page_controls .= '<a href="?p=' . $next . '">Next</a>';
}
}
$page_controls .= '</p>';
$query = $db->prepare("SELECT * FROM table limit ?, ?");
$query->bindParam(1, $start, PDO::PARAM_INT);
$query->bindParam(2, $items_per_page, PDO::PARAM_INT);
$query->execute();
echo '<table>';
$cols = 5;
foreach($query->fetchAll(PDO::FETCH_ASSOC) as $data) {
echo '<tr>
<td><a href="' . $data['url'] . '"><img src="' . $data['image_url'] . '" width=200 height=100></a></td>
</tr>';
}
echo '</table>';
echo $page_controls;
?>