MMv4 KB
 
Can QuickCode Tags Be Used in PHP Statements?
  Last Edited - 12/23/2011 3:17pm PST
  Category Path - Shopping Cart Software Components > Administration Area > Design > FAQs & Tutorials
 
question
I'm using a QuickCode Tag to try and reference a category's ID number in a PHP script. However, it's not working. The PHP code looks something like this:
<?php
if ({CATEGORY-SID} == 123) {
   echo ('You are viewing category 123.');
}
?>

What am I doing wrong? Can QuickCode Tags be used in PHP code this way?

The short answer is, no, QuickCode Tags cannot be used within PHP code in this way.

Why not?
Well, let's use the {CATEGORY-SID} QuickCode Tag as an example. This, and all other, QuickCode Tags are designed to be replaced by their source code prior to the web page that contains them being rendered in a web browser. That way, the QuickCode Tag provides a simple way to add blocks of complex HTML, JavaScript or PHP code without the need to fiddle with that code directly.

So, what is the source code that the {CATEGORY-SID} QuickCode Tag is replaced with? Is it the category's SID (System ID) number of "123"?

Nope. The {CATEGORY-SID} QuickCode Tag is actually replaced with the PHP code that it contains. In this case, any instances of this QuickCode Tag in the store's template are replaced with PHP code similar to:

{CATEGORY-SID} quickcode tag source code
<?php echo($category['id']); ?>

That means that example code from the original question would be translated by the shopping cart software into something like this:

example
<?php
if (
<?php echo($category['id']); ?> == 123) {
   echo ('You are viewing category 123.');
}
?>

See the problem there? Since the QuickCode Tag is replaced by its PHP source code, it results in a PHP tag nested within the other PHP tag, which will cause the PHP script to fail.

Fortunately, the solution is pretty simple.

The online QuickCode Tag glossary contains every QuickCode Tag available and the source code for each one. To fix the example script above, first look up the {CATEGORY-SID} QuickCode Tag in the glossary, and then use the desired content from the tag's source code in the PHP scripts.

We could then use the proper variables that we obtained from the "source code" section of the QuickCode Tag's glossary entry to create a correct version of the PHP script, which would look something like this:

example
<?php
if (
$category['id'] == 123) {
   echo ('You are viewing category 123.');
}
?>
Powered by ModularKB