画廊原型如下:

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 >
    <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>

 

相关文章:

  • 2021-06-28
  • 2021-08-20
  • 2021-11-20
  • 2022-01-25
  • 2022-01-17
  • 2021-08-14
猜你喜欢
  • 2021-06-12
  • 2021-05-30
  • 2022-12-23
  • 2021-07-07
  • 2021-12-06
  • 2022-01-15
相关资源
相似解决方案