doufeixi6014 2016-03-04 19:42
浏览 43
已采纳

PHP查询,隐藏内容Microsoft sql

I am trying to do some paging to my blog that iam working on, it is working, and now i have added so i can reach each individual post from an url like index.php?postID=1, 2, 3 and so on.

However, one thing is not working as i want it, when i go to a post with that url, the post will load on top, but the 2 other paged posts will also show, so my question is simply:

how do i not load those posts when i just want to go to a single post?

This is what the script looks like now, i know it might be a big mess in a more experienced coders eyes, bare in mind i am a rookie that wants to learn :)

<?php
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
        $rowsPerPage = 2;
        try
        {
            $conn = new PDO( "sqlsrv:server=localhost ; Database=blog", "******", "*****");
            $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        }
        catch(Exception $e)
        { 
            die( print_r( $e->getMessage() ) ); 
        }
        try
        {
            $tsql = "SELECT COUNT(blogID) FROM blog_posts";
            $stmt = $conn->query($tsql);
            $rowsReturned = $stmt->fetch(PDO::FETCH_NUM);
            if($rowsReturned[0] == 0)
            {
                echo "Inga rader hittades.";
            }
            else
            {     
                $numOfPages = ceil($rowsReturned[0]/$rowsPerPage);  
                for($i = 1; $i<=$numOfPages; $i++)     
                {
                    $pageNum = "index.php?pageNum=$i";
                    print("<a href='$pageNum' class='btn btn-primary active btn-sm'>$i</a>&nbsp;&nbsp;");
                }
            }
            $tsql = "SELECT * FROM 
            (SELECT ROW_NUMBER() OVER(ORDER BY blogID DESC) 
               AS RowNumber,
               blog_title,    
               blog_post,
               blog_author,
               blog_category,
               blog_date,
               blogID,
               blog_short
               FROM blog_posts)
AS Temp
WHERE RowNumber BETWEEN ? AND ?";
$stmt2 = $conn->prepare($tsql);

 if (isset($_GET['postID'])){
$value = $_GET['postID'];
$result = "SELECT * FROM blog_posts WHERE blogID='$value'";
$aa =  $conn->query($result);
$post = $aa->fetch(PDO::FETCH_NUM);
if($post[0] == 0)
{
    echo "Inget inlägg med det ID't hittades!";
}
else
{  
echo  "$post[1]"; 
echo "$post[2]";
echo "$post[3]"; 
echo  date_format( new DateTime($post['5']), 'd M Y, H:i' ); 
echo  "$post[4]"; 
echo  "$post[0]";
}
}
if(isset($_GET['pageNum']))
 {
$highRowNum = $_GET['pageNum'] * $rowsPerPage;
$lowRowNum = $highRowNum - $rowsPerPage + 1;
}
else
{
$lowRowNum = 1; 
$highRowNum = $rowsPerPage; 
}
$params = array(&$lowRowNum, &$highRowNum);
$stmt2->execute(array($lowRowNum, $highRowNum));
while($row = $stmt2->fetch(PDO::FETCH_NUM) )
{
echo "$row[1]"
echo "$row[7]";
echo '<a href="?postID='.$row[6].'">Read more</a>'; 
echo  "$row[3]"; 
echo date_format( new DateTime($row['5']), 'd M Y, H:i' ); 
echo  "$row[4]"; 
echo  "$row[6]";
}
}

catch(Exception $e)
{ 
die( print_r( $e->getMessage() ) ); 
}

EDIT:

if (isset($_GET['postID']))
{
$id = $_GET['postID'];
$tsql2 = "SELECT * FROM blog_posts WHERE blogID=':id'";
$stmt3 = $conn->prepare($tsql2);
$stmt3->execute(array(':id' => $id));

while($post = $stmt3->fetch(PDO::FETCH_BOTH) )
{ 

echo $post 1-6 here

  • 写回答

1条回答 默认 最新

  • dtrz17917 2016-03-04 20:27
    关注

    If you want to display only the single post or only paged posts, you should do sth like this:

    if (isset($_GET['postID'])) {
      //single post logic
    } else {
      //paged posts logic
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100