php 图像分页[已结束]

bnl4lu3b  于 2023-02-28  发布在  PHP
关注(0)|答案(1)|浏览(95)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question
分页代码.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”。我有一个问题,图片不显示在页面分页。如何解决这个问题?

jgzswidk

jgzswidk1#

我用以下方法检查了这个问题:

`echo "dir_path: " . $dir_path . "<br>";
 echo "total_images: " . $total_images . "<br>";
 echo "images: ";
 print_r($images);
 echo "<br>";`

它返回:
dir_path: ../offers/ total_images: 0 images: Array ( )

相关问题