WarpConduit Computing

  • Quick Tips
  • Web Development
  • WordPress Plugins
  • Home
  • Passphrase Generator
  • Password Generator
  • About
  • Contact

Automatic Category Images for OpenCart 1.4.9.3

February 1, 2011 by Josh Hartman

The changes set forth below will cause OpenCart to assign a random product image from the category as the category image if one has not already been set. If the category contains no products, no_image.jpg will still display.

Make the following changes around line 112 of catalog/controller/product/category.php.

foreach ($results as $result) {
	if ($result['image']) {
		$image = $result['image'];
	} else {
/* START AUTOMATIC CATEGORY IMAGES */
      		$catimg = $this->model_catalog_product->getRandomProductImageByCategoryId($result['category_id']);
      		if($catimg){
      			$image = $catimg['image'];
      		}else{
      			$image = 'no_image.jpg';
      		}
/* END AUTOMATIC CATEGORY IMAGES */
	}
	
	$this->data['categories'][] = array(
		'name'  => $result['name'],
		'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url),
		'thumb' => $this->model_tool_image->resize($image, $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'))
	);
}

Add this function to catalog/model/catalog/product.php.

public function getRandomProductImageByCategoryId($category_id) {
	$sql = "SELECT p.image FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id) WHERE p.status = '1' AND p.image != '' AND p.date_available <= NOW() AND p2c.category_id = '" . (int)$category_id . "' ORDER BY RAND() LIMIT 1";
			
	$query = $this->db->query($sql);
									  
	return $query->row;
}

That’s it, enjoy your automatically generated and random category images!

Filed Under: Web Development Tagged With: automatic, category, image, opencart, php, random

Listing available variables, constants, functions, classes in PHP

January 25, 2011 by Josh Hartman

A short post today to help you in debugging your applications. You may find yourself someday wondering what variables are currently set and their contents, or what constants have been set by an application or framework you are coding. Well the answer is rather simple using var_dump function. Here is the breakdown:

List all defined variables with their respective values:

var_dump(get_defined_vars());

List all defined constants with their respective values:

var_dump(get_defined_constants());

List all defined internal and user functions:

var_dump(get_defined_functions());

List all declared classes:

var_dump(get_declared_classes());

That’s it, I hope you get some use out of these functions like i did!

Filed Under: Web Development Tagged With: classes, constants, dump, functions, list, php, variables

Enabling Gzip Compression of PHP, CSS, and JS Files Without mod_deflate

October 23, 2010 by Josh Hartman

Normally you can easily enable Gzip compression using mod_deflate by adding the following lines to your .htaccess file:


AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript

But for those of you on shared hosts that don’t allow the mod_deflate module and run PHP in CGI/FastCGI mode you can’t go with the easy method.

So, to serve up your PHP, CSS, and JS files you can try the following method.

Note 1: Your shared web hosting account must support custom php.ini and .htaccess files.

Note 2: Be careful when mixing this solution with other cache/compression applications.

Step 1: PHP Configuration

Add or modify the following lines in your custom php.ini file:

output_handler = Off
zlib.output_compression = On
zlib.output_handler = ob_gzhandler

Now this will take care of gzipping all PHP files.

Step 2: .htaccess Configuration

Add the following lines to the bottom of a .htaccess file in the root of your website.


RewriteEngine On
RewriteRule ^(.*\.js) gzip.php?type=js&file=$1
RewriteRule ^(.*\.css) gzip.php?type=css&file=$1

This will redirect all requests for .css and .js files through gzip.php, which we will create in the next step.

Step 3: File Processing PHP Script

The following PHP script will inherently use the PHP compression you’ve already enabled and also add headers to your files take advantage of your client’s browser cache to make subsequent loads faster.

Create a file named gzip.php in your website’s root and add the following lines to it:


Great! With these steps in place your css and javascript files will be processed by gzip.php and output using PHP’s gzip compression library (zlib).

This method can be extended to more filetypes by adding to the allowed file types in gzip.php and adding more lines to your .htaccess file.

Filed Under: Web Development Tagged With: css, deflate, gzip, htaccess, javascript, php

PHP’s in_array() vs. jQuery’s inArray()

October 6, 2010 by Josh Hartman

This one stumped me for a bit when i was trying to use jQuery to populate checkboxes with a list of items pulled from a database using PHP.

The fact of the matter is that PHP’s in_array() returns a boolean whereas jQuery’s inArray() returns the index of the matching element, or if the element is not found it will return -1 or undefined, depending on the browser.

Problem

If you are used to using PHP like me jQuery’s inArray() can give you a headache when you are using the returned value of the function in an if-else condition. This is because -1 evaluates as true, when you really want it to evaluate as false.

Solution

Make it so that your if-else condition checks for a value >=0 (greater than or equal to zero). Since array’s don’t have negative indexes you’re safe doing it this way.

What did i learn from this? When working with a new function check the documentation for what type of data it will return.

jQuery.inArray Documentation

Example


Filed Under: Web Development Tagged With: in_array, inarray, jquery, php

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • Next Page »

Connect

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • YouTube

Recent Posts

  • How to Permanently Remove the “Learn about this picture” Spotlight Wallpaper Icon From Your Windows Desktop
  • How to Quickly Test a Fax Machine
  • Extremely Useful Applications for Web Development and IT Tasks

Tags

automatic benchmark bigint class composer css embed escape event font function gzip helper htaccess html htmlspecialchars image increment javascript jquery list magento media mysql number observer opencart order output photo php profiling random redirect rijndael software text type ubuntu url windows windows 7 wordpress xampp xss

Blogroll

  • CodeIgniter
  • Fusion Forward
  • jQuery
  • Nettuts+
  • Smashing Magazine

© 2025 WarpConduit Computing. All Rights Reserved.