views:

82

answers:

4

I downloaded a php blog program and I want to divide this one-file php files into several php files. The purpose is to organize this messy program. (e.g. for example, each php file handles one feature. edit.php edits posts and topics.) Can anyone give me an idea? here is the file(I did some work already): http://upit.cc/files/2cdea5ee.zip The file that I am going to divide is index.php.

+2  A: 

I am not on my home computer right now so I won't be downloading the file, but you could split each class (if it uses them) into several php files, then use require_once to include them in the main file.

Alternatively you could just group all similar functions into separate files and again use require_once to include them.

Hope this helps, it's as much as I can offer with out downloading the code.

wiggles
That's exactly what he should do. Nice answer!
jlafay
A: 

Find parts of it that can be separated out and remove them from the main file, saving them in their own files. You can then use the php function require_once(url) to include them on your page. In essence your code will look much more simple:

<?php
    require_once('header.php');
    require_once('body.php');
    require_once('footer.php');
?>
Adam
+1  A: 

Hi,

The dividing of code is called "Refactoring". I looked at the code, it is very conditional.

You can take couple of approaches to re-factor it.

Approach 1: Just re-factor every condition into a separate file and then include that file when condition is met.

before:

if (condition_1){
 // CODE....
 // CODE....
 // CODE....
}

after

// move all the code from condition to file called includes/condition_1.php
if (condition_1){
   include_once 'includes/condition_1.php';
}

Approach 2 (might be too much for this project):

MVC architecture

  1. Create object "controller" that will be responsible for deciding what needs to be executed. Basically all your conditions will go in there
  2. Create views that controller will be loading and displaying
Alex
A: 

The first thing I'd do would be to decouple all the code I could from the main loop. For example, I'd take all of this kind of code:

elseif(isset($_GET['view_post'])) {
    $_ps = sqlite_qrs('SELECT * FROM post WHERE id = \''.$_GET['view_post'].'\'');
    $_cm = sqlite_qr('SELECT * FROM comment WHERE pid = '.$_ps['id']);
    $_ct = sqlite_qrs('SELECT name FROM category WHERE id = '.$_ps['pid']);
    $meta = htmlspecialchars($_ps['title']);
    $body.= '...';
    foreach($_cm as $cm) {
        $body.= '...';
    }
}
elseif(isset($_GET['view_category'])) {
    $_ct = sqlite_qrs('SELECT * FROM category WHERE id = \''.$_GET['view_category'].'\'');
    $_ps = sqlite_qr('SELECT id, title FROM post WHERE pid = '.$_ct['id']);
    $meta = htmlspecialchars($_ct['name']);
    $body.= '...';
    foreach($_ps as $ps) {
        $body.= '...';
    }
}

And refactor it like that:

class View {

    public static function Post($post_id) {
        $_ps = sqlite_qrs('SELECT * FROM post WHERE id = \''.$post_id.'\'');
        $_cm = sqlite_qr('SELECT * FROM comment WHERE pid = '.$_ps['id']);
        $_ct = sqlite_qrs('SELECT name FROM category WHERE id = '.$_ps['pid']);
        $meta = htmlspecialchars($_ps['title']);
        $body.= '...';
        foreach($_cm as $cm) {
            $body.= '...';
        }
        return array($body, $meta);
    }

    public static function Category($category_id) {
        $_ct = sqlite_qrs('SELECT * FROM category WHERE id = \''.$category_id.'\'');
        $_ps = sqlite_qr('SELECT id, title FROM post WHERE pid = '.$_ct['id']);
        $meta = htmlspecialchars($_ct['name']);
        $body.= '...';
        foreach($_ps as $ps) {
            $body.= '...';
        }
        return array($body, $meta);
    }
}

[..]

elseif(isset($_GET['view_post'])) {
    list($body, $meta) = View::Post($_GET['view_post']);
}
elseif(isset($_GET['view_category'])) {
    list($body, $meta) = View::Category($_GET['view_post']);
}

After I've separated the pieces out as static methods, I'd start thinking about separating out the HTML into templates and the database-related code into models.

Pies