Part 6b: Edit entries menu

Originally posted by Michelle.

Please note: The Build-A-Blog series is an introduction to creating a simple blog script using PHP. These tutorials are meant to help you to learn PHP and MySQL and to use these to fetch and store data and display it on a web page. These tutorials should not be used ‘as is’ on a production website – especially if you are new to PHP and do not understand what you are doing. We would recommend that you try the B-A-B series on a safe, development environment – such as an offline installation of PHP and MySQL – so you can learn how everything works.

GWG and its staffers accept no responsibility for anything that may (or may not) happen to your site or server as a result of you using these tutorials – you do so AT YOUR OWN RISK.

Sorry I forgot to put this in the last entry. Here’s how to write a menu with links to your entries for editing.

Open PHP:

<?php

And connect to your database (replace with your own details):

mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');

Write a query to select the timestamp, id, and title from php_blog, in descending order by id:

$result = mysql_query("SELECT timestamp, id, title FROM php_blog ORDER BY id DESC");

While that is true:

while($row = mysql_fetch_array($result)) {
}

Let’s define our variables. We’ll use $date for the formatted timestamp:

while($row = mysql_fetch_array($result)) {
    $date  = date("l F d Y",$row['timestamp']);
}

And $id and $title:

while($row = mysql_fetch_array($result)) {
    $date  = date("l F d Y",$row['timestamp']);
    $id = $row['id'];
    $title = strip_tags(stripslashes($row['title']));
}

And if the title is 20 or more characters, we’re going to cut it down and add an ellipsis (…):

while($row = mysql_fetch_array($result)) {
    $date  = date("l F d Y",$row['timestamp']);
    $id = $row['id'];
    $title = strip_tags(stripslashes($row['title']));

    if (mb_strlen($title) >= 20) {
        $title = substr($title, 0, 20);
        $title = $title . "...";
    }
    else {
    }
}

Now tell it to print the link. (Remember, it’s linking to update.php?id=xx):

while($row = mysql_fetch_array($result)) {
    $date  = date("l F d Y",$row['timestamp']);
    $id = $row['id'];
    $title = strip_tags(stripslashes($row['title']));

    if (mb_strlen($title) >= 20) {
        $title = substr($title, 0, 20);
        $title = $title . "...";
    }
    print("<a href=\"update.php?id=" . $id . "\">" . $date . " -- " . $title . "</a><br />");
}

Close mysql:

mysql_close();

And close PHP:

?>

And here’s the whole thing:

<?php
mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');

$result = mysql_query("SELECT timestamp, id, title FROM php_blog ORDER BY id DESC");

while($row = mysql_fetch_array($result)) {
    $date  = date("l F d Y",$row['timestamp']);
    $id = $row['id'];
    $title = strip_tags(stripslashes($row['title']));

    if (mb_strlen($title) >= 20) {
        $title = substr($title, 0, 20);
        $title = $title . "...";
    }
    print("<a href=\"update.php?id=" . $id . "\">" . $date . " -- " . $title . "</a><br />");
}

mysql_close();

?>

Comments are closed.