Professional Documents
Culture Documents
Example MVC Model PHP
Example MVC Model PHP
Tin bi vit
Bi vit ny c ra i b tr kin thc cho vic tm hiu v lm
vic vi CodeIgniter framework
i tng hng n
Bi vit ny dnh cho tt c cc bn lp trnh vin khi mi bc vo
mng pht trin web s dng ngn ng PHP.
File demo/index.php
<html>
<head>
<title>Calculation using MVC Model</title>
<style type="text/css">
#form{
min-width:300px;
max-width:600px;
margin:auto;
font-family: sans-serif;
box-shadow: 5px 5px 5px #888888;
padding:20px;
border:4px solid #27748A;
}
#form h2{
color:#27748A;
font-size:35px;
margin:0;
}
#form p{
font-size:15px;
color:#222222;
}
#form div
{
margin-top:10px;
}
#form label
{
font-weight:bold;
font-size:12px;
color:#184552;
}
</style>
</head>
<body>
<form id="form" action="controller/CalcController.php"
method="post">
<h2>Calculation using MVC Model</h2>
<div>
<label for='name'>Number 1</label>
<input type='text' name='number1'
placeholder='number 1' required />
</div>
<div>
<label for='name'>Number 2</label>
<input type='text' name='number2'
placeholder='number 2' required />
</div>
<div>
<label for='name'>Method</label>
<select name="method_v">
<option value="add">Add</option>
<option value="sub">Sub</option>
</select>
</div>
<div>
</form>
</body>
</html>
File demo/controller/CalcModel.php
<?php
class CalcModel{
public $result;
public $a;
public $b;
public function add(){
$this->result = $this->a + $this->b;
}
public function sub(){
$this->result = $this->a - $this->b;
}
public function method_calc($method){
switch ($method) {
case 'add':
$this->add();
break;
case 'sub':
$this->sub();
break;
default:
$this->result = "No support";
break;
}
}
}
File demo/controller/CalcController.php
<?php
include_once '../model/CalcModel.php';
if(isset($_POST) AND isset($_POST['btnSubmit'])){
$var1 = $_POST['number1'];
$var2 = $_POST['number2'];
$method = $_POST['method_v'];
$calc = new CalcModel();
$calc->a = $var1;
$calc->b = $var2;
$calc->method_calc($method);
}
include_once('../view/CalcView.php');
File demo/controller/CalcView.php
<html>
<head>
<title>Calculation using MVC Model</title>
<style type="text/css">
#form{
min-width:300px;
max-width:600px;
margin:auto;
font-family: sans-serif;
box-shadow: 5px 5px 5px #888888;
padding:20px;
border:4px solid #27748A;
}
#form h2{
color:#27748A;
font-size:35px;
margin:0;
}
#form p{
font-size:15px;
color:#222222;
}
#form div
{
margin-top:10px;
}
#form input, textarea, button
{
width:50%;
border:2px solid #9ECEDB;
padding:3px 5px;
}
#form label
{
font-size:14px;
color:#184552;
}
</style>
</head>
<body>
<form id="form" action="controller/CalcController.php"
method="post">
<h2>Calculation using MVC Model</h2>
<div>
<label for='name'>Result: <strong><?php echo
$calc->result; ?></strong></label>
</div>
<form>
</body>
</html>