Jump to content



Latest News: (loading..)

- - - - -

Help with IF statement


  • Please log in to reply
11 replies to this topic

#1   al3ks

al3ks
  • Members
  • 288 posts
  • Real Name:Aleksander
  • Gender:Male
  • Location:UK

Posted 26 June 2012 - 07:16 PM

Can anyone help me put this statement right please?

 if ($product['products_special_badge'] == 1) {
			 $badges = '<div class="productBadge">' . tep_image(DIR_WS_ICONS . 'b_red.png') . '';
			 } elseif ($product['products_special_badge'] == 4) {
			 $badges = '<div class="productBadge">' . tep_image(DIR_WS_ICONS . 'b_gold.png') . '';
			 } elseif ($product['products_special_badge'] == 3) {
			 $badges = '<div class="productBadge">' . tep_image(DIR_WS_ICONS . 'b_orng.png') . '';
			 } elseif ($product['products_special_badge'] == 0) {
			 $badges = '<div class="productBadge">' . tep_image(DIR_WS_ICONS . 'b_blue.png') . '';
			 }

			  $badges = '<div class="productBadgeText">' . $product['products_special'] . '</div></div>';

What I basically want it to do is to output a different image depending on whether $product['products_special_badge'] is saved as option 1,2,3 etc. And always output the bottom line $product['products_special']

if option 1 output this
if option 2 output that... and so on then always output the bottom line "$product['products_special']"

This should be kind like a function which I can then call to output with this $badges


Would be grateful for your help
Regards

Edited by al3ks, 26 June 2012 - 07:18 PM.

Find this post helpful? Click the 'Like this' button. :)

#2   FWR Media

FWR Media
  • Community Sponsor
  • 6,837 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 26 June 2012 - 07:50 PM

This should do you.

$badges = '';
if ( isset( $product ) && is_array ( $product ) ) {
  $badge_images = array( 0 => 'b_blue.png', 1 => 'b_red.png', 3 => 'b_orng.png', 4 => 'b_gold.png' );
  $products_special = array_key_exists ( 'products_special', $product ) ? (string)$product['products_special'] : 'No product special available';
  $products_special_badge = array_key_exists ( 'products_special_badge', $product ) &&
							array_key_exists ( (int)$product['products_special_badge'], $badge_images ) ?
							tep_image ( DIR_WS_ICONS . $badge_images[(int)$product['products_special_badge']] ) : 'No badge image to display';
  $badges = '<div class="productBadge">' . $products_special_badge;
  $badges .= '<div class="productBadgeText">' . $products_special . '</div></div>' . PHP_EOL;
}

Hmmm the code editor doesn't maintain spacing so sorry if it looks a bit odd.

Edited by FWR Media, 26 June 2012 - 07:53 PM.


#3   burt

burt

    Code Monkey

  • Community Team
  • 7,745 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 26 June 2012 - 09:45 PM

And another way (may [or may not!] be easier for a non coder to see what's going on);

switch($product['products_special_badge']) {
  case 1:
  $image = tep_image(DIR_WS_ICONS . 'b_red.png');
  break;
  case 4:
  $image = tep_image(DIR_WS_ICONS . 'b_gold.png');
  break;
  case 3:
  $image = tep_image(DIR_WS_ICONS . 'b_orng.png');
  break;
  default:
  case 0:
  $image = tep_image(DIR_WS_ICONS . 'b_blue.png');
  break;
}
$badges = '<div class="productBadge">' . $image . '<div class="productBadgeText">' . $product['products_special'] . '</div></div>';

echo $badges;

Edited by burt, 26 June 2012 - 09:46 PM.

Dummies guide to designing osCommerce 2.3 Click Me

Or maybe a ready made theme for your shop ??

Warning: My posts may contain Horsemeat.

#4   FWR Media

FWR Media
  • Community Sponsor
  • 6,837 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 26 June 2012 - 09:55 PM

Yup many ways to skin this particular cat.

In both our cases however we forgot to use tep_output_string_protected() on
$products_special/$product['products_special']
Very naughty not escaping.

#5   al3ks

al3ks
  • Members
  • 288 posts
  • Real Name:Aleksander
  • Gender:Male
  • Location:UK

Posted 26 June 2012 - 11:20 PM

Thanks for your replies.

I have tried both examples and unfortunately they didn't work.

@FWR Media I mostly understand how this code is supposed to work but after implementing it it doesn't seem to make any impact on my website, but I have no idea how to adjust this code to make it work.

@burt this seems much easier to understand but for some reason it outputs always only one colour/case, and it is not always the default one but case 0.

I have organised the code a bit more to my needs, so it looks like this:

switch($product['products_special_badge']) {
  case 0:
  $image = tep_image(DIR_WS_ICONS . 'b_blue.png');
  break;
  case 2:
  $image = tep_image(DIR_WS_ICONS . 'b_green.png');
  break;
  case 3:
  $image = tep_image(DIR_WS_ICONS . 'b_orng.png');
  break;
  case 4:
  $image = tep_image(DIR_WS_ICONS . 'b_gold.png');
  break;
  default:
  case 1:
  $image = tep_image(DIR_WS_ICONS . '_b_red.png');
  break;
}
$badges = '<div>' . $image . '<div>' . $product['products_special'] . '</div></div>';

echo $badges;

I have also got a similar bit of code in admin/categories.php which I used to build the options for the $product['products_special_badge']
It looks like this:

if (!isset($pInfo->products_special_badge)) $pInfo->products_special_badge = '1';
	switch ($pInfo->products_special_badge) {
	  case '0': $red = false; $green = false; $orange = false; $gold = false; $blue = true; $none = false; break;
	  case '2': $red = false; $green = true; $orange = false; $gold = false; $blue = false; $none = false; break;
	  case '3': $red = false; $green = false; $orange = true; $gold = false; $blue = false; $none = false; break;
	  case '4': $red = false; $green = false; $orange = false; $gold = true; $blue = false; $none = false; break;
	  case '5': $red = true; $green = false; $orange = false; $gold = false; $blue = false; $none = false; break;
	  case '1':
	  default: $red = false; $green = false; $orange = false; $gold = false; $blue = false; $none = true;
	}

Edited by al3ks, 26 June 2012 - 11:21 PM.

Find this post helpful? Click the 'Like this' button. :)

#6   burt

burt

    Code Monkey

  • Community Team
  • 7,745 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 27 June 2012 - 08:34 AM

Ensure that $product['products_special_badge'] has a (int)value.
Dummies guide to designing osCommerce 2.3 Click Me

Or maybe a ready made theme for your shop ??

Warning: My posts may contain Horsemeat.

#7   burt

burt

    Code Monkey

  • Community Team
  • 7,745 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 27 June 2012 - 10:43 AM

By this I mean, ensure that it is passing a numerical value, and not just NULL (aka blank, nothing).
Dummies guide to designing osCommerce 2.3 Click Me

Or maybe a ready made theme for your shop ??

Warning: My posts may contain Horsemeat.

#8   FWR Media

FWR Media
  • Community Sponsor
  • 6,837 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 27 June 2012 - 11:11 AM

Quote

@FWR Media I mostly understand how this code is supposed to work but after implementing it it doesn't seem to make any impact on my website, but I have no idea how to adjust this code to make it work.

The code doesn't need adjustment, you can simply modify/add to the array if you wish to add badges.

If products_special_badge does not contain the required data then obviously no code is going to work.

#9   al3ks

al3ks
  • Members
  • 288 posts
  • Real Name:Aleksander
  • Gender:Male
  • Location:UK

Posted 27 June 2012 - 11:59 PM

View Postburt, on 27 June 2012 - 08:34 AM, said:

Ensure that $product['products_special_badge'] has a (int)value.

View PostFWR Media, on 27 June 2012 - 11:11 AM, said:

The code doesn't need adjustment, you can simply modify/add to the array if you wish to add badges.

If products_special_badge does not contain the required data then obviously no code is going to work.

I have the $product['products_special_badge'] set as int, still the same though I noticed the first product in each page never has a badge (not sure if it was like that previously)

@FWR Media I know that, I have tried a couple of different products to add the badge and nothing changes on the website.

I have manged to make it work in another file (before starting this topic) which is from the modular front page. You can have a look on the homepage> www.knnutrition.co.uk

And I'm trying to implement it to product_listing.php but its much more complicated.

Edited by al3ks, 28 June 2012 - 12:02 AM.

Find this post helpful? Click the 'Like this' button. :)

#10 ONLINE   multimixer

multimixer

    Lemons or Melons ?

  • Partner
  • 4,389 posts
  • Real Name:George Zarkadas
  • Gender:Male
  • Location:Greece

Posted 28 June 2012 - 03:17 AM

View Postal3ks, on 27 June 2012 - 11:59 PM, said:

And I'm trying to implement it to product_listing.php but its much more complicated.

Are you sure then that $product['products_special_badge'] has any value? Where do you get this value from? How do you build the array $product ?

I'm just wondering, because for product_listing.php, all product info is in array $listing (eg $listing['products_name']) that comes from the query on index.php

Check again if you query for products_special_badge and if you use the correct array

#11   al3ks

al3ks
  • Members
  • 288 posts
  • Real Name:Aleksander
  • Gender:Male
  • Location:UK

Posted 28 June 2012 - 09:48 AM

@multimixer

I'm using a thumbnail product listing contrib which has this code in it

$data_ok = ($listing_split->number_of_rows > 0);
	if ((PRODUCT_LIST_DESCRIPTION ) && $data_ok) {
		$listing_query = tep_db_query($listing_split->sql_query);
		while ($products = tep_db_fetch_array($listing_query)) $id_array[] = $products['products_id'];  
		$pid_string = implode(',', array_unique($id_array));
	$product_query = tep_db_query("select products_id, p.products_special, p.products_special_badge, products_description from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id in (" . $pid_string . ") and language_id = '" . (int)$languages_id . "'");
		while ($products = tep_db_fetch_array($product_query)) {

All other info in that file is pulled the same way, eg $products['products_name']

I have created the option radio boxes in admin categories.php to choose a badge for an individual product, it works correctly, it saves the correct information to db and outputs well in my "special offers" section on my homepage, where I managed to implement it.
But it doesn't work the same way in the code for product_listing.php

This is a section of the code in that file which builds the thumbnail for each product:

$name =	'<div class="productsHome-Listing">' . $link . $products['products_name'] . '</a></div>'. ($products['short_desc'] && (PRODUCT_SHORT_DESC == 'true') ? '' . $products['short_desc'] . '<br />' : '') . product_description($products['products_id'],$link, true);	
		   	
$price = (function_exists(display_short_price)) ? $currencies->display_short_price($products['products_price'], tep_get_tax_rate($products['products_tax_class_id'])) : $currencies->display_price($products['products_price'], tep_get_tax_rate($products['products_tax_class_id']));	

		$show_price = '<div class="productsHome-price">' . ($products['products_price'] > 0 ? $price : 'P.O.A') . '</div>';
	if (!PRODUCT_LIST_PRICE) $show_price = '';
	$model=(PRODUCT_LIST_MODEL ? $products['products_model'] . '' :'');
	
	$info_box_contents[$row][$col] = array('align' => 'center',
										   'params' => ' class="productsHome-main" width="'.(100/PRODUCTS_PER_ROW).'%" valign="top"',
										   'text' => $border . '<div class="productImage"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $image, $products['products_name'], PRODUCT_IMAGE_WIDTH, '') . '</a></div>' . '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products['products_id']) . '">'. ($products['short_desc'] && (PRODUCT_SHORT_DESC == 'true') ? $products['short_desc'] . '' : '') . $name . $show_price . $badges);

Edited by al3ks, 28 June 2012 - 09:49 AM.

Find this post helpful? Click the 'Like this' button. :)

#12 ONLINE   multimixer

multimixer

    Lemons or Melons ?

  • Partner
  • 4,389 posts
  • Real Name:George Zarkadas
  • Gender:Male
  • Location:Greece

Posted 28 June 2012 - 02:49 PM

I guess you are also sure you have the badge switch within the loop?

What I would do first, is to check if there is anything inside $product['products_special_badge']. Print it out just beside the product name, so you can check