Blog Script

The first step will be to setup a table , "blog".

You can dump the structure from blog.sql into your phpMyAdmin if you use it.

CREATE TABLE blog (

id mediumint(5) NOT NULL auto_increment,

title varchar(50) NOT NULL,

name varchar(50) NOT NULL,

email varchar(50) NOT NULL,

date datetime NOT NULL,

icon varchar(255),

entry text NOT NULL,

PRIMARY KEY (ID)

);


This will create the tables required in the MySQL database.

For simplicity, the variables have been named accordingly to their usage:

1. title : field that holds the entry title.

2. name : field that holds the name of the author.

3. email : email of the author.

4. date : date of entry

5. icon : field that holds path to the icon image.

6. entry : field that holds the blog entry

define.php - connection to mysql file:

<?php

$dbsrv = "localhost";

$db = "blog_db";

$user = "root";

$user_password ="root";

$dblnk = mysql_connect($dbsrv,$user,$user_password);

$rez = mysql_select_db($db, $dblnk);

?>

blog_entry.html . Next file , is a simple html file blog_entry.html , no php code is required for it .
It is a form that will enable us to fill blog entries. To minimize programming errors I named the form variables exactly as in mysql table.

<form action="submit.php" method="post">

<table width="130" border="0" cellspacing="1" cellpadding="0">
<tr>

<td height="23" colspan="2"><b>Blog Entry Form</b></td>

</tr>

<tr>

<td>Name:</td>

<td>

<input type="text" name="name" size="30">

</td>

</tr>

<tr>

<td height="25">Email:</td>

<td height="25">

<input type="text" name="email" size="30">

</td>

</tr>

<tr>

<td height="25">Title:</td>

<td height="25">

<input type="text" name="title" size="30">

</td>

</tr>

<tr>

<td height="25">Icon:</td>

<td height="25">

<input type="text" name="icon" size="30">

</td>

</tr>

<tr>

<td height="25">Entry:</td>

<td height="25">

<textarea name="entry" cols="30" rows="5"></textarea>

</td>

</tr>

<tr><td colspan=2><input type="submit" value="submit"></td></tr>

</table></form>

Even this file doesn't require php code , the action of the form will call submit.php . That means when the form is submitted all variables are sent to submit.php using POST method.

submit.php . Submit.php includes "define.php" , database connection file . Then it parses the variables sent using POST method and it checks if the user sent
blank fields. If everything is ok , the values submitted are inserted into the database and a success message is displayed .

<?php

include ("define.php");



$name = $_POST["name"];

$email = $_POST["email"];

$entry = $_POST["entry"];

$title = $_POST["title"];

$icon = $_POST["icon"];



// Check if user submit blank entry

if ($name == "" || $email == "" || $entry == "" || $title == "")

{

die ("You must fill in all fields, please click back and try again.");

}



else {

$q="insert into blog (id,name,title,email,entry,date,icon) VALUES ('','$name','$title','$email','$entry',now(),'$icon')";

$result = mysql_query($q,$dblnk);



if ($result)

{

echo "thank you, blog has been submitted.";

}



}

?>

display.php The last step is to display the blog. First we must decide how many entries to display per page and how you want them to display.

<?php

include ("define.php");



$q = "select * from blog order by date desc ";

$result= mysql_query($q, $dblnk) or die

("Could not execute query : $q." . mysql_error());



// dynamic navigation variables

$rows_per_page=1; // adjust the number here to display number of entries per page

$total_records=mysql_num_rows($result);

$pages = ceil($total_records / $rows_per_page);



$screen = $_GET["screen"];

if (!isset($screen))

$screen=0;

$start = $screen * $rows_per_page;

$q .= "LIMIT $start, $rows_per_page";

$result= mysql_query($q, $dblnk) or die

("Could not execute query : $q." . mysql_error());



while ($row=mysql_fetch_array($result))

{

$id=$row["id"];

$name=$row["name"];

$email=$row["email"];

$entry=$row["entry"];

$date=$row["date"];

$icon=$row["icon"];

$title=$row["title"];



?>



<table width="80%" border="0" cellspacing="1" cellpadding="0">

<tr>

<td><?php echo "$title"; ?></td>

</tr>

<tr>

<td>

<p><img src="<?php echo "$icon"; ?>" alt="icon" align="left"><?php echo "$entry"; ?></p>

<p>Posted by <a href="mailto:<?php echo "$email"; ?>"><?php echo "$name"; ?> on <?php echo "$date"; ?>.</p>

</td>

</tr>

</table>



<div align="center">



<?php

} #end of while



if ($screen > 0) {

$j = $screen - 1;

$url = "display.php?screen=$j";

echo "<a href=\"$url\">Prev</a>";

}



// page numbering links now



for ($i = 0; $i < $pages; $i++) {

$url = "display.php?screen=" . $i;

$j = $i + 1;

echo " | <a href=\"$url\">$j</a> | ";

}



if ($screen < $pages-1) {

$j = $screen + 1;

$url = "display.php?screen=$j";

echo "<a href=\"$url\">Next</a>";

}



?>



</div>


Code used in display.php will display 1 record per page and it will use tables to display the records.

Line 4-6 , database query to obtain all existing records in database . This number will be used to obtain the number of pages by dividing the total number of records to number of records per page (line 11). Number of records per page can be easily modified , because it is determined by the value of variable $rows_per_page from line 9.

For paging control we'll use variable screen that will be sent in url and it will determine which page it is displayed.

Since it is sent in url , it will be obtain using GET , $screen = $_GET["screen"];

If this variable has no content it means we are on the first page so it will be assigned by default value zero.

Depending on which page we are we will use sql option LIMIT using as start record variable start calculated using page number and number of rows per page, and
as number of records needed variable $rows_per_page.

If there is no mysql error variables corresponding to mysql fields are initialized.

Then a html table is created in order to display the records in an organized style.

The last part of this script is where the paging algorithm is implemented. There will be displayed links to previous and next page (if any).

If variable screen has a value greater than zero it means we are not on the first page so at least one previous page must exist . So a "previous" link will be displayed.
Variable screen will be decreased by 1 when when sent in url .

Then we'll use a loop to display all existing pages , and another for loop to display the "next" link . Note , the counter used to display
the "next" link goes only to $screen-1 since the last page won't have a "next" link.

This a basic blog script that can be easily modified adding/removing fields from the initial blog_entry.html file . Of course, these fields should be added in a database as well.

admin – Thu, 2005 – 07 – 07 15:54