您的位置 >>> 星想互联 >>> 编程技术 >>> PHP基础
新闻内容页通用PHP代码
点击数:1736  发布时间2018-05-18 09:10:44
<!DOCTYPE html>
<html lang="en">
<head>
      <meta charset="UTF-8">
      <title>Document</title>
</head>
<body>
      <?php

      $id=$_GET['id'];
      if(!isset($id)){
       header("location:demo.php") ; 
       die();         
      }
      $pdo=new pdo("mysql:host=localhost;dbname=news","root","");
      $pdo->query("set names utf8");

      $sql="修改 news set hit=hit+1 where id={$id}";
      $pdo->query($sql);


      $sql="查询 * from news where id={$id}";
      $stmt=$pdo->query($sql);
      $rs=$stmt->fetch();
      
      echo "<h1>{$rs['title']}</h1>";
      echo "点击数:{$rs['hit']}<br>";
      echo "{$rs['content']}<br><br><br><br><br><br><br><br><br><br><br><br><br><br>";

     //上一篇:
      $sql="查询 * from news where id<{$id} order by id desc limit 0,1";
      $stmt=$pdo->query($sql);
      $rs=$stmt->fetch();

            if($rs){
                  echo "上一篇:<a href='show.php?id={$rs['id']}'>";
                  echo $rs['title']."</a><br>";
            }else{
                 echo "上一篇: 没有上一篇<br>"; 
            }

     //下一篇:
      $sql="查询 * from news where id>{$id} order by id asc limit 0,1";
      $stmt=$pdo->query($sql);
      $rs=$stmt->fetch();

            if($rs){
                  echo "下一篇:<a href='show.php?id={$rs['id']}'>";
                  echo $rs['title']."</a><br>";
            }else{
                 echo "下一篇: 没有下一篇"; 
            }


      ?>

      <h4>最新新闻</h4>
       <?php
       $sql="查询 * from news order by fdate desc limit 0,6";
       $stmt=$pdo->query($sql);
       $rs=$stmt->fetchAll();
       ?>
      <ul>
      <?php foreach($rs as $v){ ?>
        <li><a href="show.php?id=<?php echo $v['id']; ?>"><?php echo $v['title']; ?></a></li>
      <?php } ?>
      </ul>


      <h4>点击排行</h4>
       <?php
       $sql="查询 * from news order by hit desc limit 0,6";
       $stmt=$pdo->query($sql);
       $rs=$stmt->fetchAll();
       ?>
      <ul>
      <?php foreach($rs as $v){ ?>
        <li><a href="show.php?id=<?php echo $v['id']; ?>"><?php echo $v['title']; ?></a></li>
      <?php } ?>
      </ul>

      </body>
</html>
来源:咸宁网站建设