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:

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'];

}

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 function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->dbconn = $db;
    }

    public function getValues () {
    $stmt = $this->dbconn->prepare("SELECT Consumption FROM consumption WHERE HeatingType LIKE '" . $_SESSION['post-data']['heatingType'] . "' AND MeterType LIKE '" . $_SESSION['post-data']['meterType'] . "' AND Bedrooms LIKE '" . $_SESSION['post-data']['noBedrooms'] . "' AND HouseType LIKE '" . $_SESSION['post-data']['houseType'] . "' AND HouseAge LIKE '" . $_SESSION['post-data']['houseAge'] . "'");
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);

    }
}

?>

Ive done a Var_Dump on the statement and it does print out the values that have been posted:

object(PDOStatement)[4]
  public 'queryString' => string 'SELECT Consumption FROM consumption WHERE HeatingType LIKE 'Gas' AND MeterType LIKE 'Standard' AND Bedrooms LIKE '1 or 2' AND HouseType LIKE 'Flat' AND HouseAge LIKE 'Less than 11''

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!

Recommended Answers

All 2 Replies

You could have spent a few minutes reading up on the fetchAll() method. Hint: it returns an array.

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
now loop through each row in the $result array, getting the Consumption value

foreach($result as $row) {
    $c = $row['Consumption'];
}

Works a charm, silly mistake on my part as i had done something very similar in a different part of my website

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.