0

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

per källström
  • 169
  • 2
  • 11
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php). – Jay Blanchard Mar 04 '16 at 19:46
  • Oh yea, thanks, i will fix that :) – per källström Mar 04 '16 at 19:54
  • @JayBlanchard please have a look at my edit in the post, cant quite get it working as intended, but it should look something like that right? – per källström Mar 04 '16 at 20:39
  • @JayBlanchard Thanks, im on the right way then, however the "$stmt3->execute(array(':id' => $id));" row is causing some issues.. any hints on what im doing wrong there? – per källström Mar 04 '16 at 20:46
  • Just make it `array($id)` since it is a single value. – Jay Blanchard Mar 04 '16 at 20:54
  • @JayBlanchard thanks, but i tried that i get the error Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[22018]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Conversion failed when converting the varchar value ':id' to data type int. – per källström Mar 04 '16 at 20:59
  • Is your id an integer, or a string? – Jay Blanchard Mar 04 '16 at 21:00
  • Ok solved it, stupid me! @JayBlanchard Big thanks for pointing out my flaws and helping me correct them! i appreciate it alot! – per källström Mar 04 '16 at 21:00
  • 1
    `blogID=':id'";` You would have to remove the quotes around the identifier here ¯\\_(ツ)_/¯ – Jay Blanchard Mar 04 '16 at 21:01
  • @JayBlanchard yea i figured hah :) but big thanks once again! – per källström Mar 04 '16 at 21:25

1 Answers1

1

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
}
kolemp
  • 131
  • 6
  • That would kinda solve the problem, however i still want the paging to show, just not the paged pages... that got messy but you might understand what i mean? – per källström Mar 04 '16 at 20:44
  • 1
    So add to your existing code another if statement: **if (isset($_GET['postID'])) { //show paged pages }** instead of condition I gave. This way your pagination navigation will be still visible. – kolemp Mar 04 '16 at 21:13
  • Thanks! it did work as i intended! tought the solution would be alot more complicated! i appreciate your help greatly! – per källström Mar 04 '16 at 21:24