Ok I will try to explain it in steps.
Step 1: Creating the database and tables
Go to: http://localhost/phpmyadmin/
There is a panel in front of you, MySQL localhost inside of it Create new database
, fill in with any name, for example test
, then click the create button
.
- Create a database named
test
[+ Enlarge Image]
Now your database is ready to use, now you will see a similar page where you can create tables inside the database, for example users
, posts
, tags
, etc.. Now you will have just posts.
- Create a table named
posts
.
- Write in number of fields
3
.
Inside every table
you can have fields
.
Hit the Go
button and now you are on the field creator page, you can configure now your 3 fields which will be id
, title
, content
.
+ Enlarge Image
id
:
Which means Integer so this will be a number. Every post will have a unique id, check out this webpages URL
, your questions id is 3217340
.
- Select
index
as PRIMARY
.
- Check in
AUTO INCREMENT
.
Title
:
Title will be shown on the frontpage, they will link to your inside post, where you can see the title and content together.
Content
:
This will be shown on the post page
what will be generated by the php I will explain it later when we will be inside PHP
.
- Click
Save
Now on the left sidebar you can see your table named posts
and now you have all the database stuff what you will need for the frontpage, and nothing else you need now in the database.
Step 2: Creating the files
Create 3 files inside your www directory in your wamp:
index.php
admin.php
style.css
Step 3: Creating the index.php
In this file we connect the PHP with the database and pull the data from the posts
table.
Source code:
Click to view source code
$connection = mysql_connect('localhost','root','');
- In this part we create a variable
$connection
which we can use to
test if the mysql_connect
was gone
right or not. So what is
mysql_connect
and what are those
things inside of it? mysql_connect is
a predefined PHP
function which
allows you to connect to a MySQL
database, mysql_connect
, in this
function you can use 3 attributes,
the database, user and
password for the database. The user usually in localhost is root
and the password what you was given
when you was setting up wamp
.
Some refereces:
if(isset($_GET['post'])){
$post_id = $_GET['post'];
$query = mysql_query("SELECT * FROM posts WHERE id = {$post_id}");
$post = mysql_fetch_array($query);
echo "<div class=\"post-info\">";
echo "<a href=\"index.php?post=".$post['id']."\">" . $post['title'] . "</a>";
echo "</div>";
echo "<div class=\"post-info\">";
echo "<p>" . $post['content'] . "</p>";
echo "</div>";
} else {
$query = mysql_query("SELECT * FROM posts");
while($post = mysql_fetch_array($query)){
echo "<div class=\"post-info\">";
echo "<a href=\"index.php?post=".$post['id']."\">" . $post['title'] . >"</a>";
echo "</div>";
}
}
In this part we run an IF
ELSE
statement. the $_GET
is a global variable, with this you can search inside the URL
for example, example.com?post=1, so here we should get the post
and see what's it's value? in this case it's one, so $_GET['post']
= 1, with isset
we set the $_GET
because in the frontpage there will be no ?post
in the URL
and if the PHP can't find it it will return an error such like this: Notice: Undefined index: post in C:\wamp\www\test\index.php on line 37
.
IF
: We have the ?post=1 in the URL then we should see the post page
$post_id = $_GET['post'];
With this we put the value 1
from ?post=1
to the $post_id
variable;
Then we query the database, and select the post WHERE id = the ID what's in the URL so $post_id.
After that we've got an array, we need to fetch that array using the built in mysql_fetch_array
function. As you can see we can specify what we ant to fetch, in this case we fetch $query
.
Now we have a really handy variable $post
. we can pull all data from the posts table in this format:
$post['field name']
, so you have to put there the field from the table. In my example you can see it in action :).
ELSE
: Else we don't have the ?post
in the URL
, so we are on the home page
.
Here we query the whole posts table with all posts from it what we put to variable $query
.
with While
we can pull all the data after the query loop ends, without while
or foreach
we just get 1 result.
So we do the same just with more results.
Step 4: Creating the style.css
Click to view source code
It's just basic CSS nothing to explain on it.
STOPPED BY REQUEST