• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

phpsolutions:创建画廊

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

画廊原型如下:

我们想要定义画廊的缩略图为两列。

This is how it works. Let’s say you want two cells in each row. When the first cell is inserted,
the counter is set to 1. If you divide 1 by 2 with the modulo operator (1%2), the result is 1.
When the next cell is inserted, the counter is increased to 2. The result of 2%2 is 0. The
next cell produces this calculation: 3%2, which results in 1; but the fourth cell produces
4%2, which is again 0. So, every time that the calculation results in 0, you know—or to be
more exact, PHP knows—you’re at the end of a row.
So how do you know if there are any more rows left? Each time you iterate through the
loop, you extract the next record into an array called $row. By using is_array(), you can
check whether $row contains the next result. If it does, you add the tags for the next row.
If is_array($row) is false, you’ve run out of records in the result set. Phew . . . let’s try it.

所以定义列用一个计数器和%取模结合起来比较简单。

define('COLS', 2);

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
</body><?php

// define number of columns in table
define('COLS', 2);
// create a connection to MySQL

define('SHOWMAX', 6);
// create a connection to MySQL
$conn=mysqli_connect('localhost','root','sm159357','phpsolutions_gallery') or die("can not connect");// prepare SQL to get total records
$getTotal = 'SELECT COUNT(*) FROM images';
// submit query and store result as $totalPix
$total = mysqli_query($conn,$getTotal);
$row = mysqli_fetch_array($total);
$totalPix = $row[0];

// set the current page
$curPage = isset($_GET['curPage']) ? $_GET['curPage'] : 0;
// calculate the start row of the subset
$startRow = $curPage * SHOWMAX;
// prepare SQL to retrieve subset of image details
$sql = "SELECT * FROM images LIMIT $startRow,".SHOWMAX;
// submit the query (PDO)
$result =mysqli_query($conn,$sql);
// get any error messages
 
// extract the first record as an array
$row = mysqli_fetch_array($result);
// get the name for the main image
if (isset($_GET['image'])) {
  $mainImage = $_GET['image'];
  }
else {
  $mainImage = $row['filename'];
  }
// get the dimensions of the main image
$imageSize = getimagesize('images/'.$mainImage);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
table{
    float:left;
}
    
#main_image{
    margin-left:40px;
    float:left;
     
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Japan Journey
<?php if (isset($title)) {echo "&#8212;{$title}";} ?>
</title>
<link href="assets/journey.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div >
    <h1>Japan Journey </h1>
</div>
<div >
    
    <div >
        <h1>Images of Japan</h1>
        <p >;
          if ($startRow+1 < $totalPix) {
            echo ' to ';
            if ($startRow+SHOWMAX < $totalPix) {
              echo $startRow+SHOWMAX;
              }
            else {
              echo $totalPix;
              }
            }
          echo " of $totalPix";
          ?></p>
        <div >
            <table >
                <tr>
                    <!--This row needs to be repeated-->
                    <?php
                    // initialize cell counter outside loop
                    $pos = 0;
                    do {
                      // set caption if thumbnail is same as main image
                      if ($row['filename'] == $mainImage) {
                         $caption = $row['caption'];
                        }
                     ?>
                    <td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?image=<?php echo $row['filename']; ?>&amp;curPage=<?php echo $curPage; ?>"><img src="images/thumbs/<?php echo $row['filename']; ?>" alt="<?php echo $row['caption']; ?>" width="80" height="54" /></a></td>
                    <?php
                    $row = mysqli_fetch_array($result);
                    // increment counter after next row extracted
                     $pos++;
                      // if at end of row and records remain, insert tags
                    if ($pos%COLS === 0 && is_array($row)) {
                      echo '</tr><tr>';
                      }
                      } while($row);  // end of loop
                      // new loop to fill in final row
                    while ($pos%COLS) {
                      echo '<td>&nbsp;</td>';
                      $pos++;
                      }
                      ?>
                </tr>
                <!-- Navigation link needs to go here -->
                <tr>
                    <td><?php
                    // create a back link if current page greater than 0
                    if ($curPage > 0) {
                      echo '<a href="'.$_SERVER['PHP_SELF'].'?curPage='.($curPage-1).'">&lt; Prev</a>';
                      }
                    // otherwise leave the cell empty
                    else {
                      echo '&nbsp;';
                      }
                     ?>
                    </td>
                    <?php
                    // pad the final row with empty cells if more than 2 columns
                    if (COLS-2 > 0) {
                      for ($i = 0; $i < COLS-2; $i++) {
                        echo '<td>&nbsp;</td>';
                        }
                      }
                    ?>
                    <td><?php
                    // create a forwards link if more records exist
                    if ($startRow+SHOWMAX < $totalPix) {
                      echo '<a href="'.$_SERVER['PHP_SELF'].'?curPage='.($curPage+1).'">Next &gt;</a>';
                      }
                    // otherwise leave the cell empty
                    else {
                      echo '&nbsp;';
                      }
                    ?>
                    </td>
                </tr>
            </table>
            <div >
                <p><img src="images/<?php echo $mainImage; ?>" alt="<?php echo $caption; ?>" <?php echo $imageSize[3]; ?> /></p>
                <p><?php echo $caption; ?></p>
            </div>
        </div>
    </div>
     
</div>
</body>
</html>

</html>

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php对url操作类:url拼接、get获取页面、post获取页面(带传参)发布时间:2022-07-10
下一篇:
如何实现php异步处理发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap