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> ");
}
}
$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