mike
mike

Reputation: 65

PHP Array (just one result)

In my loop, I have only one result.

The array result:

   array(3) {
    [3]=>
    array(1) {
         ["qty"]=> int(2)
       }
    [1]=>
    array(1) {
        ["qty"]=> int(3)
       }
    [2]=>
      array(1) {
        ["qty"]=> int(4)
      }
    }

If I write this inside the loop, it displays just one one result:

var_dump('Key=' . $key .' - Value=' . $value['qty']);

My loop:

foreach ($this->products as $key => $value) {

        $QProducts = $this->db->prepare('select p.products_id,
                                                pd.products_name,
                                                p.products_model
                                          from :table_products p,
                                               :table_products_description pd
                                          where p.products_id = :products_id
                                          and p.products_id = pd.products_id
                                        ');

        $QProducts->bindInt(':products_id', $key);
        $QProducts->execute();

        echo $QProducts->value('products_name');
}

How can I solve this problem?

Below the main element of the function . It can help you.

public function OrderProduts() {
          $prod_array = $this->orderResult['Cart']['contents'];

          foreach ($prod_array as $key => $value) {
            $QProducts = $this->db->prepare('select p.products_id,
                                                    pd.products_name
                                              from :table_products p,
                                                   :table_products_description pd
                                              where p.products_id = :products_id
                                              and p.products_id = pd.products_id
                                            ');

            $QProducts->bindInt(':products_id', $key);
            $QProducts->execute();

            echo $Products->value('products_name);
            echo $value ['qty'];
          }
       }

Upvotes: 1

Views: 173

Answers (1)

Anjana Silva
Anjana Silva

Reputation: 9201

It seems like the products works as pass by reference sort of a thing. Try assigning products to a variable,

$prodArray = $this->products

and iterate through the variable ($prodArray) instead of $this->products.

Debugging

Assign product in each iteration to a temprorary array ($productsArrayTemp) like this.

$productsArrayTemp = array(); //a temporary array.   
public function OrderProduts() {
    $prod_array = $this->orderResult['Cart']['contents'];

    foreach ($prod_array as $key => $value) {
        $QProducts = $this->db->prepare('select p.products_id,
                                            pd.products_name
                                      from :table_products p,
                                           :table_products_description pd
                                      where p.products_id = :products_id
                                      and p.products_id = pd.products_id
                                    ');

        $QProducts->bindInt(':products_id', $key);
        $QProducts->execute();

        //I am not too sure about the name of the object. 
        //But you need to push each and every object to an array.
        $this->productsArrayTemp[] = $Products;

  }
}

Then try counting that array,

count($this->productsArrayTemp)

If this results brings the expected number of results, then you can use this array (instead of $this->products) to access its sub objects.

The reason for doing this is to eliminate the fact that $this->products being overridden somewhere in your code.

I hope this helps.

Upvotes: 1

Related Questions