简体   繁体   English

正确的mvc概念,视图中包含少量php代码

[英]Right mvc concept, little php code in view

I have one not understood point In MVC pattern. 我对MVC模式有个不了解的地方。 Please help understood. 请帮助理解。

for example we have table for cars in database, we want obtain and print results from table, but if results are not found (0 rows), in this case print: "We dont have results" 例如,我们在数据库中有汽车表,我们希望从表中获取并打印结果,但是如果未找到结果(0行),则在这种情况下打印:“我们没有结果”

this is models.php 这是models.php

class modesl {

function getCars () {

    $res = $this->db->query("SELECT names FROM cars");
    if ($res->num_rows == 0) {
        return "We dont have results";
    }
    else {
        return $res;
    }

}

}

this is views.php 这是views.php

class views {

    function loadHTML ($resultFromCars) {
         require 'carspage.php';
    }

}

this is carspage.php 这是carspage.php

<html>
<body>
<?php

    if (is_object($resultFromCars)) {
        while ($row = $resultFromCars->fetch_assoc()) {
            echo $row['names']."<br>";
        }
    }
    else {
        echo  $resultFromCars;
    }

?>
</body>
</html>

this is controllers.php 这是controllers.php

class controllers {

    function generatePage () {  
        $model = new models();
        $resultFromCars = $model->getCars();


        $view = new views();
        $view->loadHTML($resultFromCars);
    }

}

This works, but as I know, many php code in view, (that is condition if (is_object) { } else { } ) is not right MVC. 这可行,但是据我所知,视图中有许多php代码(( if (is_object) { } else { } )是不正确的MVC。 tell please for this concret case, what must be change in my architecture (lol), for obtain right MVC concept? 请告诉我这个具体案例,为获得正确的MVC概念,我的体系结构(大声笑)必须进行哪些更改?

I like the answer provided by Havelock. 我喜欢Havelock提供的答案。

I would adjust this even further, by making sure your model already returns the data in an array format (or false, if nothing is found). 我将通过确保您的模型已经以数组格式返回数据来进一步调整此值(如果未找到任何内容,则返回false)。 Therefore, the logic for extracting data from resultset stays in the model, where it really should be. 因此,从结果集中提取数据的逻辑保留在模型中,而逻辑应该保留在模型中。

Your view becomes even simpler then: 您的视图将变得更加简单:

<?php

if (!empty($results)) {
  foreach ($results as $row) {
   echo $row['name'] . "<br />";
  }
} else {
    echo "Eh, Nothing found...";
}

You seem to have done a good job, just one small thing to improve. 您似乎做得不错,只是需要改进的一小件事。 As the model is a wrapper for data only, so you should return only data (and no strings, containing error/exception messages). 由于模型仅是数据的包装器,因此您应该仅返回数据(不返回包含错误/异常消息的字符串)。 In the case there's no data to return, then return FALSE , as it's done in PHP. 如果没有数据要返回,则返回FALSE ,就像在PHP中一样。

class CarModel {

    function getCars () {

        $res = $this->db->query("SELECT names FROM cars");
        if ($res->num_rows == 0) {
            return FALSE; // if that happens, the function will stop execution here, so no "else" is needed
        }

        return $res;

    }

}

And in your view 在您看来

<?php

    if ($resultFromCars === FALSE && !empty($resultFromCars)) {
        echo "We don't have results";
    }
    else { // now you know it's not FALSE, so it must be an object, no need to check whether it is one
        while ($row = $resultFromCars->fetch_assoc()) {
            echo $row['names']."<br>";
        }
    }

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM