Bong NakMadaraz
Bong NakMadaraz

Reputation: 25

Why can't instantiate my object to an array[]?

Why can't initiate my object to an array[]?

This is my work: clothing_product.php

include('../database.php');

class Clothing_Product {

    public $db;
    public $procode;
    public $probprice;
    public $proprice;
    public $prodes;
    public $propath;
    public $prostatus;
    public $image;

        public function __construct() { 
            $this->db = new MySqlDatabase();
        }

                    public function get_all(){
            return self::get_by_query('SELECT * FROM tblclothing_product;');
        }

        public function get_by_query($sql=""){
            $result_set = $this->db->query($sql);
            $object_array = array();
            while($row = $this->db->fect_array($result_set)){

                $object_array[] = $this->instantiate($row);
                echo var_dump($object_array);
            }
            return $object_array;
        }

        private function instantiate($record){
            //Dynamic control
            $object = new self;
            foreach($record as $attribute=>$value){
                if($object->has_attribute($attribute)){
                    $object->attribute = $value;
                }
            }
            return $object;
        }       

        private function has_attribute($attribute){
            $object_vars = get_object_vars($this);
            return array_key_exists($attribute,$object_vars); 
        }

                    public function add_new($a,$b,$c,$d,$e,$f){
            $this->procode = $a;
            $this->probprice = $b;
            $this->proprice = $c;
            $this->prodes = $d;
            $this->propath = $e;
            $this->prostatus = $f;
            $sql="INSERT INTO `tblclothing_product`(`proCode`,`proBPrice`,`proPrice`,`proDes`,`proPath`,`proStatus`) VALUES(
                                            '" . $a ."',
                                            '" . $b ."',
                                            '" . $c ."',
                                            '" . $d ."',
                                            '" . $e ."',
                                            " . $f ."
                                            )";
            $this->db->query($sql);

        }



        public function image_string($a,$b,$c,$d,$e){

            $this->image='';
            if($a!=""){
                $this->image = $this->image. $a .";";
            }
            if($b!=""){
                $this->image = $this->image. $b .";";
            }
            if($c!=""){
                $this->image = $this->image. $c .";";
            }
            if($d!=""){
                $this->image = $this->image. $d .";";
            }
            if($e!=""){
                $this->image = $this->image. $e;
            }

            return $this->image;
        }



        public function remove_by_id($id){
            $this->db->query('DELETE FROM tblclothing_product WHERE proId={$id};');
        }



        public function get_by_id($id=0){
            $result_array = self::get_by_query('SELECT * FROM tblclothing_product WHERE proId={$id} LIMIT 1;');
            return !empty($result_array) ? array_shift($result_array) : FALSE;
        }

        public function image_exploit_string(){
            $arr = array();
            if($this->image != ""){
                $arr = explode(";",$this->image);
            }

            return $arr;

        }   




    }

I try to echo the above script var_dump($object_array) my array and it has shown null to every object. It looks like it can't initiate at all.

In clothing_view.php it is a script to view my array of object but it has shown null.

require_once("../include/function.php");
require_once("../include/database.php");

$buttonname = $_POST["submit"];

$image1 = '';
$image2 = '';
$image3 = '';
$image4 = '';
$image5 = '';
$image1 = $_FILES['image1']['name'];
$image2 = $_FILES["image2"]['name'];
$image3 = $_FILES["image3"]['name'];
$image4 = $_FILES["image4"]['name'];
$image5 = $_FILES["image5"]['name'];

    $obj = new Clothing_Product();
$items =  $obj->get_all();

foreach($items as $item){
    echo "ProId:".$item->procode."<br />";
    echo "ProPath:".$item->propath."<br />";
}

In database.php:

require_once('config.php');

class MySqlDatabase
{
    private $connection;
    private $last_query;
    private $magic_quotes_active;
    private $real_escape_string_exist;

        function __construct(){

            $this->open_connection();
            $this->magic_quotes_active = get_magic_quotes_gpc();
            $this->real_escape_string_exist = function_exists("mysql_real_escape_string");


        }

        private function open_connection()
        {

            $this->connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
            if (!$this->connection){
                die("Connection failed!". mysql_error());
            }
            else{
                $db_select = mysql_select_db(DB_NAME);
                if(!$db_select){
                    die("Select database failed". mysql_error());
                }
            }

        }

        public function query($sql){

            $this->last_query = $sql;
            $result = mysql_query($sql,$this->connection);

            $this->confirm_query($result);
            return $result;

        }

        public function confirm_query($result){
            if(!$result){
                $output = "Database query failed: " . mysql_error()."<br /><br />";
                $output.= "Last query that fail is:" . $this->last_query;
                die($output);
            }
        }

        private function escape_value($value) {

            if ($this->real_escape_string_exist) {
                if($this->magic_quotes_active) {$value = stripslashes($value);}
                $value = mysql_real_escape_string($value);
            }
            else {
                if (!$this->magic_quotes_active) {$value = addslashes($value);}
            }
            return $value;
        }

        public function fect_array($result){
            return mysql_fetch_array($result);
        }

        public function num_rows($result){
            return mysql_num_rows($result);
        }

        public function last_id(){
            return mysql_insert_id($this->connection);
        }

        public function affected_rows(){
            return mysql_affected_rows($this->connection);
        }

        public function close_connection(){
            if(isset($this->connection)){
                mysql_close($this->connection);
                unset($this->connection);
            }
        }



}

Is there problem with $item->? It always returns null.

Upvotes: 1

Views: 201

Answers (3)

Kerem
Kerem

Reputation: 11586

Finally, I got what you trying to do.. If you need db result as object (ORM-like), you can use another class for this (just an advise).

Pseudo;

// Let's call another class as CProduct
class CProduct
{
    public $procode, $probprice,
           $proprice, $prodes,
           $propath, $prostatus,
           $image;

    public function __construct($row) {
        foreach ($row as $key => $val) {
            if ($this->_hasAttr($key)) {
                $this->$key = $val;
            }
        }
    }

    private function _hasAttr($key) {
        return array_key_exists($key, get_class_vars($this));
    }

    // you can also extend your class here
    // and use it anywhere
    public function printCode() {
        print $this->procode;
    }
}

And using in your case;

while ($row = $this->db->fect_array($result_set)) {
    $object_array[] = new CProduct($row);
}

Html part;

<p>Product Code: <?php $cp->printCode(); ?></p>

Upvotes: 1

dualed
dualed

Reputation: 10512

I think your problem is that you set the object properties in the instantiate function with

$object->attribute = $value;

instead of the likely more useful

$object->{$attribute} = $value;

Even if it does not solve your problem right away, this would likely make you pull your hair later...

Upvotes: 1

Kerem
Kerem

Reputation: 11586

As far as I see, self::get_by_query will never work cos it is not defined as static function.

Opt. 1- You can define it so: public static function get_by_query
Opt. 2- You can call it so: $this->get_by_query

PS: var_dump doesn't need echo.

Upvotes: 1

Related Questions