tags:

views:

44

answers:

1

Hi,

I'm new to OOP and i'm trying to get a grasp on it. What i'm trying to do is return all of the data in the table. There is only 2 rows right now, but i'm not sure how to put all the data in an array and then return it all the way back to the $results variable.

Please help.

<?php
include('dates.php');

$events = new dates();
echo $events->getNumberEvents(); <-----returns the number of rows in the table (Works as expected)

$results = $events->getRows(); <------Doesn't work.

?>

Dates Class

<?php
/********************************************
* Class to connect and get dates from a database 
*********************************************/
require 'phpDatabaseClass.php';

class dates{

    private $db;
    private $arr2 = array();

    /****************************
    * Constructor which creates the $db variable
    ****************************/
    function __construct() 
    {
        $this->db = new phpDatabaseClass();
    }   

    /****************************
    * Function which calls the database class and returns the number of rows
    ****************************/
    public function getNumberEvents()
    {   
        $sql = "select count(*) from events";           
        return $this->db->queryNumber($sql);
    }

    /****************************
    * Function which calls the database class and returns the actual rows
    ****************************/
    public function getRows() 
    {
        $sql = "select * from events";

        $this->arr2 = $this->db->queryString($sql);

        foreach($this->arr2 as $key=>$val)
        {
            //$this->arr2 = $val;
            //echo $val;    
        }

    }
}
?>

phpDatabaseClass

<?php
/********************************************
* Class to connect and query a mysql database 
*********************************************/
require 'config.php';

class phpDatabaseClass {

    private $db;
    private $arr = array();

    /****************************
    * Constructor function which makes the connection to the database
    ****************************/
    public function __construct()
    {
        try 
        {
            $this->db = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USERNAME, DB_PASSWORD);
        }
        catch(PDOexception $e)
        {
            echo 'Can\'t connect to database. Please contact the site administrator';
        }
    }

    /****************************
    * Function which returns the number of rows
    ****************************/
    public function queryNumber($sql)
    {                           
        $stmt = $this->db->prepare($sql);
        $stmt->execute();
        $resultsCount = $stmt->fetchColumn();

        return $resultsCount;
    }


    /****************************
    * Function which returns the data in the rows
    ****************************/
    public function queryString($sql)
    {                           
        $stmt = $this->db->prepare($sql);

        $stmt->execute();

        $results = $stmt->fetch(PDO::FETCH_ASSOC);

        foreach($results as $key=>$val)
        {
            $this->arr = $val;
            //echo $this->arr;
        }

        return array($this->arr);
    }
}
?>
+2  A: 
public function getRows() 
{
    $sql = "select * from events";

    $this->arr2 = $this->db->queryString($sql);
    $return = array();
    foreach($this->arr2 as $key=>$val)
    {
        $return[$key] = $val;  
    }
    return $return;
}

That is if you wish to do anything at that stage. To simply return you can do:

return $this->db->queryString($sql);

To echo do:

foreach($results as $result)
{

    echo $result->fieldname;

}

To echo everything do:

foreach($results as $result)
{
    foreach($result as $key => $value)
    {
        echo $key.': '.$value;
    }

}

The reason for accessing as an object and not an array is that mysqli returns rows as objects.

Gazler
If i just return $this->db->queryString($sql), then how do i output all the values? If i do this in my original call: $results = $events->getRows(); foreach($results as $key=>$val) { echo $val; } it only prints out the last column of the first row of data.
Catfish
Original answer edited.
Gazler
but what if i don't want to echo in the getRows function and i just want to return the results all the way to the original call?
Catfish
what is fieldname suppose to be referring to?
Catfish
The field from the db you wish to print. If you do print_r($result) in the foreach loop, it will show you the whole object and you will see the fields there.
Gazler
ok using print_r i see that the array contains all the data, but i'm still not sure what to replace fieldname with. the first few fields of the array are [id]=>1 [heading]=>Christmas Party etc.....
Catfish
If $result is an array, and you wish echo the heading, you do:echo $result['heading'];
Gazler
That makes sense, but what if i want to echo the entire array though using a foreach loop?
Catfish
Updated original. If that isn't what you want then I am afraid you will have to be far more specific.
Gazler
Yep i just figured that out right before you posted it. I'm still unsure why i would have to use a nested foreach loop though. I guess that every row in the data base is returned as it's own array.
Catfish
Exactly that, $results is an array of arrays, hence the double foreach.
Gazler