duanbi1888 2014-01-17 18:40
浏览 53
已采纳

使用next / prev按钮从mysql回显一个图像

So I have upload form wich uploads image to foldar and save name to database(mysql). So far I have this:

<?php

mysqli_query($connection, "SELECT * FROM images LIMIT 1");
$result = mysqli_query($connection, $sql) or die ("SQL Error: ".mysqli_error($connection));

while($row = mysqli_fetch_array($result)){
echo "<div class=\"picture\">";
echo "<p>";

echo "<img src=\"upload/" . $row['filename']  . "\" alt=\"\" /><br />";
echo $row['caption'] . "<br />";
echo "</p>";
echo "</div>";
}
?>

How can I change the code to show me one image at the time.. Like sort of gallery with buttons 'next' and 'prev' to change images? In DB I store only 'id' and 'filename'. Thanks

edit: I made it like this

<?php
if (isset($_GET['id'])) {
 $id = $_GET['id'];
} else {
$id = 1;
$res=mysqli_query($connection, "select * from images order by id ASC LIMIT  $id, 1");
$prevSQL = mysqli_query($connection, "SELECT * FROM images WHERE id > '".$id."' LIMIT  1");
$nextSQL = mysqli_query($connection, "SELECT * FROM images WHERE id < '".$id."' LIMIT 1");

$row = mysqli_fetch_array($res);
if($prevSQL === FALSE) {
    die("SQL Error: " . mysqli_errno($connection));
}
if($nextSQL === FALSE) {
    die("SQL Error: " . mysqli_errno($connection));
}
if(mysqli_num_rows($prevSQL)>1){
$prevRow = mysqli_fetch_array($prevSQL);
$prev = '<a href="show.php?page='.$prevRow['id'].'">Prev</a>';

} else {
$prev = 'Prev';
}
if(mysqli_num_rows($nextSQL)<1){
$nextRow = mysqli_fetch_array($nextSQL);
$next = '<a href="show.php?page='.$nextRow['id'].'">Next</a>';

} else {
$next = 'Next';
}
echo "<img src=\"upload/" . $row['filename']  . "\" alt=\"\" /><br />";
echo $row['caption'] . "<br />";
echo "</p>";
echo "<a href='show.php?page=".$id."'>Next</a>";
echo "<br/>";
echo "<a href='show.php?page=".$id."'>Prev</a>";
echo "</div>";
}
?>
  • 写回答

2条回答 默认 最新

  • du2229 2014-01-20 07:48
    关注

    I make it this way

    $query = "select * from images order by id asc";
    $result = mysqli_query($connection, $query) or die("Query failed: " . mysqli_errno($connection));
    
    $line = mysqli_fetch_array($result, MYSQL_BOTH);
    if (!$line) die('Empty table');
    $previd = -1;
    $currid = $line[0];
    if (isset($_GET['id'])) {
    do {
        $currid = $line[0];
        if ($currid == $_GET['id']) break;
        $previd = $currid;
        $line = mysqli_fetch_array($result, MYSQL_BOTH);
     } while ($line);
    }
    
    if ($line) {
    echo "<div id=\"picture\">";
    echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/" . $line['filename']  . "\" alt=\"\" /><br />";
    echo $line['caption'] . "<br />";
    echo "</p>";
    
    
    } else echo "No records.
    ";
    
    if ($previd > -1) echo '<a href="show.php?id='.$previd.'">Prev | </a>';
    
    $line = mysqli_fetch_array($result, MYSQL_BOTH);
    if ($line) echo '<a href="show.php?id='.$line[0].'">Next </a>';
        echo '<br>';
        echo "</div>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?