dqm83011 2014-03-16 20:34
浏览 9
已采纳

单击链接时不显示正确的图像

On my home page I have one image(big) on left side and small box in my right panel where I show random thumb of images on every refresh page. Here is how I show this image:

$rand = mysqli_query($con, "SELECT * from images order by RAND() LIMIT 0,1");
while ($res = mysqli_fetch_assoc($rand))
{
  echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$res['name']."\" /><br />";
  echo "<div id=\"caption\" style=\"text-align:center;\">".$res['caption']."</div><br />";
  echo "<a href=pics.php?id=".$res['id'].">Open in new page &raquo</a>";
}

How to make when someone click on thumb to open that image in new page? This echo "<a href=pics.php?id=".$res['id'].">Open in new window &raquo</a>"; doesn't open the proper image. In fact just refreshing the page but the image is the same.

UPDATE: pics.php

$sql = "SELECT COUNT(*) FROM images";
            $result = mysqli_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
            $r = mysqli_fetch_row($result);
            $numrows = $r[0];

            $rowsperpage = 1;
            $totalpages = ceil($numrows / $rowsperpage);

            if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
                $currentpage = (int) $_GET['currentpage'];
            } else {
                $currentpage = 1;
            }
            if ($currentpage > $totalpages) {
                $currentpage = $totalpages;
            } // end if
            if ($currentpage < 1) {
                $currentpage = 1;
            } // end if

            $offset = ($currentpage - 1) * $rowsperpage;
$sql = "SELECT name, caption FROM images LIMIT $offset, $rowsperpage";
$result = mysqli_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
    while ($list = mysqli_fetch_assoc($result)) {

    echo "<div id=\"picture\">";
    echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$list['name']."\" /></a><br />";
    echo "<div id=\"caption\">".$list['caption']."</div><br />";
} // end while
  • 写回答

1条回答 默认 最新

  • dreamwind1985 2014-03-17 03:33
    关注

    First there is no need to put LIMIT 0,1 Just have

    $rand = mysqli_query($con, "SELECT * from images order by RAND() LIMIT 1");
    $res = mysqli_fetch_assoc($rand);
    
      echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$res['name']."\" /><br   />";
      echo "<div id=\"caption\" style=\"text-align:center;\">".$res['caption']."</div><br />";
      echo "<a href=pics.php?id=".$res['id'].">Open in new page &raquo</a>";
    

    And in you pics.php you must specify the id of the image in your sql query

    $id = abs((int)$_GET['id']);
    $sql = "SELECT name, caption FROM images WHERE id='{$id}'  LIMIT $offset, $rowsperpage";
    $result = mysqli_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
    while ($list = mysqli_fetch_assoc($result)) {
    
    echo "<div id=\"picture\">";
    echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$list['name']."\" /></a><br />";
    echo "<div id=\"caption\">".$list['caption']."</div><br />";
    } // end while
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么