0

but lets talk from the begin. I am creating website similar like wordpres blog index page (can't show picture bescause dont have enough reputation). And then I click read more link in the intro article I want save that article id from the database to php sesions. Here is the code.

                while($row = mysqli_fetch_array($result))
                        {
                          echo '<div class="img">
                            <img src="img/smiley.gif" alt="Smiley face" width="250" height="250">     
                            </div>';
                        echo "<h2>".$row['Name'] . "</h2> " ;
                        $string = strip_tags($row['Content']);

                        if (strlen($string) > 500) {
                            $stringCut = substr($string, 0, 500);
                            $string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="post.php?link=1">Read more</a>'; 
                        }
                        //echo $string;
                        if(isset($_GET['link']) /*you can validate the link here*/){
                                                    $_SESSION['link']= true;
                                                 }
                         echo $string;
                        echo "<br>";

                        echo '<div class="content dashboard clearfix"></div>';
                        echo '<hr>';
                        }
                      mysqli_close($con);
                      ?>

So I have 3 intro articles in index.php file and I whant read one, so I press READ MORE (then I should write article id to session) and go to other page, were I think I should get articles id from session. I am tryyng do it with

if(isset($_GET['link']) ){ /*you can validate the link here*/
    $_SESSION['link']= true;
}

But it always write number 5 the last ID from database, so I think I should use maybe AJAX, javascript?? Maybe some one can give me the example? Thank you.

saravankg
  • 909
  • 1
  • 10
  • 21
piotr
  • 45
  • 1
  • 7

1 Answers1

0

You could set a SESSION variable through ajax but... that'd get pretty insane, making things overly complicated and not very SEO friendly

There's a better method: Make your "read more" actual links to your content. So you've got a "read more" link to http://example.com/page.php?id=5, then inside "page.php" you simply do:

$Id = intval($_GET['id']);

You can make this more pretty after reading how to create friendly URL in php? so they look like http://example.com/page/5.

From your code, you automatically go to the desired page when clicking on the link. Therefore, you only need to create the page post.php and retrieve a single row from the database, in a similar fashion as I indicated above but with the proper name:

$Id = intval($_GET['link']);
Community
  • 1
  • 1
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90