Php 带有MVC的pdoselect语句
" 我在一个小型MVC网站上工作,遇到了一个小问题。我希望能够用从数据库中提取的数据填充HTML选择下拉列表。我的模型类中的select语句、视图类和控制器中的标准HTML内容目前都是空的,因为我对应该在那里添加什么感到有点困惑 代码运行良好,没有错误。但是,我的视图渲染了两次 这是我的密码: 型号:Php 带有MVC的pdoselect语句,php,pdo,Php,Pdo," 我在一个小型MVC网站上工作,遇到了一个小问题。我希望能够用从数据库中提取的数据填充HTML选择下拉列表。我的模型类中的select语句、视图类和控制器中的标准HTML内容目前都是空的,因为我对应该在那里添加什么感到有点困惑 代码运行良好,没有错误。但是,我的视图渲染了两次 这是我的密码: 型号: <?php require_once('../Controller/config.php'); class AppCalc { public $dbconn; publ
<?php
require_once('../Controller/config.php');
class AppCalc
{
public $dbconn;
public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->dbconn = $db;
}
public function fillDropdown(){
$stmt = $dbconn->prepare("SELECT Appliance FROM appliances");
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
?>
这就是我目前看到的:
任何帮助都将不胜感激
谢谢!!模型函数
fillDropdown
应该返回提取行的数组,例如:
public function fillDropdown(){
$stmt = $dbconn->prepare("SELECT Appliance FROM appliances");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
然后,您必须以某种方式将数据传递给视图。我建议构建一些类来呈现视图。这可以非常简单:
class DrawTool
{
public function render($viewPath, array $context = array())
{
// start output buffering
ob_start();
// make variables with $key => value pairs from $context associative array
// context will be gone after method execution thanks to variable scope
extract($context);
// render View
include $viewPath;
// return rendered View as string data type
return ob_get_clean();
}
}
现在,如果您有Draw类来渲染视图,您只需在控制器中使用下面的代码绘制视图即可:
<?php
require_once('../Model/applianceModel.php');
require_once('../Tool/DrawTool.php');
$newCalc = new AppCalc();
// instantiate drawing tool
$draw = new DrawTool();
// parse (render) appliance view
$renderedView = $draw->render('../View/applianceCalculator.php', array(
'appliances' => $newCalc->fillDropdown()
));
echo $renderedView;
?>
require_once('../Model/applianceModel.php');
require_once('../Tool/DrawTool.php'); // or wherever else you plan to put this class
$newCalc = new AppCalc();
// instantiate drawing tool
$draw = new DrawTool();
// parse (render) appliance view
$renderedView = $draw->render('../View/applianceView.php', array(
'appliances' => $newCalc->fillDropdown()
));
echo $renderedView;
以及内部视图文件:
<div id="appForm">
<form id="applianceCalc">
Typical Appliance:
<select name="appliances">
<?php foreach($appliances as $appliance): ?>
<option value="<?php echo $appliance['id']; ?>">
<?php echo $appliance['name']; ?>
</option>
<?php endforeach; ?>
</select>
<br>
Power Consumption:
<input type="text"></input>
<br>
Hours of use per day:
<input type="text"></input>
<br>
</div>
<input type="submit" name="btn-calcApp" value="Calculate"></input>
<input type="submit" name="btn-calRes" value="Reset"></input>
</form>
</div>
典型设备:
耗电量:
每天使用小时数:
在视图文件中,您只需遍历$appliances
数组(获取的行),其中single$appliances
是由数组表示的一行,其中包含id
和name
键
最后还有几个提示:
- 不要以
标记结束纯PHP文件,因为如果您希望在输出内容之前设置一些标题,则该标记之后的任何字符(最常见的是白色字符)都将触发已发送标题的错误?>
- 考虑在
方法中进行一些值检查,因为方法fillDropdown
在失败时也会返回false$stmt->fetchAll()
注意:未定义变量:dbconn
这是在视图中调用我的控制器类之后。我还没有编辑我的原始帖子以包含我正在使用的表。对,而不是$stmt=$dbconn->prepare(“从设备中选择设备“;
应该是$stmt=$this->dbconn->prepare(“从设备中选择设备”)
Hi这似乎奏效了。但是HTML表单打印了两次。一次是填充的下拉列表,一次是没有。我编辑了我的原始帖子。我想你的控制器回送了整个视图文件,所以你可能会在更远的地方再次回送。是你的控制器入口点文件(类似index.php)?在我的视图文件中,我需要控制器。我应该删除它吗?
class DrawTool
{
public function render($viewPath, array $context = array())
{
// start output buffering
ob_start();
// make variables with $key => value pairs from $context associative array
// context will be gone after method execution thanks to variable scope
extract($context);
// render View
include $viewPath;
// return rendered View as string data type
return ob_get_clean();
}
}
require_once('../Model/applianceModel.php');
require_once('../Tool/DrawTool.php'); // or wherever else you plan to put this class
$newCalc = new AppCalc();
// instantiate drawing tool
$draw = new DrawTool();
// parse (render) appliance view
$renderedView = $draw->render('../View/applianceView.php', array(
'appliances' => $newCalc->fillDropdown()
));
echo $renderedView;
<div id="appForm">
<form id="applianceCalc">
Typical Appliance:
<select name="appliances">
<?php foreach($appliances as $appliance): ?>
<option value="<?php echo $appliance['id']; ?>">
<?php echo $appliance['name']; ?>
</option>
<?php endforeach; ?>
</select>
<br>
Power Consumption:
<input type="text"></input>
<br>
Hours of use per day:
<input type="text"></input>
<br>
</div>
<input type="submit" name="btn-calcApp" value="Calculate"></input>
<input type="submit" name="btn-calRes" value="Reset"></input>
</form>
</div>