Help with displaying element from array

Discussion in 'PHP' started by gummybear, Oct 13, 2011.

  1. #1
    My php knowledge is very limited so please help me out.

    So basically there's a product with multiple options, containing stuff like image, name, value etc.
    
    $this->data['options'] = array();
    			
    foreach ($this->model_catalog_product->getProductOptions($this->request->get['product_id']) as $option) { 
    	if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') { 
    		$option_value_data = array();
    					
    		foreach ($option['option_value'] as $option_value) {
    			if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
    				$option_value_data[] = array(
    					'product_option_value_id' => $option_value['product_option_value_id'],
    					'option_value_id'         => $option_value['option_value_id'],
    					'name'                    => $option_value['name'],
    					'image'                   => $this->model_tool_image->resize($option_value['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height')),
    					'imagethumb'              => 'image/' . $option_value['image'],
    					'price'                   => (float)$option_value['price'] ? $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax'))) : false,
    					'price_prefix'            => $option_value['price_prefix']
    				);
    			}
    		}
    
    Code (markup):
    I have another template file that pulls info from this array:
    <img src="<?php echo $option_value['imagethumb']; ?>">
    Code (markup):
    There are multiple 'imagethumb's from the array. Right now the above code shows the last picture in the array. How can I echo the first imagethumb in the array??

    Please help. Thank you.
     
    gummybear, Oct 13, 2011 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    add a counter, starting before the foreach

    $counter = 0;

    then loop the foreach and the next line should be.

    $counter++;
    if ($counter == 1) { // display image }
     
    EricBruggema, Oct 14, 2011 IP