TheBlackBenzKid
TheBlackBenzKid

Reputation: 27087

How do I get an variable outside of an array in PHP for MySQL?

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
    ");

Upvotes: 0

Views: 95

Answers (1)

Stephan
Stephan

Reputation: 8090

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).")
");

Upvotes: 1

Related Questions