Im working on a small website and im a little confused on one step. I'm following a very simple mvc pattern, ive currently got one controller that creates a session array that holds all the posted data IF the form has been submitted, This data is coming from several different dropdown boxes which all have different Ids:
<?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;
?>
I have a select statement in my Model class that uses these session variables in a WHERE clause as below:
<?php
session_start();
require_once('../Config/config.php');
class ConCalc
{
public $dbconn;
public $TypicalReading;
public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->dbconn = $db;
}
public function getValues () {
$stmt = $this->dbconn->prepare("SELECT Consumption FROM consumption
WHERE HeatingType = :heatType
AND MeterType = :meterType
AND Bedrooms = :noBeds
AND HouseType = :house
AND HouseAge LIKE :age");
$stmt->bindparam(":heatType", $_SESSION['post-data']['heatingType']);
$stmt->bindparam(":meterType", $_SESSION['post-data']['meterType']);
$stmt->bindparam(":noBeds", $_SESSION['post-data']['noBedrooms']);
$stmt->bindparam(":house", $_SESSION['post-data']['houseType']);
$stmt->bindparam(":age", $_SESSION['post-data']['houseAge']);
$stmt->execute();
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)){
$TypicalReading = $result['Consumption'];
echo $TypicalReading; //used for testing
break;
}
}
}
?>
Ive done a Var_Dump on the statement and it does print out the values that have been posted:
array (size=8)
'gasUsed' => string 'Yes' (length=3)
'heatingType' => &string 'Electricity' (length=11)
'meterType' => &string 'Economy 7' (length=9)
'overnight' => string 'Yes' (length=3)
'noBedrooms' => &string '1 or 2' (length=6)
'houseType' => &string 'Flat' (length=4)
'houseAge' => &string 'More than 11' (length=12)
'btn-calcCon' => string 'Calculate' (length=9)
Now the top two rows of my table are as follows:
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
If i select Gas, Standard, 1 or 2, Flat, Less than 11 then the number 5430 should be displayed in a textbox once the form has been submitted.
Im a little unsure how to return that value. I tried to echo out my $stmt variable but it said PHP is unable to echo out a PDO object.
Any help will be appreciated Thanks!