0

I have array like this

Array
(
[0] => 149
[1] => 150
)

how to make foreach loop and query ? i like each of this $values to match result and return

$damasks = $cart['damask_id']; //array data
            foreach ($damasks as $key => $value) {

            }

Complete Example:

foreach ($damasks as $key => $value) 
{ 
    $damask_query = $this->db->query("
        SELECT oc_damask_name.name 
        FROM " . DB_PREFIX . "cart 
        LEFT JOIN oc_damask_frontend ON ( $value = oc_damask_frontend.damask_id) 
        LEFT JOIN oc_damask_name ON ( oc_damask_frontend.damask_id = oc_damask_name.id ) WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' 
        AND customer_id = '" . (int)$this->customer->getId() . "' 
        AND session_id = '" . $this->db->escape($this->session->getId()) . "'"); 
}
12
  • echo $value; will return 149 and 150, what kind of match result u have? Commented Jan 23, 2019 at 14:19
  • yes if i echo inside foreach will return both and if i do inside foreach query is returning only one value Commented Jan 23, 2019 at 14:33
  • share complete example, Commented Jan 23, 2019 at 14:34
  • foreach ($damasks as $key => $value) { $damask_query = $this->db->query("SELECT oc_damask_name.name FROM " . DB_PREFIX . "cart LEFT JOIN oc_damask_frontend ON ( $value = oc_damask_frontend.damask_id) LEFT JOIN oc_damask_name ON ( oc_damask_frontend.damask_id = oc_damask_name.id ) WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'"); } Commented Jan 23, 2019 at 14:35
  • ( $value = oc_damask_frontend.damask_id) equal to column name? Commented Jan 23, 2019 at 14:42

1 Answer 1

1

According to your example and comment, you are not storing data into an array and printing outside the loop, which will only print last index.

you need to store result into an array like: $damask_query[]

Example:

$damask_query = array(); // initialize
foreach ($damasks as $key => $value) 
{ 
    $damask_query[] = $this->db->query(" // change in this line
        SELECT oc_damask_name.name 
        FROM " . DB_PREFIX . "cart 
        LEFT JOIN oc_damask_frontend ON ( $value = oc_damask_frontend.damask_id) 
        LEFT JOIN oc_damask_name ON ( oc_damask_frontend.damask_id = oc_damask_name.id ) WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' 
        AND customer_id = '" . (int)$this->customer->getId() . "' 
        AND session_id = '" . $this->db->escape($this->session->getId()) . "'"); 
}

print_r($damask_query); // will print multiple records store in array.
Sign up to request clarification or add additional context in comments.

4 Comments

now i get to another problem ... how to access them ? Array ( [0] => stdClass Object ( [num_rows] => 2 [row] => Array ( [name] => test 6 ) [1] => stdClass Object ( [num_rows] => 2 [row] => Array ( [name] => test 7 ) )
solution is that, foreach($damask_query as $newVal){ echo $newVal->name;} @bobibobi
i face another problem if are only 1 item in cart this work fine but if is more than one all other items have the previews product damask look like its stack in that array any ideas how to fix it ?
@bobibobi: please create a new question with this problem.

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.