首页 > 解决方案 > PHP MVC Count 函数并传递给视图

问题描述

只是想了解 MVC,并且在计算数据库中小部件的数量时遇到了一些麻烦。

我不确定这是不是搞砸了,但在我的索引页面上我有这个

        <?php require APPROOT . '/views/inc/header.php'; ?>

          Some Other stuff here

        <?php require APPROOT . '/views/inc/widget.php'; ?>

模型

        <?php
     class Widget {
        private $db;

        public function __construct(){
          $this->db = new Database;
        }

        public function countWidgets(){
          $this->db->query("SELECT id FROM widgets WHERE id=id");
          $count = $this->db->rowCount();
           }

控制器

<?php
         class Widgets extends Controller{
            public function __construct(){
              // Load Models
              $this->widgetModel = $this->model('Widget');
            }

        public function widget(){
              $count = $this->widgetModel->countWidgets();
              $data = [
                'count' => $count
              ];
              $this->view('inc/widget', $data);
            }  
        }

并查看

<div class="media-body text-right">
  <h2 class="text-white"><?php echo $count->id; ?></h2>
  <span>Total Widgets</span>
</div>

我似乎得到的只是这个错误 注意:尝试在第 29 行的 C:\xampp\htdocs\app\views\inc\widget.php 中获取非对象的属性“id”

如果有人能给我指出我哪里出错了,我将不胜感激,并提前感谢您提供的任何帮助。

谢谢莫莉

标签: phpmodel-view-controllercount

解决方案


推荐阅读