[英]how can I paginate images from database using PHP?
I've got this pagination code: 我有以下分页代码:
<?php
$db_username = 'root'; // Your MYSQL Username.
$db_password = ''; // Your MYSQL Password.
$db_name = 'database_name_here'; // Your Database name.
$db_host = 'localhost';
$conDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('Error: Could not connect to database.');
// Pagination Function
function pagination($query,$per_page=10,$page=1,$url='?'){
global $conDB;
$query = "SELECT COUNT(*) as `num` FROM {$query}";
$row = mysqli_fetch_array(mysqli_query($conDB,$query));
$total = $row['num'];
$adjacents = "2";
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$lastlabel = "Last ››";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total/$per_page);
$lpm1 = $lastpage - 1; // //last page minus 1
$pagination = "";
if($lastpage > 1){
$pagination .= "<ul class='pagination'>";
$pagination .= "<li class='page_info'>Page {$page} of {$lastpage}</li>";
if ($page > 1) $pagination.= "<li><a href='{$url}page={$prev}'>{$prevlabel}</a></li>";
if ($lastpage < 7 + ($adjacents * 2)){
for ($counter = 1; $counter <= $lastpage; $counter++){
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
} elseif($lastpage > 5 + ($adjacents * 2)){
if($page < 1 + ($adjacents * 2)) {
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
$pagination.= "<li class='dot'>...</li>";
$pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
$pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";
} elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
$pagination.= "<li class='dot'>..</li>";
$pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
$pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";
} else {
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>..</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
}
}
if ($page < $counter - 1) {
$pagination.= "<li><a href='{$url}page={$next}'>{$nextlabel}</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>{$lastlabel}</a></li>";
}
$pagination.= "</ul>";
}
return $pagination;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Pagination - OTallu.com</title>
<style type="text/css">
/* For this page only */
body { font-family:Arial, Helvetica, sans-serif; font-size:13px; }
.wrap { text-align:center; line-height:21px; padding:20px; }
/* For pagination function. */
ul.pagination {
text-align:center;
color:#829994;
}
ul.pagination li {
display:inline;
padding:0 3px;
}
ul.pagination a {
color:#0d7963;
display:inline-block;
padding:5px 10px;
border:1px solid #cde0dc;
text-decoration:none;
}
ul.pagination a:hover,
ul.pagination a.current {
background:#0d7963;
color:#fff;
}
</style>
</head>
<body>
<div class="wrap">
<?php
$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
if ($page <= 0) $page = 1;
$per_page = 10; // Set how many records do you want to display per page.
$startpoint = ($page * $per_page) - $per_page;
$statement = "`records` ORDER BY `id` ASC"; // Change `records` according to your table name.
$results = mysqli_query($conDB,"SELECT * FROM {$statement} LIMIT {$startpoint} , {$per_page}");
if (mysqli_num_rows($results) != 0) {
// displaying records.
while ($row = mysqli_fetch_array($results)) {
echo $row['name'] . '<br>';
}
} else {
echo "No records are found.";
}
// displaying paginaiton.
echo pagination($statement,$per_page,$page,$url='?');
?>
</div><!-- .wrap -->
</body>
</html>
the result of this pagination code is the following picture: text pagination 此分页代码的结果如下图: 文本分页
but I want it not just show the data from database on the screen but put them in html img tags so the results can be 10 simple pictures per page, NOT the text alone. 但我希望它不仅在屏幕上显示数据库中的数据,而且将它们放在html img标签中,以便结果可以是每页10张简单图片,而不是单独的文本。 take a look at the final result I want(created in photoshop): pagination-2
看一下我想要的最终结果(在photoshop中创建): pagination-2
I'm new with PHP and your help will be invaluable, THANKS. 我是PHP的新手,非常感谢您的帮助。
The problem lies in this statement of while
loop, echo $row['name'] . '<br>';
问题在于
while
循环的语句, echo $row['name'] . '<br>';
echo $row['name'] . '<br>';
. 。 Instead of name , you should fetch image name or complete image path(whatever is there in that column).
除了名称 ,您应该获取图像名称或完整的图像路径(无论该列中有什么)。 Subsequently, you have to use
<img src="..." />
to display the images. 随后,您必须使用
<img src="..." />
显示图像。 So your while
loop would be somewhat like this: 因此,您的
while
循环将如下所示:
while ($row = mysqli_fetch_array($results)) {
echo '<img src="/path/'.$row['ImgName'].'" alt="..." height="..." width="..." />';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.