How to add a new php file to mvc concept
How to use the mvc concept,
I need to add a php fi开发者_JAVA百科le to mvc concept.
Please explain
Before you start adding files, you need to understand what MVC is, i assume that you understand what a controller, model and view is, ok i will try to explain step-by-step on how to add a file. Let's say you want to create a page that grabs some product info from database and shows that on a page called products.php.
Step 1: You create a controller named products.php and put all the variables that will be passed to view as well as function to grab products info from db through model.
Step 2: You create a model named products.php and write a function it it that will grab the product info from db.
Step 3: You create a view named products.php and show all variables coming from controller as well as any html for the layout.
Here is the basic skeletion:
products.php controller
class products_controller extends controller
{
// set a variable to be shown on the view
$this->view->myvariable = 'Our Products';
// call model function to get info from db that will be shown on the view.
$this->load->model('products');
$this->view->db_products = $this->products->getProducts();
// now render the view
$this->view->render();
}
products.php model
class products_model extends model
{
function getProducts()
{
$result = mysql_query("select * from products_table");
$rows = mysql_fetch_assoc($result);
return $rows;
}
}
products.php view
<html>
........
<?php echo $myvariable; // this var comes from controller?>
<?php
// now show products coming from db
foreach ($db_products as $product)
{
echo $product['name'];
echo $product['price'];
echo $product['etc'];
}
?>
........
</html>
Note: This is just an example but depending on which MVC framework you are using, file names and class names or syntax might look different, so you will have to adjust that. However, i have put in the code from my own MVC framework named EZPHP
, and as the name suggests, it is very easy to use MVC framework
. If you need it just reply through a comment.
Thanks and hope that helps :)
精彩评论