+ Reply to Thread
Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By alejandroangulo32
  • 1 Post By vv.bbcc19

Thread: Creating a Simple CMS [PHP + MySQL ?]

  1. #1
    alejandroangulo32 is offline x10Hosting Member alejandroangulo32 is an unknown quantity at this point
    Join Date
    Oct 2010
    Posts
    4

    Creating a Simple CMS [PHP + MySQL ?]

    I wanted to create a simple CMS but I'm not sure how to start off. I've started working on the MySQL database. All I have right now is a table titled posts.

    Here's the output of describe posts;

    Code:
    +-----------+-------------+------+-----+-------------------+-----------------------------+
    | Field     | Type        | Null | Key | Default           | Extra                       |
    +-----------+-------------+------+-----+-------------------+-----------------------------+
    | id        | int(11)     | NO   | PRI | NULL              | auto_increment              |
    | title     | varchar(80) | YES  |     | Untitled          |                             |
    | author    | varchar(50) | YES  |     | Anonymous         |                             |
    | content   | text        | NO   |     | NULL              |                             |
    | timestamp | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
    +-----------+-------------+------+-----+-------------------+-----------------------------+
    I was wondering if this was a good way to start off my database. What other tables will I have to create? I plan on creating a user system, but for now I want to create a barebones CMS.

    I can figure out how to write the PHP on my own, but I'm not sure about the MySQL.

    I'm not planning on using a WYSIWYG editor, instead I'll just write files by hand and insert them into the database. I was also wondering how I should handle images? Should I just create a directory for them, or insert them into the database, or insert links in the database to the path of the actual image?

    I hope I'm not asking for too much. I don't need to have my hand held, I'd just like a push in the right direction.

    Thanks in advance.
    dinomirt96 likes this.

  2. #2
    MaestroFX1's Avatar
    MaestroFX1 is offline Community Advocate MaestroFX1 has a spectacular aura about
    Join Date
    Feb 2008
    Location
    Area 51
    Posts
    1,577

    Re: Creating a Simple CMS [PHP + MySQL ?]

    Hello

    Any special reason to do so ? Is it some sort of experiment that you are doing or something like that?
    I mean you can always take the easier way out by using a CMS like wordpress or drupal

    --------
    I was also wondering how I should handle images? Should I just create a directory for them, or insert them into the database, or insert links in the database to the path of the actual image?
    --------
    Avoid storing images in mysql databases.Links to location should be the preffered solution.

  3. #3
    alejandroangulo32 is offline x10Hosting Member alejandroangulo32 is an unknown quantity at this point
    Join Date
    Oct 2010
    Posts
    4

    Re: Creating a Simple CMS [PHP + MySQL ?]

    Yeah, I'm just experimenting. WP and Drupal are bloated for me, I just need something simple. Besides, I think coding is sorta fun.

    So any noob mistakes I should avoid, like forgetting to sanitize queries?

  4. #4
    creastery is offline x10Hosting Member creastery is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    16

    Re: Creating a Simple CMS [PHP + MySQL ?]

    Code:
    +-----------+-------------+------+-----+-------------------+-----------------------------+
    | Field     | Type        | Null | Key | Default           | Extra                       |
    +-----------+-------------+------+-----+-------------------+-----------------------------+
    | id        | int(11)     | NO   | PRI | NULL              | auto_increment              |
    | title     | varchar(80) | YES  |     | Untitled          |                             |
    | author    | varchar(50) | YES  |     | Anonymous         |                             |
    | content   | text        | NO   |     | NULL              |                             |
    | timestamp | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
    +-----------+-------------+------+-----+-------------------+-----------------------------+
    One thing to note about using this table is that if you are intending to work on multiple pages for CMS, you should modify the table a little bit to fit your needs.
    Otherwise, the table above should be a good start.

    For the user system, you should at least have the structure below.
    +-----------+-------------+------+-----+-------------------+-----------------------------+
    | Field | Type | Null | Key | Default | Extra |
    +-----------+-------------+------+-----+-------------------+-----------------------------+
    | id | int(11) | NO | PRI | NULL | auto_increment |
    | username | varchar(100) | NO | | | |
    | password | varchar(100) | NO | | | |
    | admin | int(11) | NO | | 0 | |
    +-----------+-------------+------+-----+-------------------+-----------------------------+
    Note: When `admin` is given a value of 0, the user is not admin.

    To handle images, you should just create a directory for them and if needed, make a page to upload the images. That should settle your problem.

    These are some mistakes I usually make, so take note of them as well!
    1) Creating security loopholes (Using GET when I am supposed to use POST)
    2) Messing up database while experimenting with PHP

    If you need any more help, feel free to leave me a private message
    Good luck on your CMS

  5. #5
    vv.bbcc19's Avatar
    vv.bbcc19 is offline Community Advocate vv.bbcc19 is just really nice
    Join Date
    Jun 2010
    Location
    India
    Posts
    1,505

    Re: Creating a Simple CMS [PHP + MySQL ?]

    Here is a tutorial.Small one though..
    The first step is to simply lay out the class in a file named 'simpleCMS.php' so we have a road map to work with.
    PHP Code:
    <?php

    class simpleCMS {
      var 
    $host;
      var 
    $username;
      var 
    $password;
      var 
    $table;

      public function 
    display_public() {

      }

      public function 
    display_admin() {

      }

      public function 
    write() {

      }

      public function 
    connect() {

      }

      private function 
    buildDB() {

      }
    }

    ?>
    As you can see, we're creating one class with four variables and five methods. I've opted to use PHP's object-oriented approach because it makes for cleaner code in large projects, and, in my opinion, it's just good practice.
    The Variables

    In this case, all four variables are for connecting to the database: $host, $username, $password, and $table provide a path and access to our database on the server. For now, we'll leave those empty and move on to our database, which is constructed by the method buildDB().
    Build the Database
    PHP Code:
    private function buildDB() {
        
    $sql = <<<MySQL_QUERY
            CREATE TABLE IF NOT EXISTS testDB (
                title       VARCHAR(150),
                bodytext    TEXT,
                created     VARCHAR(100)
        )
        MySQL_QUERY;

        return mysql_query(
    $sql);

    This function runs a MySQL command that checks the database to see if testDB exists. If so, it simply passes along a notification of success; if not, it creates our table and assigns three columns to hold data.

    Connect to the Database
    PHP Code:
    public function connect() {
        
    mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " mysql_error());
        
    mysql_select_db($this->table) or die("Could not select database. " mysql_error());

        return 
    $this->buildDB();

    We call mysql_connect() to hook into our database, and then mysql_select_db() to make sure we save our data in the right place. Both of these functions are accompanied by the die() command, which essentially says, "in the event that this function fails, stop execution of this script and display a message."

    Our connect() function connects to the database and gets us pointed in the right direction, then runs our buildDB() function. Remember the grammatically awkward "IF NOT EXISTS" part of our MySQL command? Because we're going to run this function every time the page is loaded, we have to make sure we're not overwriting our database with every function call, and that's exactly what that phrase requires.

    Build the Form
    PHP Code:
    public function display_admin() {
        return <<<ADMIN_FORM

        <form action="
    {$_SERVER['PHP_SELF']}" method="post">
          <label for="title">Title:</label>
          <input name="title" id="title" type="text" maxlength="150" />
          <label for="bodytext">Body Text:</label>
          <textarea name="bodytext" id="bodytext"></textarea>
          <input type="submit" value="Create This Entry!" />
        </form>

    ADMIN_FORM;
      } 
    Again, this is a very simple function. When called, it simply returns the HTML markup to create our form. You'll notice, however, in the action attribute of the form element, that I've used the variable $_SERVER['PHP_SELF']. This is, essentially, a shortcut that references the file you're currently using (in our case, it's display.php). This is useful if you'll be reusing your code across a site and don't necessarily want to rewrite this function for each page.

    I'm also going to take a second right now to talk about the method I'm using to return the HTML. It's a format used in PHP called HEREDOC syntax, and I love it.

    The primary advantage of HEREDOC is that it allows you to include formatting in your output. This is extraordinarily useful for folks like me who take issue with cluttered source code. You can read more about HEREDOC syntax and its ilk in the PHP manual.

    Next steps later...
    Last edited by callumacrae; 04-15-2011 at 09:34 AM. Reason: Added [php] tags
    karimirt47 likes this.
    BCV | Community Support Representative
    █ x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  6. #6
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Creating a Simple CMS [PHP + MySQL ?]

    Quote Originally Posted by vv.bbcc19 View Post
    Build the Database
    PHP Code:
    private function buildDB() {
        
    $sql = <<<MySQL_QUERY
            CREATE TABLE IF NOT EXISTS testDB (
                title       VARCHAR(150),
                bodytext    TEXT,
                created     VARCHAR(100)
        )
        MySQL_QUERY;

        return mysql_query(
    $sql);

    The closing identifier must be at the beginning of the line as per php.net's heredoc documentation:

    PHP Code:
    private function buildDB() {
        
    $sql = <<<MySQL_QUERY
            CREATE TABLE IF NOT EXISTS testDB (
                title       VARCHAR(150),
                bodytext    TEXT,
                created     VARCHAR(100)
        )
    MySQL_QUERY;

        return 
    mysql_query($sql);

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  7. #7
    vv.bbcc19's Avatar
    vv.bbcc19 is offline Community Advocate vv.bbcc19 is just really nice
    Join Date
    Jun 2010
    Location
    India
    Posts
    1,505

    Re: Creating a Simple CMS [PHP + MySQL ?]

    Ahah..thanks for the message.I did not get until then how your posts have a code segment separately put.
    I understood when you told about [php]
    Thanks for editing the post to make it look good.

    Quote Originally Posted by callumacrae View Post
    The closing identifier must be at the beginning of the line as per php.net's heredoc documentation:

    PHP Code:
    private function buildDB() {
        
    $sql = <<<MySQL_QUERY
            CREATE TABLE IF NOT EXISTS testDB (
                title       VARCHAR(150),
                bodytext    TEXT,
                created     VARCHAR(100)
        )
    MySQL_QUERY;

        return 
    mysql_query($sql);

    ~Callum
    Last edited by vv.bbcc19; 04-15-2011 at 01:26 PM.
    BCV | Community Support Representative
    █ x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Creating a Simple CMS [PHP + MySQL ?]

    Quote Originally Posted by alejandroangulo32 View Post
    I wanted to create a simple CMS but I'm not sure how to start off.
    [...]
    What other tables will I have to create? I plan on creating a user system, but for now I want to create a barebones CMS.
    Database design is a matter of modeling. Ask yourself: what are the properties of the things (e.g. pages, posts, authors/contributors/editors) you are modeling? Express this using the relational model (as you are using an RDB), where to-one properties become columns, and to-many (both one-to-many and many-to-many) properties are stored in their own table (with columns that reference rows in the object tables).

    As for a users table, an "admin" column (which should be a BOOL or INT(1)) isn't enough. Better is to have a "row" column (of ENUM or VARCHAR type), or (better still) a separate table relating users to the roles they have.

    Quote Originally Posted by alejandroangulo32 View Post
    I was also wondering how I should handle images? Should I just create a directory for them, or insert them into the database, or insert links in the database to the path of the actual image?
    MaestroFX has the short of it; for more, see "Storing Images in DB - Yea or Nay?"
    Last edited by misson; 04-16-2011 at 10:45 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. creating MYSQL database
    By poyax96 in forum Free Hosting
    Replies: 1
    Last Post: 11-13-2010, 09:24 PM
  2. Help with creating a mySQL database
    By josh72 in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 04-12-2010, 12:18 AM
  3. Creating MySQL Schema
    By tusharwadekar in forum Free Hosting
    Replies: 3
    Last Post: 03-26-2008, 03:28 AM
  4. MySQL Not Creating Databases
    By dudestermikey in forum Free Hosting
    Replies: 2
    Last Post: 01-04-2008, 09:16 AM
  5. Creating MySQL DB
    By argroup in forum Free Hosting
    Replies: 1
    Last Post: 12-24-2007, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers