如何将此列表组织到 mvc 中?
How do I organize this list into mvc?
我想列出我店里最畅销的印刷品。
我有一个 table (print_for_sale),其中包含 fk_sale_id 和 fk_print_id,我可以在其中查看已购买了多少印刷品。
我想出了这个代码来打印出一个有组织的列表,从最多到最少(不确定它是否完全正确):
<?php
$sql = "SELECT fk_print_id as printId, COUNT(print_for_sale_id) as saleCount
FROM print_for_sale
GROUP BY fk_print_id
ORDER BY saleCount DESC";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo $row['printId']; echo $row['saleCount']; }
?>
我正在尝试将其转换为我的模型、控制器和视图 (mvc),但我遇到了很多麻烦。我是新手,我尝试过很多不同的方法,none 似乎有效。我迷路了。现在我是这样的(它什么也不打印),或者当我在我的视图中回显时,它打印的唯一东西是 "Notice undefined variable arr"
model.php
function getBestSelling() {
$sql = "SELECT fk_print_id as printId, COUNT(print_for_sale_id) as saleCount
FROM print_for_sale
GROUP BY fk_print_id
ORDER BY saleCount DESC";
$result = $this->conn->query($sql);
return $this->buildArr($result);
}
controller.php
function bestselling() {
$arr = $this->model->getBestSelling();
print_r($arr);
include 'view.php';
}
view.php
<?php echo $arr['printId'] ?>
我想打印出查询结果,但没有任何效果。如果这不是一个合适的问题,我很抱歉,但我真的需要一些帮助来理解这个问题。
我没有使用任何框架。
在我的 模型 中,我有一个 class model { }
、一个 function __construct()
和一个 function __destruct()
。我还有一个构建数组的功能。
function buildArr($result) {
$arr = array();
while ($row = $result->fetch_assoc()) {
array_push($arr, $row);
}
return $arr;
}
在我的 控制器中 我 include 'model.php';
有一个 class Controller {}
和 function __construct() { $this->model = new model();}
.
我还有一个 index.php 开始会话和 define("BASE_URL", 'http://' . $_SERVER['SERVER_NAME'] . '/printstore/index.php/');
该应用正在运行。我已经可以注册用户、登录并进行购买。我似乎无法正确理解这部分代码。
将不胜感激一些帮助!很抱歉这么长post,提前谢谢你。
包含文件的行为与您预期的不同。要做到这一点,您必须从变量中创建一个全局变量(但不要这样做!)。您可以 print_r()
控制器中的数组,因为您是从模型中获取它的。但在那之后你包括视图。当您将该数组放在包含的文件中,而不在文件本身中定义它时,您不会得到结果,因为只有 including 文件可以访问任何已定义的变量。反之则不然。
因此,如果您希望它起作用,您要么必须将变量 $arr
设置为全局变量(不推荐),要么将控制器包含在 view.php
中。 (请注意,这是 而不是 MVC!)如果您希望这是 MVC,则必须对控制器使用 类 并在视图中使用这些对象。要对此找到更好的解释,请参阅下面的链接。
我个人喜欢使用模板引擎(也与 MVC 无关)。我也很喜欢Smarty, but I've heard good stories about mustache
这些模板引擎为您提供了一种将变量传递给模板的方法,而无需包含控制器文件。例如在 Smarty 中:
PHP:
$variable = 'Hello World!';
$smarty->assign('variable', $variable);
$smarty->display('helloworld.tpl');
聪明,helloworld.tpl:
{$variable}
输出:
Hello World!
我还建议您阅读这些内容,它们可能对您的帮助超出我所能解释的范围。
- 在 PHP 中设置 MVC:http://www.sitepoint.com/the-mvc-pattern-and-php-1/
- 包括 PHP 中的文件:Passing a variable from one php include file to another: global vs. not
编辑
Teresko 是对的,您不应该使用全局变量。我只是在这里指定它们,因为可以使用它们(绝对不推荐)。
阅读这个问题,了解几个原因:Stop using `global` in PHP
我想列出我店里最畅销的印刷品。 我有一个 table (print_for_sale),其中包含 fk_sale_id 和 fk_print_id,我可以在其中查看已购买了多少印刷品。
我想出了这个代码来打印出一个有组织的列表,从最多到最少(不确定它是否完全正确):
<?php
$sql = "SELECT fk_print_id as printId, COUNT(print_for_sale_id) as saleCount
FROM print_for_sale
GROUP BY fk_print_id
ORDER BY saleCount DESC";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo $row['printId']; echo $row['saleCount']; }
?>
我正在尝试将其转换为我的模型、控制器和视图 (mvc),但我遇到了很多麻烦。我是新手,我尝试过很多不同的方法,none 似乎有效。我迷路了。现在我是这样的(它什么也不打印),或者当我在我的视图中回显时,它打印的唯一东西是 "Notice undefined variable arr"
model.php
function getBestSelling() {
$sql = "SELECT fk_print_id as printId, COUNT(print_for_sale_id) as saleCount
FROM print_for_sale
GROUP BY fk_print_id
ORDER BY saleCount DESC";
$result = $this->conn->query($sql);
return $this->buildArr($result);
}
controller.php
function bestselling() {
$arr = $this->model->getBestSelling();
print_r($arr);
include 'view.php';
}
view.php
<?php echo $arr['printId'] ?>
我想打印出查询结果,但没有任何效果。如果这不是一个合适的问题,我很抱歉,但我真的需要一些帮助来理解这个问题。
我没有使用任何框架。
在我的 模型 中,我有一个 class model { }
、一个 function __construct()
和一个 function __destruct()
。我还有一个构建数组的功能。
function buildArr($result) {
$arr = array();
while ($row = $result->fetch_assoc()) {
array_push($arr, $row);
}
return $arr;
}
在我的 控制器中 我 include 'model.php';
有一个 class Controller {}
和 function __construct() { $this->model = new model();}
.
我还有一个 index.php 开始会话和 define("BASE_URL", 'http://' . $_SERVER['SERVER_NAME'] . '/printstore/index.php/');
该应用正在运行。我已经可以注册用户、登录并进行购买。我似乎无法正确理解这部分代码。 将不胜感激一些帮助!很抱歉这么长post,提前谢谢你。
包含文件的行为与您预期的不同。要做到这一点,您必须从变量中创建一个全局变量(但不要这样做!)。您可以 print_r()
控制器中的数组,因为您是从模型中获取它的。但在那之后你包括视图。当您将该数组放在包含的文件中,而不在文件本身中定义它时,您不会得到结果,因为只有 including 文件可以访问任何已定义的变量。反之则不然。
因此,如果您希望它起作用,您要么必须将变量 $arr
设置为全局变量(不推荐),要么将控制器包含在 view.php
中。 (请注意,这是 而不是 MVC!)如果您希望这是 MVC,则必须对控制器使用 类 并在视图中使用这些对象。要对此找到更好的解释,请参阅下面的链接。
我个人喜欢使用模板引擎(也与 MVC 无关)。我也很喜欢Smarty, but I've heard good stories about mustache
这些模板引擎为您提供了一种将变量传递给模板的方法,而无需包含控制器文件。例如在 Smarty 中:
PHP:
$variable = 'Hello World!';
$smarty->assign('variable', $variable);
$smarty->display('helloworld.tpl');
聪明,helloworld.tpl:
{$variable}
输出:
Hello World!
我还建议您阅读这些内容,它们可能对您的帮助超出我所能解释的范围。
- 在 PHP 中设置 MVC:http://www.sitepoint.com/the-mvc-pattern-and-php-1/
- 包括 PHP 中的文件:Passing a variable from one php include file to another: global vs. not
编辑
Teresko 是对的,您不应该使用全局变量。我只是在这里指定它们,因为可以使用它们(绝对不推荐)。
阅读这个问题,了解几个原因:Stop using `global` in PHP