Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I try to improve my code: separate html/php, much clear, next time change will be easier.
After do research about MVC/OOP, I made below code for example to learn,
I understand this is not the MVC pattern now, can anybody help me fix it become real MVC?
(then next time me or us won't be close question again because it is not mvc)
Please change code for example, Many Thanks!!!!

  1. get the result from db (list all)
  2. get the result from db (list search)

index.php

require_once 'grant.php';
require_once 'controller.php';
$controller = new controller();
$controller -> temp_index();

controller.php

require_once 'model.php';
class controller{
    public $model;
    public function __construct(){
        $this -> model = new model();
    }

    public function temp_index(){
        require 'temp_index.php';
        if($_REQUEST['submit'] == 'get_all'){
            $result = $this -> model -> get_all();
            require 'get_all.php';
        }
        else if($_REQUEST['submit'] == 'get_search'){
            $result = $this -> model -> get_search();
            require 'get_search.php';
        }
    }
}

model.php // please ignore not use PDO

class model{
    public function get_all(){
        $sql = "select * from tb order by id desc";
            $query = mysql_query($sql) or die(mysql_error());
            $result = array();
    while($list = mysql_fetch_array($query)){
                $result[] = $list;
            }
            return $result;
    }
    public function get_search($search){
        $search = mysql_real_escape_string($_POST['search']);
        $search = trim($search);
        if($search !== ''){
            $sql = "select * from tb where ac_email like '%$search%'";
            $query = mysql_query($sql) or die(mysql_error());
            if(mysql_num_rows($query) > 0){
                $result = array();
                while($list = mysql_fetch_array($query)){
                    $result['result'][] = $list;
                }
                return $result;
            }
            else{
                $result['statu'] = 'can\'t find search';
                return $result;
            }
        }else{
            $result['statu'] = 'can\'t find search plz input text';
            return $result;
        }           
    }
}

get_search.php// view

<div class="reslt_get_search">
<?php foreach ($result['result'] as $list) : ?>
    <div><?php print"$list[ac_id]";?></div>
    <div><?php print"$list[ac_email]";?></div>
<?php endforeach; ?>

    <div><?php print"$result[statu]";?></div>
</div>

temp_index.php// view

<form action="" method="POST">
    <input type="submit" name="submit" value="get_all">
</form>
<form action="" method="POST">
    <input type="text" name="search">
    <input type="submit" name="submit" value="get_search">
</form>
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.