SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How would YOU build this CMS?

    Hi,

    Firstly, my apologies for the length of this post.

    Now and then I find myself building the same types of CMS in PHP/MySQL, and frankly I'm getting bored. Not so bored I'm going to start learning OOP, but close to it(!)
    I have started working on a CMS for a client. Very basic stuff, no OOP or clever code here. The client wants the ability to Add/Edit/Remove News, Events and Special Offers on the site.

    Normally I would go ahead and make up addevent.php, addnews.php, addspecialoffer.php - all pretty well doing the same thing, save a few differences.


    Should I just have(as I've always done) addnews.php, addevent.php and addoffer.php etc., or should I (for efficiency sake) create an add.php then append a ?type=1 or ?type=2 to it with an if statement inside the add.php to determine which type is being added?

    So if they visit add.php?type=2 they could then be shown the 'Add Event' form, which will in turn use the 'event' validation in the add.php script.
    Or perhaps news.php?action=add <-- I sometimes see this.
    Hope I'm making sense.



    I just fancy knowing how you guys do your CMS' and if I'm perhaps doing more work than I should be.

    Right now I do it the following way:
    + use a database connection file
    + header / footer files
    + login / logout files(connect to mysql)
    + then an endless number of addevent.php - editevent.php - deleteevent.php type scripts (for news, events, special offers, web pages etc).


    Is there a sensible way to condese and 'modularise' my work?

    What references do you guys use when building a CMS? Is there a good example of a basic CMS I should look to for building my own?



    Many thanks for any advice.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Initially, I would say that you are repeating a lot of your code with your current add*.php approach.

    It would be far more extensible if you have a single 'add' entry point, which would validate the request then delegate it to the correct object/function/script.

    It would allow you to quickly add (no pun intended) additional items whilst maintaining the same validation logic.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for such a speedy response SilverBullet. Just what I wanted to hear.

    Like I say, I feel I've got a decent modular approach to the other areas of my CMS(of what little they are heh) and the public web site in general, but really feel I need to improve on this part to speed the process up further.

    Can you possibly give me an example of how I can do this? Really appreciate this, as I'm sure it will kick-start me into producing more efficient code in the future.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are you able to use, and can you see the advantages of using, a shared code-base that serves many different clients with differing cms requirements?

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    To clarify, for example, are all your websites on the same server, so you could make, for example, some kind of database connector that you could use throughout your future projects.
    PHP Code:
    <?php
    //project A.com
    include('lib/database_function.php');
    include(
    'lib/event_diary_functions.php');

    ?>

    <?php
    //project B.com
    include('lib/database_functions.php');
    include(
    'lib/event_diary_functions.php');

    ?>
    Is that what you are aiming for?

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that.

    Unfortunately, not all the sites are on the same server.

    I just hoped to have a good foundation for a CMS so that I only need to tweak certain parts of it to reuse it for other sites in the future.


    So say if the client wanted to have the ability to add events, I could just open the existing 'add.php' and insert some code for adding events.

    Right now, if they want to add news and special offers, I create unique 'addnews.php' and 'addoffer.php' every time, which is not time-efficient, however perfect world, I'd just have an 'add.php' script which then stores the code for both options:

    PHP Code:
    <?
    // add.php

    $type $_GET['type'];

    if (
    $type 1) {
    // then add news script goes here
    } else {
    // then add special offers script goes here
    }

    // and the same goes for the HTML form
    How should I best do this?


    Thanks for any input guys!

  7. #7
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think that approach is very efficient.
    You will find that it gets VERY messy the more you add to it (I know it does.. I used that approach years ago.)

    Now-a-days, I use an MVC approach and structure my admin like so:
    /controllers/admin/users.php (Class which contains add/edit/delete methods and logic.)
    /controllers/admin/news.php (ditto)
    /views/admin/users/add.php (Loaded by the controller. Contains the presentation code a.k.a. HTML / PHP result loops / etc..)
    /views/admin/users/edit.php (ditto)
    /views/admin/news/add.php (ditto)
    /views/admin/news/edit.php (ditto)
    /models/UsersModel.php (Interacts with the database. Typically a controller calls this to fetch data from the database.)
    /models/NewsModel.php (ditto)
    It's not very modular, but works for me at the moment.
    If I was building something that is redistributed, I'd use modules..
    /modules/users/controllers/users.php (Class which contains add/edit/delete methods and logic.)
    /modules/users/views/add.php (Loaded by the controller. Contains the presentation code a.k.a. HTML / PHP result loops / etc..)
    /modules/users/views/edit.php (ditto)

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the help.

    Sadly I've been doing it the slow way for a couple of years now and want to break free of it. But I always need a good tutorial to kick start me, or some example code to put me on the right track.

    A good offline pal of mine is happy to help, so I'm hopefully going to build some basic CMS with him, in a more efficient way.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Take a look at one of the easier PHP Frameworks, spend at least a day messing with it, read the documentation.

    If you can see the benefits, then go for it and start using a Framework.

    Else, start to slowly remove the bits of code that talk to your database into functions.

    Move those functions into their own files a bit like this post.

    So your scripts end up doing things like this:
    PHP Code:
    <?php
    include( "events/database_functions.php");

    ....
    // get your post values, check them over, decide to proceed and then simply do:

    add_event$event );
    Where add_event is a simple function which adds an event to the database, so keep all your mysql function (and all the associated, checking, error handling etc) in there.

    If you read anything on the CodeIgniter site, you should have grasped the idea that seperating your data into (basically) "views" (how it looks, - not just html, but could be simple php loops etc) and "models" ( your data, where it goes, how to get it out )

    Also, take a look at this current thread about templates, that might get you thinking.

    Separating concerns by simply extracting sql and simplifying the html/php can be made piecemeal, as long as you find yourself a reasonable naming system and get your include directives correct.

    Post the code you currently use to say, add an event (or anything similar) and someone will try and help you turn it into a function if you are unsure where to start.

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant! Thanks for the reply, most helpful.

    The following script is used to add news to the db. Firstly it connects to the db, includes a header file, includes a 'logged in' file to check if they're logged in then goes straight to the IF statement to validate input, then decide what to do with it.

    As you can see if I have one of these for each event, special offer adding/editing page etc., it soon adds up and becomes messy....

    PHP Code:
    <?

    // addnews.php

    require_once('../dbx/connect.php');

    $page_title "Add News";

    include (
    '../include/header.php');

    include (
    '../include/loggedin.php');


    // if form is submitted
    if (isset($_POST['submitted'])) {

        
    // news title
        
    if (!isset($_POST['title'][6])) {
            
    $titleclass "errortext";
            
    $title FALSE;
        } else {
            
    $title escape_data(strip_tags(stripslashes(trim($_POST['title']))));
            
    $titleclass FALSE;
        }
        
        
    // news description
        
    if (!isset($_POST['descr'][10])) {
            
    $descr "errortext";
            
    $descr FALSE;
        } else {
            
    $descr escape_data(strip_tags(stripslashes(trim($_POST['descr']))));
            
    $descrclass FALSE;
        }
        
        
    // store users session id value in variable
        
    $user_id $_SESSION[user_id];
        
        
    // if title + description are completed then...
        
    if ($title && $descr) {
                
        
            
    // insert values into table
            
    $query "INSERT INTO enews(title,descr,date,user_id) VALUES ('$title','$descr',NOW(),'$user_id')";
            
    mysql_query($query);
            
            echo 
    '<div class="success">';
            
            echo 
    '<p><strong>News Added !</strong><br />';

            echo 
    'The News item has been successfully added to the site.</div>';

            include (
    '../include/afooter.php');
        
            exit;
                
    } else {

        echo 
    '<div class="alert"><strong>News Not Added</strong><br />Please fill out the form completely and correctly, then Submit.<br /></div>';

    }

    }

    ?>

    <h3>Add News :</h3>

            <p>Simply fill in the form below to add a news item to the web site.</p>
            
            <p>&nbsp;</p>

            <form id="addnews" enctype="multipart/form-data" name="addnews" method="POST" action="addnews.php" class="book">

              <table border="0">
                <tr>
                  <td><strong>Title :</strong> </td>
                  <td><input name="title" type="text" class="txtfld" id="title" size="30" value="<?php if (isset($_POST['title'])) echo $_POST['title']; ?>" />
                      <?php if (!empty($titleclass)) { echo '<span class="errortext2">Please enter a Title for the News.</span>'; }?>      

    </td>
                </tr>
                
                <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
                
                <tr> 
                  <td colspan="2"><strong>Description :</strong> 
                    <?php if (!empty($descrclass)) { echo '<span class="errortext2">Please enter a detailed Description for the News.</span>'; }?>
                    <br />
                    <textarea name="descr" id="descr" cols="50" rows="8"><?php if (isset($_POST['descr'])) echo $_POST['descr']; ?></textarea>    

      </td>
                </tr>
                
                <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
                
                <tr> 
                  <td><strong>Image 1 :</strong></td>
                  <td> 
                    <input type="file" name="image1" id="image1" />  * main image     </td>
                </tr>
                
                <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
                
                <tr> 
                  <td colspan="2"><br />
                    <br />
                    <input name="submit" type="submit" class="submit" value="Add News" />
                    <input type="hidden" name="submitted" value="TRUE" />      
                  </td>
                </tr>
              </table>
                    <p><br />
              </p>
            
            </form>

    <?

    include ('../include/afooter.php');

    ?>

    If anyone can help me come up with a system to reduce this workload, I'd be over the moon and back again.
    Feel this is really letting me down, but I've no idea where to turn.


    Also, think I'll play around with CodeIgniter and do things 'better' from now on.

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Ostensibly as simply as this:
    PHP Code:

    function add_news$title$desc$user_id){

                
    // insert values into table
            
    $query "INSERT INTO enews(title,descr,date,user_id) VALUES ('$title','$descr',NOW(),'$user_id')";
    if(  
    mysql_query($query) ) {

    return 
    true;  // return something  BOOL true ..

    }else{

    return 
    false;  // return something else
    }


    Which your calling code can now do this:

    if( !add_news( $title, $desc, $user_id ) ) {
    // something went wrong ....
    }

    Thats the basics.

    But the thing is, you can make add_news() do so much more work for you.

    It could return an error instead of false when something goes wrong
    It could scream ERROR if you miss any of the 3 arguments
    It could check some of the values for you
    It could return a different error if a duplicate is found.... ( mysql err_no 1062 )
    It could handle being passed an array, and sort out which field to update

    But the thing is, if that file holding this function was called like this:

    include("news/database_functions.php");

    And you wanted to alter how your database code worked, say turn on logging for news items etc - then you would know where to look first.

    If this effect is what you are after, and you are rusty on functions, then head off to the manual and spend a day looking at all the native things that functions can do:

    func_get_args() etc etc.


    If you are good on functions, then start checking out OOP.

    Edit:


    PHP Code:
    Sorryforgot to add the database connection to the function .... heres a hacky way ... there are others ways of getting this clients database credentials.

    function 
    add_news$title$desc$user_id){

    include(
    "database_settings.php");

                
    // insert values into table

    ... // and so on 

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks Cups.

    I think I will take this and begin to plan out a well-constructed CMS. Your post has really made me re-think the way I do things.

    Perhaps in the future I can come back to you with further questions.


    Thank you.

  13. #13
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On a side note... if you are working with your friend on a new CMS, you may want to look into SVN (if you havent already)

  14. #14
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool. Is this something built into CodeIgniter or separate?

    Just out of interest simshaun, what tools do you use for PHP development?
    Kinda want to get myself on track with the more experienced coders

  15. #15
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not the only one you should be asking then.

    I use NuSphere PHPEd for development on a local server (check out XAMPP for an "easy" install.)

    SVN is a repository tool where you can keep track of any changes you make to files.
    Its a great tool once you get the hang of it.

    Check out TortoiseSVN and google for tutorials on how to use it.

    As you can see by my sig, CodeIgniter is my framework of choice. It's faster than just about all of the other popular frameworks in terms of requests per second. It's also easy to get started with and learn because it has great documentation.

  16. #16
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ha, it looks like you're getting dragged into OOP whether you like it or not!

    I have to agree with simshaun with regards to CodeIgniter, I find it is a great way to tease you into MVC. It's not the greatest out there, but it sure does have the easiest to follow user guide.

    Once you get the hang of the MVC approach, and you start to mentally take this into account when considering your project, consider moving on up into Kohana which a fork of CodeIgniter and IMO more advanced.

    You'll find CI / Kohana great for speedy development, yet once you delve into the realms of trying to apply many of the patterns OOP afford, you can quickly outgrow them and find them quite restrictive.

    It seems like a natural progression though, and I'm sure you'll enjoy every PHP minute.

    Good luck, and enjoy the ride.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  17. #17
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aaaah good. I have XAMPP installed, and d/l CodeIgniter, just haven't unwrapped it that much yet
    In terms of IDE, I may just stick with NetBeans(which I've also still to really play with).

    Thanks for your ideas. Really just need to get my head down and practice with them.

    edit:
    Thanks for that SilverBullet!

    So I've got CI, XAMPP and NetBeans at the ready.

    I'm now going to plan out a basic CMS with my friend and see the "best" way of doing things. I find it the easiest way to learn.

  18. #18
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SilverBullet pretty much hit dead center with what he says about CI (and most frameworks in general IMO).

    If I may make a recommendation, start out with the "Getting started" part of the user guide and read each page top to bottom. I found it helped me learn quicker.

  19. #19
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got a bad feeling I learned PHP the poor way for the past 3-4 years, can I still unlearn bad habits?

    What is the best way to learn best practices with PHP? I know I'm asking a lot, but you all seem very helpful and I appreciate it.

    I'll definitely take a look at the CI docs.

  20. #20
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You and me both man.

    Everyone is new to it at some point.
    Nobody simply "clicks" and knows everything about PHP.

    Can you unlearn bad habits? I did.

    The only way I know of to learn best practices is to read a lot and try things out for yourself.
    I suggest taking a look at how CI works behind the scenes.
    *Most* of the popular frameworks you see use "best practices", and you'll probably learn a lot just by figuring out how the framework works.

    Also, you could read this. It has some good pointers/guidelines you could follow.

  21. #21
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by PM
    I was just wondering if you could help me change my current 'add_news.php' script into one that works with your method, if it's not too much bother.
    Off Topic:

    ps Its better to post to this forum rather than pm me on this, that way more people might benefit from the discussion.


    What I was suggesting was how you can start to move towards "separating concerns" without using a framework, but using a framework would be my first suggestion given what you want to achieve. You can easily distribute your framework on multi-servers, its all copies of the code, but you can always upgrade all the servers with the same version of, say, CI.

    As you are at least downloading and looking at CI, this middle way I was trying to illustrate is something you can come back to.

    But, roughly this is how you could use the hack around I made:
    PHP Code:
    // as before ..
    // include your news to database functions ...

        // if title + description are completed then...
    //pseudo code here
    //if ( title and desc are not set) { show another warning and exit }


    if( add_news($title$desc$user_id)  === true ){          
           
            echo 
    '<div class="success">';
            echo 
    '<p><strong>News Added !</strong><br />';
            echo 
    'The News item has been successfully added to the site.</div>';

            include (
    '../include/afooter.php');
        
            exit;
                
    } else {

        echo 
    '<div class="alert"><strong>News Not Added</strong><br />Please fill out the form completely and correctly, then Submit.<br /></div>';


    BTW, in order to make good use of CI, you will need to understand how functions work (then referred to as "methods" inside classes), and you also need to give yourself a crash course on the syntax of OOP.

    OOP can get very heavy very fast, depending on how much you want to learn, but by association you will keep tripping over referrals to best practice as has already been mentioned here, cvs etc.

    Take things one a time though, don't get bogged down. CI is a good choice because the help forum is well maintained, you should find all the answers to simple questions in there.

  22. #22
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by simshaun View Post
    You and me both man.

    Everyone is new to it at some point.
    Nobody simply "clicks" and knows everything about PHP.

    Can you unlearn bad habits? I did.
    Me too.

    I get the feeling many of us on here are going down this road, a journey from scripter to programmer ... its no mean feat.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •