【问题标题】:How do I create a 'picture gallery' with pagination?如何创建带有分页的“图片库”?
【发布时间】: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&nbsp;</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]}" : '&nbsp;';
        $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


    【解决方案1】:

    如果您绝对想自己做这件事,这似乎是合理的,因为它可以让您学习新的东西,请寻找以下资源:
    1.从php连接到mysql数据库表
    2. 从数据库中获取数据到某个变量
    3. 使用查询字符串在页面之间发送数据
    4. 发布并获取
    对于高级的东西,了解发送异步调用和 AJAX

    【讨论】:

      【解决方案2】:

      作为一个程序员,你为什么要自己写代码(甚至请别人为你写)?
      有很多现成的画廊套件,如Menalto GalleryCoppermine Gallery 等。
      这些代码比您从 Stackoverflow 上的志愿者那里获得的草图更专业。

      自己下载一个然后玩玩。我相信它会满足您的所有需求

      【讨论】:

      • 问题是,我需要自定义图库。当我查看代码时,我了解 php 是如何工作的,但我无法终生了解如何开始编码。你知道我的意思?所以,是的,我真的更喜欢从堆栈中寻找草图,而不是专业制作的画廊。
      • @user657847 你知道的,你可以自定义这些画廊。
      • 好吧,您能告诉我创建图片库是否容易?如果我能很好地估计我需要多长时间才能达到这一点,我愿意学习。
      • @user657847 对于我上面提到的好画廊它的年龄。对于小的,这取决于你的技能。对我来说,这就像几个小时。上次我做了一个 30 行代码的小画廊。但对你来说,它需要很长时间才能负担得起。别忘了,你必须同时学习 PHP。即使是像分页这样微不足道的事情对你来说也是一个大问题。还有数百个问题需要解决。
      • 我认为你真的低估了我已经拥有的技能/经验,哈哈。我的意思是,我知道如何制作一个纯粹的画廊,如上图所示。我只是不知道如何将其移至数据库...为什么我需要数据库?如果我有 10000 个 img,最好存储在数据库中,而不是拥有一个包含我所有 img src 和 href 的巨大数组。
      猜你喜欢
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      相关资源
      最近更新 更多