php - beat by database query and displaying result

Joined
12 Feb 2006
Posts
17,628
Location
Surrey
i'm using opencart 1.40 and have come across a simple problem. i posted this on their forums but it's extremely slow to get a response and feel i'd get more help posting here. i've pretty much copy and pasted the thread i made so hope it makes enough sense. althoug it's for opencart, it's just simple a bit of php so should be ok.

from opencart thread:

i'm trying to show the manufacturer image rather then their name on the product page. i'm trying to edit the files and i'm so so close but last bit is beating me and i need help.

here is what i have (with what i think is not needed removed)

i added this to model/manufacuter.php page...

PHP:
   public function getManufacturerImg($manufacturer_name) {
      $query = $this->db->query("SELECT image FROM " . DB_PREFIX . "manufacturer WHERE name = '" . $manufacturer_name . "'");
   
      return $query->row;   
}

then on controller/product.php page added this...

PHP:
      $this->load->model('catalog/manufacturer');   
      
      $manu = $this->model_catalog_manufacturer->getManufacturerImg($product_info['manufacturer']);
      
      print_r($manu);
      
      $this->data['manu'] = array(); 
      
      foreach ($manu as $manus) {
            $this->data['manu'][] = array(
            'image' => $manus['image']
);
         }




print_r($manu); shows the exact string i want, which is the location of the image, great as then i guess my database query is fine, but on the template page i want it i'm stuck and it doesn't work, which i assume is down to me incorrectly doing something e.g. treating an array as a string

on view/product.tpl page i have the 3 following lines to try see if either will make a difference

PHP:
<?php print_r($manu); ?>
<?php echo $manu; ?>
<?php echo $manu['image']; ?>
print_r shows: Array ( [0] => Array ( [image] => d ) )
echo $manu shows the word Array,
echo $manu['image'] shows error message saying undefined call to...

where am i going wrong? sure it must be so simple and i just have one/two things slightly incorrect.

many thanks
 
This doesn't fix your problem, but rather than using the manufacturer name you should really be using its id in your queries:
$product_info['manufacturer_id']

If there are two manufacturers with the same name your method would not know which to choose.
 
This doesn't fix your problem, but rather than using the manufacturer name you should really be using its id in your queries:
$product_info['manufacturer_id']

If there are two manufacturers with the same name your method would not know which to choose.

good point, i only had access to the name at the time but changed it to id

It's a nested array
PHP:
echo $manu[0]["image"];

that throws up the following error :(

Fatal error: Cannot use string offset as an array in filenameremoved
 
something is resetting $manu. If the print_r is outputting: "Array ( [0] => Array ( [image] => d ) )", then accessing $manu[0]["image"] will be possible.

EDIT: What is print_r outputting at this stage on product.php:
PHP:
      $manu = $this->model_catalog_manufacturer->getManufacturerImg($product_info['manufacturer']);
      
      print_r($manu);
change it to use var_export as above, too.

also, this is a very good cause for using descriptive variable names, even if they are long names.
 
Last edited:
something is resetting $manu. If the print_r is outputting: "Array ( [0] => Array ( [image] => d ) )", then accessing $manu[0]["image"] will be possible.

EDIT: What is print_r outputting at this stage on product.php:
PHP:
      $manu = $this->model_catalog_manufacturer->getManufacturerImg($product_info['manufacturer']);
      
      print_r($manu);

this prints the following: Array ( [image] => data/apple_logo.jpg )

change it to use var_export as above, too.

changes the above to: array (
'image' => 'data/apple_logo.jpg',
)

also, this is a very good cause for using descriptive variable names, even if they are long names.

i guess something else is happening to the variable which is why it's not working? changing to name to something extremely random gives the same problem so it's not used else where, unless of course that wasn't your point?
 
Here's your fix, the cheque's in the post :p.

opencart_manufacturer.png


Revert your files to the original from 1.4.0 (take a backup) so the line numbers match up - I didn't use any of the above code anyway.

Step 1
Open:
\catalog\controller\product\product.php

After the following (around line 212):
PHP:
if ($product_info['quantity'] <= 0) {
	$this->data['stock'] = $product_info['stock'];
} else {
	if ($this->config->get('config_stock_display')) {
		$this->data['stock'] = $product_info['quantity'];
	} else {
		$this->data['stock'] = $this->language->get('text_instock');
	}
}

add:

PHP:
/** Manufacturer Image Start */
$manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($product_info['manufacturer_id']);
/** Manufacturer Image End */



Step 2
After:
PHP:
$this->data['manufacturer'] = $product_info['manufacturer'];
(a couple of lines down from above)

add:
PHP:
/** Manufacturer Image Start */
$this->data['manufacturer_image'] = image_resize($manufacturer_info['image'], $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'));
/** Manufacturer Image End */



Step 3
Open:
\catalog\view\theme\default\template\product\product.tpl

After:
PHP:
<?php if ($manufacturer) { ?>
<tr>
<td><b><?php echo $text_manufacturer; ?></b></td>
<td><a href="<?php echo $manufacturers; ?>"><?php echo $manufacturer; ?></a></td>
</tr>

Add:
PHP:
<?php /** Manufacturer Image Start */ ?>
<tr>
<td colspan="2" style="text-align: center;"><a href="<?php echo $manufacturers; ?>"><img src="<?php echo $manufacturer_image; ?>" title="<?php echo $manufacturer; ?>" alt="<?php echo $manufacturer; ?>" /></a></td>
</tr>			  
<?php /** Manufacturer Image End */ ?>


Done! I could uploaded for you the modified files, but where's the fun?
 
Last edited:
Oops, I've edited the above post, I used the wrong variables in the alt/title tags in the template, they should be:

PHP:
<td colspan="2" style="text-align: center;"><a href="<?php echo $manufacturers; ?>"><img src="<?php echo $manufacturer_image; ?>" title="<?php echo $manufacturer; ?>" alt="<?php echo $manufacturer; ?>" /></a></td>


I do have PayPal (sig) but there's no need :).
 
Oops, I've edited the above post, I used the wrong variables in the alt/title tags in the template, they should be:

PHP:
<td colspan="2" style="text-align: center;"><a href="<?php echo $manufacturers; ?>"><img src="<?php echo $manufacturer_image; ?>" title="<?php echo $manufacturer; ?>" alt="<?php echo $manufacturer; ?>" /></a></td>
I do have PayPal (sig) but there's no need :).

thanks for mentioning it but i noticed the mistake anyways, as well as adding a simple isset and strlen check to $manufacturer_info['image'] before image resize as some products may have no manufacturer which this throws up many errors.

thank you, this one thing has been holding me up for days, can't seem to focus on other issues whilst this one was hanging around.
 
Ah good point, I didn't think of doing that, mind you, you would have thought the resize function would do its own checks to make sure it exists first.

Heh I know what you mean, I have a load of small things to do and it's hard to choose which one to go for next.
 
Back
Top Bottom