views:

185

answers:

5

I want to create a simple CMS with PHP and MySql, I use wamp as a server. Currently I just want it on my localhost.

What I want is to have a front page, where are normal posts like on wordpress, just simple posts, currently I don't want comments maybe later if it works cool :).

And a very simple admin panel where I can manage the posts, with a page where I can see the existing ones and to edit or delete them, or create a new post.

I don't need super high tech javascript tools like in wordpress, it should be really simple.

I have a basic knowledge about PHP, but this is really basic, If I could see this would help a lot on me! I know maybe it's a bit hard question but let it try :)

Best Regards,

George Fist

A: 

You'll need more than basic knowledge of PHP. Learn PHP better (be able to read through the Wordpress source) and also learn MySQL (for storage of data).

aharon
well this answer will not help me :P Maybe I could learn faster in this way, don't u think?
GGF
+1  A: 

If you need a really simple one, you should try learning some simple SQL commands. You should also learn how to use the PHP PDO interfaces to SQL databases. After you've learned those, you should learn how to use sessions in PHP, for the login to the management interface. After you've learned all that, then that's all the basics for a simple one, and you should be able to make one. If you're going to get into making more applications, you might want to look into some frameworks, such as CakePHP or CodeIgniter.

icktoofay
+13  A: 

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] alt text


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 alt text


id:

  • Select for the type INT

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:

  • Select for the type TEXT

Title will be shown on the frontpage, they will link to your inside post, where you can see the title and content together.

Content:

  • Select for the type TEXT

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

CIRK
Wow, you're going down that route? You're either brave, bored or a masochist. ;) Seriously, this answer is much more involved than is appropriate for a SO answer. Not to mention that there are simple tutorials all over the web.
George Marian
Well if I know the answer why not?:D I remember when I learned PHP with my first CMS, I built it in 10 days and It worked I will never forget those days :)
CIRK
Just because it's a long answer that would be painful to answer here. It's also been answered all over the web, which is probably the better reason. That said, by all means, go to town. You'll learn your lesson soon enough. ;)
George Marian
Hmmm.. I made everything what you said, it works, and what should I do now? And lol thanks!
GGF
@GGF, please stay tuned it takes long time to write it! If I'm ready I will post it :)
CIRK
I've created some posts manually from the insert tab from the phpmyadmin :), its similar to what I want but mine has to be much simpler.
GGF
Good! if you created some posts you can test it right now, however I will need to explain some things here before you can use it :)
CIRK
I really would recommend not going the `mysql_*` function route. If you use something like PDO, then it'll be more portable (though not completely portable if you use some non-trivial SQL stuff).
icktoofay
+1, for the effort dude
Starx
@icktoofay let's just keep it simple, he's using `MySQL` now, that's enought for now I think(or sry if I misunderstood something) , thanks very much @Starx
CIRK
@Cirk: Yeah, but I've found PDO to be fairly simple also. It also allows you to use prepared statements and other neat things like that. The plain MySQL driver you're using there does not have those new features AFAIK. For example, you could use a prepared statement for right now what you're doing as `mysql_query("SELECT * FROM posts WHERE id = {$post_id}");`. If you used a prepared statement, then it would eliminate the SQL injection vulnerability that exists in that line. Etc, etc.
icktoofay
Hey @cirk thanks very much, I learned a lot with your help, and yesterday I continued myself, and I read a lots of things so finally I've made myself one! Thanks thanks very much! I really appreciate your work and the effort what you put in this answer :)
GGF
Hey good to know! and good luck in the future ;)
CIRK
A: 

Following the 80%/20% rule, that says rewriting 20% of existing code you can achieve all what you need, there is no sense to start building CMS just from the beginning.

please check some nice open source projects which you can adopt to your needs. it will be faster and cleaner and buggyless solution for you

Dobiatowski
This is not an answer. It's good practice to learn how to create a CMS.
ggfan