php - 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”
如果有人能给我指出我哪里出错了,我将不胜感激,并提前感谢您提供的任何帮助。
谢谢莫莉
解决方案
推荐阅读
- c++ - C++ 字符串出现次数
- google-sheets - 当编辑的单元格值显示“完成”并且电子邮件主题是编辑行中的第一个单元格时自动发送电子邮件
- c# - 在 C# 中正确初始化 Windows 事件日志源
- sorting - 有人可以帮助我了解鸡尾酒种类的一些优点和缺点吗?
- python - Numba Python - 如何有效地利用并行性?
- search - 如何在欧几里得空间中找到最远的邻居?
- r - ggplot 上的 scale_y_continuous 和离散 - 将 x 轴设置为固定
- sql - 数据透视表非唯一值
- c# - 如何找到游戏对象正在查看的方向的 2D 方向向量
- jquery - 使用 Ajax 和 Web Api 级联下拉菜单