Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a large foreach loop that has other loops and conditionals inside it. At the top of the code I am modifying I wish to a value out of the option_data array - how is this done?

foreach ($this->cart->getProducts() as $product) {

    $option_data = array();
    foreach ($product['option'] as $option) {
        if ($option['type'] != 'file') {
            $value = $option['option_value'];   
        } else {
            $filename = $this->encryption->decrypt($option['option_value']);

            $value = utf8_substr($filename, 0, utf8_strrpos($filename, '.'));
        }               
        $option_data[] = array(     
            'cid'       => $option['option_value_id'], // WANT THIS CID VALUE                          
            'name'  => $option['name'],
            'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
            'type'  => $option['type']
        );
    }


    $sql_get_colour_image=mysql_query("
    SELECT
        imagefb_url
    FROM
        `ocm1__product_image_fb`
    WHERE 
        `pid`=`".$product['product_id']."`
    AND
        `cid`= //GOES HERE
    ");
share|improve this question
cid exist in a inner loop from the context your trying to refer it – DevZer0 Jul 4 at 8:16
How do I take $option['option_value_id'] out and place it in my MySQL query? Thanks for the reply – TheBlackBenzKid Jul 4 at 8:22
which $option['option_value_id'] the first or the last ? – DevZer0 Jul 4 at 8:26
I want to get the cid from the $option_data[] and place it in the cid call of the MySQL – TheBlackBenzKid Jul 4 at 8:37
$option_data[] can have multiple cid values – DevZer0 Jul 4 at 8:38
show 3 more comments

1 Answer

up vote 1 down vote accepted

You can try this:

    $option_data = array();
    foreach ($product['option'] as $option) {
    if ($option['type'] != 'file') {
        $value = $option['option_value'];   
    } else {
        $filename = $this->encryption->decrypt($option['option_value']);

        $value = utf8_substr($filename, 0, utf8_strrpos($filename, '.'));
    }               
    $option_data[] = array(     
        'cid'       => $option['option_value_id'], // WANT THIS CID VALUE                          
        'name'  => $option['name'],
        'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
        'type'  => $option['type']
    );
    $sql_get_colour_image=mysql_query("
        SELECT
          imagefb_url
        FROM
          `ocm1__product_image_fb`
        WHERE 
          `pid`=".$product['product_id']."
          AND `cid`= ".$option['option_value_id']."
    ");
  }

Alternative you can:

$option_data = array();
$cids = array();
foreach ($product['option'] as $option) {
    if ($option['type'] != 'file') {
        $value = $option['option_value'];   
    } else {
        $filename = $this->encryption->decrypt($option['option_value']);

        $value = utf8_substr($filename, 0, utf8_strrpos($filename, '.'));
    }               
    $option_data[] = array(     
        'cid'       => $option['option_value_id'], // WANT THIS CID VALUE                          
        'name'  => $option['name'],
        'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
        'type'  => $option['type']
    );
    $cids[] = $option['option_value_id'];
}
$sql_get_colour_image=mysql_query("
    SELECT
        imagefb_url
    FROM
        `ocm1__product_image_fb`
    WHERE 
        `pid`=".$product['product_id']."
    AND
        `cid` IN (".implode(",",$cids).")
");
share|improve this answer
you moved his query to the inner loop, i don;t think thats what he wants – DevZer0 Jul 4 at 8:48
That was the problem. I had to move it into the inner loop to get the value too! Thanks for noticing that inner loop too. – TheBlackBenzKid Jul 4 at 8:49
@DevZer0 well that was the only option ... or to have cid IN (implode array) – Stephan Jul 4 at 8:49
@TheBlackBenzKid glad i could help :) cheers mate – Stephan Jul 4 at 8:49
1  
@TheBlackBenzKid done , i've updated my answer – Stephan Jul 4 at 9:24
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.