-2

I am trying to fetch and display users data using a while loop. I already created the get class and the result class to handle this but I think there is something I might be missing as I am new to OOP

i already tried using the result object but i get the **Notice: Undefined index: type in ** on the table

Here is my class

    private static $_instance = null;
    private $_pdo, 
            $_query, 
            $_error = false, 
            $_results,
            $_count = 0;

    private function __construct(){
        try{
            $this->_pdo = new PDO('mysql:host='. config::get('mysql/host') .';dbname='. config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
        }catch(PDOException $e){
            die($e->getMessage());
        }
    }
    public static function getInstance(){
    if(!isset(self::$_instance)){
        self::$_instance = new DB;
    }
    return self::$_instance;
    }

    public function query($sql, $params = array()){
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql)){
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }

            if($this->_query->execute()){
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            }else{
                $this->_error = true;
            }
        }
        return $this;
    }
    public function action($action, $table, $where = array()){
        if(count($where) === 3){
            $operators = array('=', '>', '<', '>=', '<=');

            $field   = $where[0];
            $operator   = $where[1];
            $value  = $where[2];

            if(in_array($operator, $operators)){
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";

                if(!$this->query($sql, array($value))->error()){
                    return $this;
                }
            }

        }
        return false;
    }
    public function get($table, $where){
        return $this->action("SELECT *", $table, $where);
    }
    public function results(){
        return $this->_results;
    }

Here is my while loop

<?php
                                               $transactions = DB::getInstance()->get('transactions', array('user_id', '=', $user->data()->user_id));
                                               if($transactions->count() < 1){
                                                   ?>
                                                   <td colspan='5'>No transaction yet on your account</td>
                                                   <?php
                                               }else{ 
                                                    while($trans = $transactions->results()){
                                               ?>
                                                   <th scope="row"><?php echo $trans->date?></th>
                                                   <td><?php echo $trans->type?></td>
                                                   <td><span class="label label-info"><?php echo $trans->description?></span></td>
                                                   <td><?php echo $trans->description?>/td>
                                                   <td>
                                                   <span class="label label-primary"><?php
                                                   if($trans->status === 0){
                                                       echo 'PENDING';
                                                   }else if($trans->status === 1){
                                                       echo 'PROCESSING';
                                                   }else{
                                                       echo 'CONFIRMED';
                                                   }
                                                   ?></span>
                                                   </td>
                                                   <?php
                                                   }
                                               }
                                               ?>

I expect that the individual row are displayed but I get the Notice: Undefined index: error

King Kesh
  • 27
  • 6

1 Answers1

-1

in your DB class you have

    public function results(){
        return $this->_results;
    }

and

...
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
...

which means that $transactions->results() is returning an array of results, from the database, not a single row as the you appear to be expecting.

This explains the first error, since you're trying to run $trans->date on an array.

Additionally since you're never modifying the results array $transactions->results() will continue returning the same array over and over again until it times out.

I haven't looked through all the code to see if there are any other issues, but to solve your immediate issue you just need to use a foreach loop instead of while

foreach ($transactions->results() as $trans) {
   ...

}
Urza
  • 219
  • 1
  • 8