图片分页

问题描述 投票:0回答:0

pagination.php代码:

<?php
// Connect to database
$conn = mysqli_connect('localhost', 'root', '', 'fin_travel');

// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

// Get the total number of images in the folder
$dir_path = '../../offers/';
$images = glob($dir_path . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$total_images = count($images);

// Get the images for the current page
$records_per_page = 25;
$total_pages = ceil($total_images / $records_per_page);

if (isset($_GET['page']) && is_numeric($_GET['page'])) {
  $current_page = $_GET['page'];
} else {
  $current_page = 1;
}

$offset = ($current_page - 1) * $records_per_page;

// Insert the images into the database
for ($i = $offset; $i < $offset + $records_per_page && $i < $total_images; $i++) {
  $name_of_image = basename($images[$i]);
  $file_path = $dir_path . $name_of_image;
  $sql = "INSERT INTO arangements (name_of_image, file_path) VALUES ('$name_of_image', '$file_path')";
  mysqli_query($conn, $sql);
}

// Retrieve the images from the database
$sql = "SELECT file_path FROM arangements LIMIT $offset, $records_per_page";
$result = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_assoc($result)) {
  echo '<img src="' . $row['file_path'] . '" alt="Image">';
  echo "<br>"; // Add this line to separate each image path for easier debugging
}

mysqli_close($conn);
?>

这里是 pocetna.php 的代码,我在其中创建了用于图片分页的 HTML 表单:

<div class = "pagination">
            <form action="pocetna.php" method="GET">
              <div class="image-container">
                <?php
                  // include the pagination.php file
                  require 'sources/pagination.php';

                  /* Five images per row */
                  for ($i = 0; $i < count($images); $i++) {
                    if ($i % 5 == 0) {
                      echo '<div class="row">';
                    }
                    echo '<div class="image">';
                    echo '<img src="./offers/' . $images[$i] . '" alt="Image">';
                    echo '</div>';
                    if (($i + 1) % 5 == 0 || $i == count($images) - 1) {
                      echo '</div>';
                    }
                  }
                ?>
              </div>
              <div class="pagination-controls">
                <button type="submit" name="page" value="<?php echo $current_page - 1; ?>" <?php echo ($current_page == 1) ? 'disabled' : ''; ?>>Previous</button>
                <button type="submit" name="page" value="<?php echo $current_page + 1; ?>" <?php echo ($current_page == $total_pages) ? 'disabled' : ''; ?>>Next</button>
              </div>
            </form>
          </div>

这是文件夹“offers”的路径,我在其中存储了 5 张图片 -> “C:\xampp\htdocs\FIN Travel\FIN-Travel-main”,pagination.php -> “C:\xampp\htdocs\FIN Travel \FIN-Travel-main\sources" 和 pocetna.php -> "C:\xampp\htdocs\FIN Travel\FIN-Travel-main"。我有一个问题,图片没有显示在分页页面中。如何解决这个问题?

php pagination
© www.soinside.com 2019 - 2024. All rights reserved.