【发布时间】:2011-03-19 22:59:23
【问题描述】:
我希望我的网页看起来像这样:http://1x.com/photos/latest-additions/
我几乎只使用 PHP 来镜像它(代码如下)。但是,在 stackoverflow 的人告诉我需要之后,我决定我需要一个数据库,因为从长远来看它会很好。无论如何,所以现在我很茫然,学习 MySQL。我想让我的页面与那个 1x 页面非常相似:按特定顺序包含可点击图片(除了我想使用列表而不是表格,以获得更好的网络语义)和分页。有人可以告诉我如何实现这一目标吗?我不打算成为程序员,我只想知道如何制作布局+分页:[
有人可以帮忙吗? (给我一个脚本,或者告诉我具体要做什么)
哦,还有
这是我的原始代码。这个可以跳过,没必要。
<?php
$rows_per_page = 2;
$cols_per_page = 2;
$image_href = '<a href=/';
$image_links = array('comics/randy>', 'otherstuff/randy>', 'otherstuff/randy>', 'otherstuff/randy>', 'otherstuff/randy>', 'otherstuff/randy>', 'otherstuff/randy>', 'otherstuff/randy>', 'otherstuff/randy>');
$img_srcs = '<img src="https://s3.amazonaws.com/imagetitle/';
$images = array();
for($i = 1; $i < 10; $i++)
{
$images[$i] = $i;
}
$image_ending = '.png" height="200" width="200" /></a>';
$image_break = '<br /><div class="timeago"><div id="submitted">submitted </div>';
$image_descriptions = array('<abbr class="timeago" title="2011-03-13T07:24:17Z"></abbr></div>', '<abbr class="timeago" title="2011-03-13T07:24:17Z"></abbr></div>', '<abbr class="timeago" title="2011-03-13T07:24:17Z"></abbr></div>', '<abbr class="timeago" title="2011-03-13T07:24:17Z"></abbr></div>', '<abbr class="timeago" title="2011-03-13T07:24:17Z"></abbr></div>', '<abbr class="timeago" title="2011-03-13T07:24:17Z"></abbr></div>', '<abbr class="timeago" title="2011-03-13T07:24:17Z"></abbr></div>', '<abbr class="timeago" title="2011-03-13T07:24:17Z"></abbr></div>', '<abbr class="timeago" title="2011-03-13T07:24:17Z"></abbr></div>');
$total_images = count($images);
$images_per_page = $rows_per_page * $cols_per_page;
$total_images = count($images);
$total_pages = ceil($total_images / $images_per_page);
$current_page = (int) $_GET['page'];
if($current_page<1 || $current_page>$total_pages)
{
$current_page = 1;
}
//Get records for the current page
$page_image_links = array_splice($image_links, ($current_page-1)*$images_per_page, $images_per_page);
$page_images = array_splice($images, ($current_page-1)*$images_per_page, $images_per_page);
$page_image_descriptions = array_splice($image_descriptions, ($current_page-1)*$images_per_page, $images_per_page);
$slots = "<table border=\"0\">";
for($row=0; $row<$rows_per_page; $row++)
{
$slots .= "<tr>";
for($col=0; $col<$cols_per_page; $col++)
{
$imgIdx = ($row * $rows_per_page) + $col;
$img = (isset($page_images[$imgIdx])) ? "{$image_href}{$page_image_links[$imgIdx]}{$img_srcs}{$page_images[$imgIdx]}{$image_ending}{$image_break}{$page_image_descriptions[$imgIdx]}" : ' ';
$slots .= "<td class='tables'>$img</td>";
}
$slots .= "</tr>";
}
$slots .= "</table>";
//Create pagination links
$first = "First";
$prev = "Prev";
$next = "Next";
$last = "Last";
if($current_page>1)
{
$prevPage = $current_page - 1;
$first = "<a href=\"w4rmoemfdoiemroifmeromfxdnxvl.php?page=1\">First</a>";
$prev = "<a href=\"w4rmoemfdoiemroifmeromfxdnxvl.php?page={$prevPage}\">Prev</a>";
}
if($current_page<$total_pages)
{
$nextPage = $current_page + 1;
$next = "<a href=\"w4rmoemfdoiemroifmeromfxdnxvl.php?page={$nextPage}\">Next</a>";
$last = "<a href=\"w4rmoemfdoiemroifmeromfxdnxvl.php?page={$total_pages}\">Last</a>";
}
?>
<html>
<title></title>
<head><style type="text/css">
#submitted {color: #888888; font-family:Verdana, Geneva, sans-serif; font-size: .8em; float:left;}
.tables {padding-left: 20px; padding-right: 20px;}
.timeago {color: #888888; font-family:Verdana, Geneva, sans-serif; font-size: .8em; float:right;}
</style><script src="/static/jquery-1.5.1.js" type="text/javascript"></script>
<script src="/static/jquery.timeago.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("abbr.timeago").timeago();
});</script></head>
<body>
<h2>Here are the records for page <?php echo $current_page; ?></h2>
<ul>
<?php echo $slots; ?>
</ul>
Page <?php echo $current_page; ?> of <?php echo $total_pages; ?>
<br />
<?php echo "view more: {$next}"; ?>
</body>
</html>
谢谢!
【问题讨论】:
标签: php mysql sql arrays pagination