Tutorials
Part 6b: Edit entries menu
-
Part 6b: Edit entries menu
Posted on August 1, 2004
Written by
Michelle (view more by Michelle)
Comments (0)
Filed under Build A Blog, Tutorials
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
Comments are closed for this entry.