如何在phpmysql中使用mvc框架创建表单?

时间:2016-10-12 09:56:01

标签: php mysql model-view-controller

使用mvc结构我创建它下面的文件工作,但我想创建一个简单的表单并将该数据存储到mysql数据库并从db检索数据,我将在其中编写db和config db的查询。怎么做,请给我一些想法......

这是我的index.php文件

     <?php 
     include_once('../../../mvc/Controller.php');


     if(!empty($_SERVER['QUERY_STRING'])){
       //reads and explodes the querystring into array eg 
       //index.php?Controller/getAllCars
      //where Controller end up in $querArray[0]=Controller 
    // and getAllCars end up in $queryArray[1]=getAllCars
       $queryArray=explode('/',$_SERVER['QUERY_STRING']);
       $controller=new $queryArray[0];
       //calls the method in the controller that gets data from the model
       //and assigns it to a view that presents it
       //$controller->getAllCars();

       if(isset($queryArray[2])){
          $controller->$queryArray[1]($queryArray[2]);
       }
       else{
         $controller->$queryArray[1]();
       }
     }
     echo '<a href="./index.php?Controller/getAllCars">Show cars</a>';
     ?> 

这是我的控制器代码:

    <?php
    include_once('../../../mvc/Model.php');
    include_once('../../../mvc/View.php');
    class Controller{
      private $model;
       private $view;
      function __construct(){
         $this->model=new Model();
          $this->view =new View();
      }
     public function getAllCars(){

        $cars=$this->model->getCars();


        //assigns data from the model to a variable cars
        //that will be presented in the "html" file
        $this->view->assign('cars',$cars);
        //displays the "html" file that will present data from the model
        $this->view->display('./cars.html');

      }
      public function getCarByRegnumber($regnumber){
         //assigns data from the model to a variable cars
        //that will be presented in the "html" file
        $this->view->assign('car', $this->model->getCarByRegnumber($regnumber));
        //displays the "html" file that will present data from the model
        $this->view->display('./cars.html');

      }
    }//end claSS  

    ?>

这是我的model.php代码:

    <?php
    class Model {
      private $cars;
     function __construct(){
       $this->cars=array('abc123'=>'volvo','bbb123'=>'bmw','ccc123'=>'audi');//here values for variable assigned
     } 
     public function getCars(){
        return $this->cars;
      }
     public  function  getCarByRegnumber($regnumber){
       return $this->cars[$regnumber];

      }
    }
    ?>

这是我的view.php代码:

    <?php
    class View {
     private $data=array();
      function assign($varname,$vardata){
         $this->data[$varname]=$vardata;
      }
      function display($filename){
        //extract the data that the controller got from the model
        extract($this->data);
        //include the file that presents the extracted data
        include($filename);
      }

    }


    ?>

这是我的cars.html代码:

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Your page title</title>
        <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
    </head>
    <body>
      <ul>
        <?php
        //loops the data that the controller got from the model and puttedd in th view
          foreach($cars as $key =>$car){
            echo '<li>';
            echo $car;
            echo '</li>';
          }

        ?>
        </ul>
    </body>

0 个答案:

没有答案