从SELECT语句返回的PHP打印特定值
我在一个小网站上工作,有一步我有点困惑。 我遵循一个非常简单的mvc模式,我目前有一个控制器,它创建一个会话数组,保存所有发布的数据。如果表单已经提交,这些数据来自几个不同的下拉框,它们都有不同的ID:从SELECT语句返回的PHP打印特定值,php,mysql,model-view-controller,pdo,Php,Mysql,Model View Controller,Pdo,我在一个小网站上工作,有一步我有点困惑。 我遵循一个非常简单的mvc模式,我目前有一个控制器,它创建一个会话数组,保存所有发布的数据。如果表单已经提交,这些数据来自几个不同的下拉框,它们都有不同的ID: <?php require_once('../Model/CalculatorModel.php'); require_once('../Tool/DrawTool.php'); $newCalc = new ConCalc(); // instantiate drawing too
<?php
require_once('../Model/CalculatorModel.php');
require_once('../Tool/DrawTool.php');
$newCalc = new ConCalc();
// instantiate drawing tool
$draw = new DrawTool();
// parse (render) appliance view
$renderedView = $draw->render('../View/calculator.php', array('calcvalues' => $newCalc->getValues()));
if(isset($_POST['btn-calcCon'])){
$_SESSION['post-data'] = $_POST;
$_SESSION['post-data']['heatingType'];
$_SESSION['post-data']['meterType'];
$_SESSION['post-data']['noBedrooms'];
$_SESSION['post-data']['houseType'];
$_SESSION['post-data']['houseAge'];
}
echo $renderedView;
?>
现在,我的表格顶部的两行如下所示:
HeatingType MeterType Bedrooms HouseType HouseAge Consumption
Gas Standard 1 or 2 Flat Less than 11 years 5430
Gas Standard 1 or 2 Flat More than 11 years 7270
如果我选择Gas,Standard,1或2,Flat,小于11,则提交表单后,数字5430应显示在文本框中
我有点不确定如何返回那个值。我试图回显$stmt变量,但它说PHP无法回显PDO对象
任何帮助都将不胜感激
谢谢 请尝试以下代码:
while ($result = $stmt->fetch(PDO::FETCH_ASSOC))
echo $result['gasUsed'].' '.$result['heatingType'] ;//...
编辑:所以,如果您需要在结果中查找值(但我不知道为什么不使用where子句来筛选结果),请执行以下操作:
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
if( $result['gasUsed'] == 'Gas' && $result['heatingType'] == 'Standard' /* ... */ ){
$v = $result['Consumption'];
break;
}
}
语句具有
=
var_dump类似吗?是的,很抱歉我忘记更新我的var_dump,原始代码已被编辑。可能重复($result=$stmt->fetch(PDO::fetch_ASSOC)){echo$result['Consumption'];}
似乎可以工作,但我希望能够将消费值存储在变量中,并在我看来调用该变量。我做得很好while($result=$stmt->fetch(PDO::fetch_ASSOC)){$TypicalReading=$result['Consumption'];break;}
和$TypicalReading
是我的模型类中的公共变量。当我在我的视图类中调用它时,我得到了未定义的变量error对不起,我的视图文件没有类,它是纯HTMLshow代码,在这里设置$TypicalReading,在那里使用$TypicalReading如何使用getValues()
?尝试添加返回$TypicalReading代码>到getValues
函数。。。
while ($result = $stmt->fetch(PDO::FETCH_ASSOC))
echo $result['gasUsed'].' '.$result['heatingType'] ;//...
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
if( $result['gasUsed'] == 'Gas' && $result['heatingType'] == 'Standard' /* ... */ ){
$v = $result['Consumption'];
break;
}
}