1

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'];
          }
       }
5
  • Try to show the array count with count($this->products)? Commented Sep 10, 2018 at 1:12
  • @NurMuhammad : count($this->products) = 3 Commented Sep 10, 2018 at 3:24
  • if you iterate over an array with three values and only get one back that means something is breaking your loop and your script has probably crashed. check your logs and/or enable error reporting. Commented Sep 10, 2018 at 4:36
  • Perhaps you may not have products with those IDs? Commented Sep 10, 2018 at 4:38
  • I have no error and the ID is correct Commented Sep 10, 2018 at 15:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

tk but it take always one value : string(15) "Key=3 - Value=2"
Hi @mike, I know you did a count($this->products). Was this just before the loop? If not try to do a count just before the loop. Looks like your $this->products getting overriden by by something else.
I inserted above the function.
Ok cool. Another point, is the where condition in your sql is correct ? ´where p.products_id = :products_id and p.products_id = pd.products_id` ? It looks both the same to me.
no, it's correct because you have after $QProducts->bindInt(':products_id', $key); - :products_id take the value of $key .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.