Zen Cart Logo
Forums / General Questions / PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

Views: 1,151

Results 1 to 14 of 14
13 May 2015, 6:01 PM
#1
suedouglas avatar

suedouglas

New Zenner

Join Date:
Nov 2008
Posts:
45
Plugin Contributions:
0

PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

Not sure if this is the right forum for php questions, but I'm having trouble finding a PHP forum

I'm trying to display the manufacturers image on the product info page which when clicked goes to their url to see all that manufacturers products

My coding is in the following .php file:
/includes/templates/mytemplate/templates/tpl_product_info_display.php The problem is that only the first manufacturers image is shown on all products, even those that belong to the second manufacturer. Can anyone spot what I have wrong please? (I've resorted to hardcoding the urls and images which is why I need an if statement, as I couldn't get it to work at all when trying to get the manufacturers url and image from the database, and since there are only two of us ...!)

This is my problem coding:

<?php $manufacturers_name = zen_get_products_manufacturers_name($product_info->fields['products_id']); If ($manufacturers_name = "Sandie Burchell") { ?>

<a href="index.php?main_page=index&manufacturers_id=3"> <img src="/images/sandie_burchell.jpg"> </a>

<?php } elseif ($manufacturers_name = "Sue Douglas") { ?>

<a href="index.php?main_page=index&manufacturers_id=4"> <img src="/images/sue_douglas.jpg"> </a>

<?php } ?>

If I'm totally in the wrong forum for this question, could anyone recommend a good (preferably free!) PHP forum that would be able to help me please?

Thanks!

13 May 2015, 6:32 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,091
Plugin Contributions:
56

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

suedouglas:

Not sure if this is the right forum for php questions, but I'm having trouble finding a PHP forum

I'm trying to display the manufacturers image on the product info page which when clicked goes to their url to see all that manufacturers products

My coding is in the following .php file:
/includes/templates/mytemplate/templates/tpl_product_info_display.php The problem is that only the first manufacturers image is shown on all products, even those that belong to the second manufacturer. Can anyone spot what I have wrong please? (I've resorted to hardcoding the urls and images which is why I need an if statement, as I couldn't get it to work at all when trying to get the manufacturers url and image from the database, and since there are only two of us ...!)

This is my problem coding:

<?php $manufacturers_name = zen_get_products_manufacturers_name($product_info->fields['products_id']); If ($manufacturers_name = "Sandie Burchell") { ?>

<a href="index.php?main_page=index&manufacturers_id=3"> <img src="/images/sandie_burchell.jpg"> </a>

<?php } elseif ($manufacturers_name = "Sue Douglas") { ?>

<a href="index.php?main_page=index&manufacturers_id=4"> <img src="/images/sue_douglas.jpg"> </a>

<?php } ?>

If I'm totally in the wrong forum for this question, could anyone recommend a good (preferably free!) PHP forum that would be able to help me please?

Thanks!
You've used assignment operators (single equal sign) instead of equality operations (double equal sign):

<?php

$manufacturers_name = zen_get_products_manufacturers_name($product_info->fields['products_id']);

If ($manufacturers_name [B]==[/B] "Sandie Burchell") { 
?>
<a href="index.php?main_page=index&manufacturers_id=3"> <img src="/images/sandie_burchell.jpg"> </a>	

<?php
} elseif ($manufacturers_name [B]==[/B] "Sue Douglas")  {
?>
<a href="index.php?main_page=index&manufacturers_id=4"> <img src="/images/sue_douglas.jpg"> </a>	
<?php
} 
?>
13 May 2015, 6:37 PM
#3
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

lat9:

You've used assignment operators (single equal sign) instead of equality operations (double equal sign):

The recommendation these days is to use a triple equals sign (===)

Cheers
RodG

13 May 2015, 6:41 PM
#4
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,091
Plugin Contributions:
56

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

RodG:

The recommendation these days is to use a triple equals sign (===)

Cheers
RodG
Agreed, on new code; simply changing all == (equal to) to === (exactly equal to) in "legacy" code can have unwanted consequences.:blink:

13 May 2015, 7:05 PM
#5
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

lat9:

Agreed, on new code; simply changing all == (equal to) to === (exactly equal to) in "legacy" code can have unwanted consequences.:blink:

Yes, I am aware that there is a subtle difference between == and ===

Just curious, it a known fact about 'unwanted consequences' , or is it the same disclaimer that I probably should have made on the basis that it probably hasn't been tested in every possible case?

Fact or not, I agree, it doesn't make sense take the risk of retroactively changing all legacy code due to the current recommendations.

I do like the idea of making PHP a little more strict though, and for new code, the === is less likely to have unwanted consequences than the ==, and generally speaking, if I AM working on legacy code and come across the odd == I tend to automatically change it to the === and test that nothing got busted as a result (so far, I haven't busted anything by doing this), hence my curiosity as to whether the 'unwanted consequences' are real, or just something that is theoretically possible.

"I don't know either" is a perfectly acceptable answer. :-)

Cheers
Rod.

13 May 2015, 7:30 PM
#6
suedouglas avatar

suedouglas

New Zenner

Join Date:
Nov 2008
Posts:
45
Plugin Contributions:
0

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

Thanks so much everyone who replied! The === sign worked fine! I've spent about 5 days on this one piece of coding so I'm delighted I now know what the problem is! I was under the misconception that the == meant not equal to, even though I had seen coding with the != as well which I know for sure is not equal to, because I do write SQL. This was most definitely my problem with trying to get the data from the database too!!
Thanks so much for your help, it's much appreciated!

13 May 2015, 7:51 PM
#7
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

RodG:

Yes, I am aware that there is a subtle difference between == and ===

Just curious, it a known fact about 'unwanted consequences' , or is it the same disclaimer that I probably should have made on the basis that it probably hasn't been tested in every possible case?

Fact or not, I agree, it doesn't make sense take the risk of retroactively changing all legacy code due to the current recommendations.

I do like the idea of making PHP a little more strict though, and for new code, the === is less likely to have unwanted consequences than the ==, and generally speaking, if I AM working on legacy code and come across the odd == I tend to automatically change it to the === and test that nothing got busted as a result (so far, I haven't busted anything by doing this), hence my curiosity as to whether the 'unwanted consequences' are real, or just something that is theoretically possible.

"I don't know either" is a perfectly acceptable answer. :-)

Cheers
Rod.

Outside of ZC I was programming some PHP to provide some information. During the initial coding I had used == for all such verification of equalities. I then chose to use === in all such cases by replacement of the two with the three. Problems followed. The most basic example of where such a problem is likely to occur is if the false statement is used to capture situations where the value is 0 or false... Certainly with a newer perspective one could evaluate using both criteria ored together, but under the "old" thought there was no need...

There may be other examples, but that is one that I have seen/experienced.

13 May 2015, 8:10 PM
#8
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,091
Plugin Contributions:
56

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

RodG:

Yes, I am aware that there is a subtle difference between == and ===

Just curious, it a known fact about 'unwanted consequences' , or is it the same disclaimer that I probably should have made on the basis that it probably hasn't been tested in every possible case?

Fact or not, I agree, it doesn't make sense take the risk of retroactively changing all legacy code due to the current recommendations.

I do like the idea of making PHP a little more strict though, and for new code, the === is less likely to have unwanted consequences than the ==, and generally speaking, if I AM working on legacy code and come across the odd == I tend to automatically change it to the === and test that nothing got busted as a result (so far, I haven't busted anything by doing this), hence my curiosity as to whether the 'unwanted consequences' are real, or just something that is theoretically possible.

"I don't know either" is a perfectly acceptable answer. :-)

Cheers
Rod.
As mc12345678 replied, there are places within Zen Cart (and especially the plugins!) where configuration values are checked as

if (CONFIGURATION_VALUE_CONSTANT == 0) {
}

which works just fine. The construct

if (CONFIGURATION_VALUE_CONSTANT === 0) {
}

will never evaluate to true, since configuration constants are strings, requiring the exactly-equal-to code to be formatted as

if (CONFIGURATION_VALUE_CONSTANT === '0') {
}
13 May 2015, 8:19 PM
#9
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

suedouglas:

Not sure if this is the right forum for php questions, but I'm having trouble finding a PHP forum

I'm trying to display the manufacturers image on the product info page which when clicked goes to their url to see all that manufacturers products

My coding is in the following .php file:
/includes/templates/mytemplate/templates/tpl_product_info_display.php The problem is that only the first manufacturers image is shown on all products, even those that belong to the second manufacturer. Can anyone spot what I have wrong please? (I've resorted to hardcoding the urls and images which is why I need an if statement, as I couldn't get it to work at all when trying to get the manufacturers url and image from the database, and since there are only two of us ...!)

This is my problem coding:

<?php $manufacturers_name = zen_get_products_manufacturers_name($product_info->fields['products_id']); If ($manufacturers_name = "Sandie Burchell") { ?>

<a href="index.php?main_page=index&manufacturers_id=3"> <img src="/images/sandie_burchell.jpg"> </a>

<?php } elseif ($manufacturers_name = "Sue Douglas") { ?>

<a href="index.php?main_page=index&manufacturers_id=4"> <img src="/images/sue_douglas.jpg"> </a>

<?php } ?>

If I'm totally in the wrong forum for this question, could anyone recommend a good (preferably free!) PHP forum that would be able to help me please?

Thanks!

suedouglas:

Thanks so much everyone who replied! The === sign worked fine! I've spent about 5 days on this one piece of coding so I'm delighted I now know what the problem is! I was under the misconception that the == meant not equal to, even though I had seen coding with the != as well which I know for sure is not equal to, because I do write SQL. This was most definitely my problem with trying to get the data from the database too!!
Thanks so much for your help, it's much appreciated!

As to a possibly more rounded approach to the solution now that it is more clearly represented than in a previous related thread:

<?php

$manufacturers_name = zen_get_products_manufacturers_name($product_info->fields['products_id']);

?>
<a href="<?php echo zen_href_link(FILENAME_DEFAULT, "manufacturers_id=" . zen_get_products_manufacturers_id($product_info->fields['products_id']), $request_type); ?>"><img src="<?php echo zen_get_manufacturers_image($product_info->fields['products_id']); ?>"></a>    

This is untested, so may have some error(s)...

13 May 2015, 8:35 PM
#10
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

In my above recoding I forgot that there is a function also to provide the image html through ZC as well... So the above falls short of a truly Zen response.

zen_image( $src, [ $alt = ''], [ $width = ''], [ $height = ''], [ $parameters = ''])
Where $src would be everything provided above at and after the ```
<img src=


> **mc12345678:**
>
> As to a possibly more rounded approach to the solution now that it is more clearly represented than in a previous related thread:
> 
> ```
<?php

$manufacturers_name = zen_get_products_manufacturers_name($product_info->fields['products_id']);

?>
<a href="<?php echo zen_href_link(FILENAME_DEFAULT, "manufacturers_id=" . zen_get_products_manufacturers_id($product_info->fields['products_id']), $request_type); ?>"><?php echo zen_image(zen_get_manufacturers_image($product_info->fields['products_id'])); ?></a>    

This is untested, so may have some error(s)...

13 May 2015, 9:20 PM
#11
lhungil avatar

lhungil

Totally Zenned

Join Date:
Feb 2012
Location:
mostly harmless
Posts:
1,818
Plugin Contributions:
4

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

suedouglas:

Not sure if this is the right forum for php questions, but I'm having trouble finding a PHP forum

I'm trying to display the manufacturers image on the product info page which when clicked goes to their url to see all that manufacturers products ...
*** deleted ***
Almost identical to the second snippet posted by mc12345678. Only difference was the use of an observer to retrieve the manufacturer info (in a single SQL query) instead of retrieving directly in the template.
*** deleted ***

RodG:

Yes, I am aware that there is a subtle difference between == and ===

Just curious, it a known fact about 'unwanted consequences' , or is it the same disclaimer that I probably should have made on the basis that it probably hasn't been tested in every possible case? ...
As far as I know, unwanted consequences are likely when the original author of the code WANTED the behavior of ==, not ===. When upgrading old code, the author of the old code may have knowingly relied upon: false == 0 == "0" == null == "" (so just changing to === could have negative consequences). Or the old code may be comparing objects (not primitives) and thus intentionally using == instead of ===.

For primitive types in PHP (such as int, float, bool, etc) I'm usually a fan of using === (aka the identity operator or identical) in favor of == (aka the comparison operator or equals). IMHO this makes it less likely for a coder skimming over code to misinterpret the intent (and helps the coder avoid mistakes if they forget how == works in PHP).

When it comes to objects in PHP, I disagree with using == (aka the comparison operator or equals) altogether in favor of the class defining a comparison method (more control over what constitutes equality - such as the use of === for checking primitive properties and checking the actual classname). For objects, === (aka the identity operator or identical) is not often used as it only evaluates true when the two objects are the same instance of the same class.

13 May 2015, 9:42 PM
#12
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

lhungil:

*** deleted ***
Almost identical to the second snippet posted by mc12345678. Only difference was the use of an observer to retrieve the manufacturer info (in a single SQL query) instead of retrieving directly in the template.
*** deleted ***

So curious, in the concept of using the observer, that would still require modification to the template file correct? (Ie. One issue with the modification proposed is that in subsequent upgrades or in new templates, this change must be carried over to remain a part of the displayed informatiion.) I haven't played/tested this aspect but do the variables assigned in the observer become a part ofthe global space or do they fall out of scope? Would declaring the variable in the observer as global maintain it in scope for "global" use?

I understand that the above basically would have provided all of the requested data (manufacturers_id as well as manufacturers_image), but was trying to understand how to keep it in use for the code space that had the notifier.

13 May 2015, 10:06 PM
#13
lhungil avatar

lhungil

Totally Zenned

Join Date:
Feb 2012
Location:
mostly harmless
Posts:
1,818
Plugin Contributions:
4

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

Only three reasons I was initially thinking observer on this one. First it removes data querying and business logic from the presentation layer (strong reason in my little world). Second only one SQL query is needed versus one per zen_get_products_manufacturers_* function call (weaker reason in my little world as most SQL queries are very fast). Third, it provides yet another example of how to use an observer.

mc12345678:

So curious, in the concept of using the observer, that would still require modification to the template file correct? ...
Yes, the exact same ones as you already provided while I was typing (just using a variable in place of the zen_get_products_manufacturers_* function calls).

mc12345678:

... Would declaring the variable in the observer as global maintain it in scope for "global" use? ...
Yes.

Declaring the variable as in the global scope (and making it available) inside a method (class function) works.

global $my_special_variable;

Assigning and accessing the variable using $GLOBAL in the observer also works.

$GLOBALS['my_special_variable']
15 May 2015, 4:18 PM
#14
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: PHP question on /includes/templates/mytemplate/templates/tpl_product_info_display.php

lhungil:

Only three reasons I was initially thinking observer on this one. First it removes data querying and business logic from the presentation layer (strong reason in my little world). Second only one SQL query is needed versus one per zen_get_products_manufacturers_* function call (weaker reason in my little world as most SQL queries are very fast). Third, it provides yet another example of how to use an observer.

Yes, the exact same ones as you already provided while I was typing (just using a variable in place of the zen_get_products_manufacturers_* function calls).

Yes.

Declaring the variable as in the global scope (and making it available) inside a method (class function) works.

global $my_special_variable;

> 
> Assigning and accessing the variable using $GLOBAL in the observer also works.
> ```
$GLOBALS['my_special_variable']

Come to think about it, I may have misspoke about not having considered passing a variable back to the main program flow by declaring it global within the observer. I think it was an "issue" of passing a variable back to a function without modifying the function where the notifier (in ZC 1.5.4+) did not include the variable as a pointer and the variable is not associated with the class...

Sort of on a related/non-related subject and probably should start a new thread in the ZC suggestion area, but a proposed solution to this issue made me think about it... The use of the "procedure" names in the observer files is one that doesn't lend itself well to consistent file search. To explain, the observer functions when listening to/for a notifier are either:
function update
with an if statement to identify the notifier inside of the update function
or
function updateNotifierName (without need for capital letters and only available beginning in ZC 1.5.4)

The notifier is of the type 'NOTIFIER_NAME' but when troubleshooting code, if a value changed from just before a notifier to just after the notifier, one could not "readily" find the code that caused the change just by using NOTIFIER_NAME in the developers toolkit if the function type naming added in ZC 1.5.4 were used. Personally I add the notifier name in a comment before the listing, but that is because of what I describe above. Suggestion is that this (I think highly helpful feature) use/allow a naming convention to include the underlines at least in the NOTIFIER_NAME variable and am non-committal to the need of it between update and NOTIFIER_NAME.