Declaring variables that contain PHP code..

Im trying to declare a variable,

I can’t get the syntax right and am not sure what it is even meant to be. This is for a magento installation… but it’s just a general PHP problem.

What I want this to do, is when an item page loads, it checks to see if there is a file matching the SKU exactly + ‘.pdf’, as it’s a PDF file, and if there is one, to attach it to the product page… and if there isn’t, display an email address.

Here is my code:

<?php $filename = 'echo $this->htmlEscape($_product->getSku()).pdf; if (file_exists($filename)) { ?>

To download the PDF SPEC SHEET, please click here

<?PHP } else { ?>

No Spec Sheet exists for this item. Please email us to request one.

<?PHP } ?>

It’s mainly the “$filename = 'echo $this->htmlEscape($_product->getSku()).pdf;” part I think… I’ve had it breaking and ‘loading’ front-end but it never works properly.

This code: ‘click here’ works perfectly.

I can make a permanent link… my only trouble is defining $filename and possibly the if statement… but I do think it;s probably just defining the $filename.

[php]<?php
$filename = 'echo $this->htmlEscape($_product->getSku()).pdf;

if (file_exists($filename)) { ?>

To download the PDF SPEC SHEET, please click here

<?PHP } else { ?>

No Spec Sheet exists for this item. Please email us to request one.

<?PHP } ?>[/php]

you only echo when you want it to be output to the browser, or when the function RETURNS the value instead of printing it. when you are assigning variables, just make it equals. you only wrap text in quotes, aboce you start it before the echo, which is wrong, and you don’t end it before the ', which is wrong. also, look at the syntax below to see how to combine text and returned string.
[php]<?php
$filename = $this->htmlEscape($_product->getSku()). ‘.pdf’;
?>[/php]

good luck

Sponsor our Newsletter | Privacy Policy | Terms of Service