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!!!!
- get the result from db (list all)
- 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>