zoukankan      html  css  js  c++  java
  • php solutions:创建画廊

    画廊原型如下:

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

    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 id="header">
        <h1>Japan Journey </h1>
    </div>
    <div id="wrapper">
        
        <div id="maincontent">
            <h1>Images of Japan</h1>
            <p id="picCount">Displaying <?php echo $startRow+1;
              if ($startRow+1 < $totalPix) {
                echo ' to ';
                if ($startRow+SHOWMAX < $totalPix) {
                  echo $startRow+SHOWMAX;
                  }
                else {
                  echo $totalPix;
                  }
                }
              echo " of $totalPix";
              ?></p>
            <div id="gallery">
                <table id="thumbs">
                    <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 id="main_image">
                    <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>
  • 相关阅读:
    JAVA入门到精通-第22/23讲-容器、集合类
    JAVA入门到精通-第24讲-容器、集合类
    JAVA入门到精通-第20/21讲-二进制.位运算.位移运算
    JAVA入门到精通-第19讲-多维数组
    JAVA入门到精通-第18讲-排序查找
    JAVA入门到精通-第16讲-数组
    spring demo
    springmvc启动加载指定方法
    Java 日志
    web前端框架
  • 原文地址:https://www.cnblogs.com/youxin/p/2655546.html
Copyright © 2011-2022 走看看