-2

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!

Kingspud
  • 65
  • 6

1 Answers1

0

try this code:

while ($result = $stmt->fetch(PDO::FETCH_ASSOC))
  echo $result['gasUsed'].' '.$result['heatingType'] ;//...

EDIT:
so if you need find values in result (but I don't know why don't use where clause to filter results) do something like this:

while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
  if( $result['gasUsed'] == 'Gas' && $result['heatingType'] == 'Standard' /* ... */ ){
    $v = $result['Consumption'];
    break;
  } 
}
Adam Silenko
  • 3,025
  • 1
  • 14
  • 30
  • `while ($result = $stmt->fetch(PDO::FETCH_ASSOC)){ echo $result['Consumption']; }` Seems to work but i wanted to be able to store the Consumption value in a variable and call that in my View class – Kingspud Apr 16 '16 at 20:53
  • Well ive done this; `while ($result = $stmt->fetch(PDO::FETCH_ASSOC)){ $TypicalReading = $result['Consumption']; break; }` And `$TypicalReading` is a public variable in my Model class. When i call it in my View class i get the Undefined Variable error – Kingspud Apr 16 '16 at 21:47
  • Sorry my view file has no classes, its pure HTML – Kingspud Apr 16 '16 at 21:58
  • show code where you set $TypicalReading and where you use $TypicalReading – Adam Silenko Apr 16 '16 at 22:57
  • how you use `getValues()`? Try add `return $TypicalReading;` to `getValues` function... – Adam Silenko Apr 16 '16 at 23:08
  • Edited original post to include full controller class – Kingspud Apr 16 '16 at 23:13
  • if you use `$newCalc->getValues()` to get value, you must it return. If you use `$newCalc->getValues()` only to calculate, then set `$this->TypicalReading` and use `$newCalc->TypicalReading` – Adam Silenko Apr 16 '16 at 23:27
  • I am using `$newCalc->getValues()` to only retrieve values from database. What do you mean it must return? – Kingspud Apr 16 '16 at 23:31
  • **add `return $TypicalReading;` to getValues function** – Adam Silenko Apr 16 '16 at 23:38
  • read this: [User-defined functions](http://php.net/manual/en/functions.user-defined.php), and this [Returning values](http://php.net/manual/en/functions.returning-values.php) – Adam Silenko Apr 16 '16 at 23:47
  • Thank you for the links, but I'm still a little confused. Where would I are the return? In my model or my class? – Kingspud Apr 17 '16 at 00:41
  • after set $TypicalReading or instead of `$TypicalReading =` – Adam Silenko Apr 17 '16 at 00:49