Snippets

Easy way to link to your dynamic pages.

The first code snippet asks the database to get the ‘id’ wich in this case is the primary key and identifier for our rows. We ask the DB to get the latest added id and ad it to the variable $id.

The second code smippet is the actual link to your dynamic site. In this case my blog page on my site. Here we print out the blog link and HTML. Don’t use this html if your links have diffirent html structure. In my case every blog entry have a diffirent ID, wich means the pages will look like this www.bjorngrunde.se/blog?id=”xx” where xx is the uniqe identifier for the blog post.

add the variable to the end of the link and your link always get the latest entry for that page!

<?php
		$dbc = mysqli_connect ('your_host', 'your_user', ''your_pw', 'your_db');
		$sql=("SELECT id FROM yourdatabase ORDER BY id DESC LIMIT 1");
		$result = mysqli_query($dbc, $sql);
		while($row = mysqli_fetch_array($result)) {	
		$id=$row['id'];
		}
?>

//The Link
<?php print ("<li><a href="blog.php?id=" . $id . "">Blog<br /><small>My stories</small></a></li>"); ?>

Share this snippet

 
  • Stauc

    $sql=(“SELECT MAX(id) FROM yourdatabase”);
    $result = mysqli_query($dbc, $sql);
    $row = mysqli_fetch_row($result));
    $id=$row[0];

    better.