Monday, February 1, 2010

Dynamic website tutorials

This is my first time to create a tutorials in web development and i hope this will help some newbie web developer.

In this article, I used PHP server scripting and mySQL as a data repository. This is only a basic dynamic website. You can edit, add and delete pages.

First, we have to create a database. Since this is only a basic, i will only use one table.

1. Create your database. name it myDB.

2. Create the table. Name it myTABLE and create the fields: id [integer][auto increment], pages [varchar 20], title [varchar 255], body[longtext ], date[varchar 20], and author [varchar 20].

3. Put a dummy data on the fields.
id auto number
pages About
title Company Profile
body This can be a paragraph
date [blank]
author einnox

4. Ok. let’s do the coding. First you have to plan the structure and how you can execute a less work. Using reusable codes is one of them.
Create a header and footer page, and include them in your index page:

Now, create the connection. In this tuturial i will put the connection in a one file called conn.php and save it under include folder.

Folder Structure:
wwwroot:
footer.php
header.php
index.php
/include/conn.php

Now, lets do the connection page. Save it at folder /include

Ohhh that sweet. We have already a connection script where we will user entire the page. We just include the conn.php to the page and call the functions mylogin() and mylogout().

ex:

create a html table here for your header

create a html table here for your footer

We will now create the actual page. Save it as index.php
——————————————————–

<\?php
$myquery = “SELECT pages FROM MyTable order by id asc”;
$myresult = mysql_query($myquery) or die (“Error in query: $myquery. ” . mysql_error());

while($willdisp = mysql_fetch_object($myresult))
{
echo “”;
echo “”;
echo ” $willdisp->pages”;
echo “”;
}
}

?\>

<\?php
$q = “SELECT * FROM MyTable WHERE id = ‘$id’”;
$r = mysql_query($q) or die (“Error in query: $q. ” . mysql_error());

$disp = mysql_fetch_object($r);

if($disp) {
echo “$disp->title ”;
echo “”;

echo “”;
echo nl2br($disp->body);
echo “”;
echo “”;
echo “Last updated:”;
echo formatDate($disp->date);
echo “”;
echo “updated by:”
echo “disp->author”;
echo “”;
}
?\>

———————————————–
dont worry, explanation here

The first part is the connection where we only call the connection script conn.php.Then we open the database using the mylogin(); function.

Then we create a html table where we included the header.php

Then the body. The body consist of a table with two columns. first column hold the navigation script, and the second column holds the contents.

On the navigation column you can find the php scripts where the [index.php?id='$id'] is passing a parameter [$id] to itself. If you have a navigation [Home][Services][products][Contacts] and has the corresponding id’s [1][2][3][4] the script will display the content of navigation when the id parameter will be pass along. Quiet cool, but we have only the index.php which the content are dynamically interchanging everytime the id is changing. This kind of website can hold may navigation link, just add on the database and its automatically display on the index page.

So now? you owe me 5 cent This is calle da spagheti code.

No comments:

Post a Comment