dongzhihong3940 2016-10-12 09:56
浏览 86

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

Using mvc structure I create the files below it works, but I want to create a simple form and store that data into mysql database and retrieve data from db, where I will write query for db and config db. How to do this, please give me some idea guys...

THIS MY index.php file

     <?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>';
     ?> 

This my controller code:

    <?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  

    ?>

This my model.php code:

    <?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];

      }
    }
    ?>

this my view.php code:

    <?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);
      }

    }


    ?>

this my cars.html code:

    <!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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 MATLAB动图问题
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名