Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How do I use PHP in CMS pages
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

Hi Sicarius, I actually tried it both ways but that doesn’t make a difference. If I close it as you have suggested I get this error: “Notice: Undefined variable: html”

I’m wondering if I’m actually using this technique completely wrong…

Should what Moshe has given as the ‘test.php’ just be the php script you actually want to use on your site? Which then gets called/referenced in the CMS page?

Therefore, if I make a PHP file called rotate.php which is just the rotate PHP script from alistapart and I save it here app\code\local\Mayerwin\Custom\block\rotator.php - should I just be using this command in the CMS page to make it work?

{{block type="mayerwin_custom/rotator"}}

?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sicarius
Jr. Member
 
Total Posts:  9
Joined:  2008-09-20
 

The right way to close the PHP tag is the one I showed you. That notice “Undefined variable html” happens because nothing is actually being set on the $html variable.

You can’t just include the rotator.php directly, you’ll have to adapt it. Remember, you need to set up a class, extending the Mage_Core_Block_Abstract, it must have an overloaded function called _toHtml(), and that function must return a $html variable - inside it is the actual content you’ll display on your CMS.

The problem is that this rotator code actually prints the information directly - it’s not stored in a single variable. I haven’t studied Magento code properly yet, so I can’t tell you exactly what would happen with this output.

Think about this:

<?php

class Ben_Custom_Block_NewRotator extends Mage_Core_Block_Abstract {
protected function _toHtml() {

// All rotator code comes here

$html $rotatorResult;
// Assuming rotator code is changed, and now all it's output
// is stored in rotatorResult instead of being printed directly

return $html;

}
?>

Tip: take a look in every printf and sprintf function insite rotator.php.

 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

Sicarius, thanks for the detailed response. I’m afraid even after reading and re-reading that a number of times I’m still struggling to get my head around it (I’m a humble designer!) but I will endeavor to figure it out.

It is frustrating to get PHP working in the home page as I have found it so easy to add PHP into the sidebars etc as they use phtml files as their basis. This time next year I may have it cracked wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sicarius
Jr. Member
 
Total Posts:  9
Joined:  2008-09-20
 

I’ ve been there too… wink

Look, try this: fix the closing tag ("?>"), and insert this anywhere in your rotator code, inside the toHtml() function:

$html "Hello";

You can insert it near the end, like this:

$html "Hello";

return 
$html;

Worst case scenario, it’ll print just the “hello”. Best case, it’ll print everything you want.

 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

Hi Sicarius, it ended up the worst case. :(

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sicarius
Jr. Member
 
Total Posts:  9
Joined:  2008-09-20
 

But that’s good news too.

What you have to do now is change all your printf and sprintf code lines to store their result inside $html.

It’s more about PHP than Magento itself, but I’ll point you to the “right” direction (more of a quick’n’ugly hack actually) smile

Consider this:

printf(
        
'<a href="%s" title="%s">',
        
$images[$img]['url'],
        
$images[$img]['title']
      
);

Change it this way:

// Here is the big change... it's not printf anymore, it's sprintf,
// and I'm storing it's result in $html
$html sprintf
        
'<a href="%s" title="%s">',
        
$images[$img]['url'],
        
$images[$img]['title']
      
);

Give it a try and lemme know if it worked wink

If you want to know more about printf and sprintf (and why I switched from one to another), check php.net wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

Hi Sicarius, I’ll take a shot at this tomorrow and update you. Thanks for taking the time to help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

Hi Sicarius, well I went through the rotator PHP script and changes the instances of printf( to $html = sprintf(

The full php that the module is now referencing is thus (I have adjusted the ini code url in this post)…

<?php

class Mayerwin_Custom_Block_Test extends Mage_Core_Block_Abstract
{
  
protected function _toHtml()
  
{
     
// put here your custom PHP code with output in $html;
     // use arguments like $this->getMyParam1() , $this->getAnotherParam()
      

/*

  Author: Dan Benjamin - http://hivelogic.com/

  Copyright (c) 2004 Automatic, Ltd. All Rights Reserved.

  THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF 
  ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY 
  IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR 
  PURPOSE OR NONINFRINGEMENT.  

  IN NO EVENT SHALL DAN BENJAMIN, A LIST APART, OR AUTOMATIC, LTD. BE LIABLE
  FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR
  LOST PROFITS ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER
  ARISING, INCLUDING NEGLIGENCE), EVEN IF DAN BENJAMIN, A LIST APART, OR
  AUTOMATIC, LTD. IS AWARE OF THE POSSIBILITY OF SUCH DAMAGES.

*/



  # file containg your image descriptions

  
$IMG_CONFIG_FILE 'http://www.mydomain/images/media/images.ini';



  
# You shouldn't need to change anything below this point

  
function showImage$ini=null {
    
global $IMG_CONFIG_FILE;
    
# if no custom ini file has been specified, use the default
    
$ini_file $ini $ini $IMG_CONFIG_FILE;
    
# read the config file into an array or die trying
    
$images = @parse_ini_file($ini_file,true);
    if (! 
$images{
      
die('Unable to read ini file.');
    
}
    
# pick a random image from the parsed config file
    
$img array_rand($images);
    
# get the selected image's css id if one exists
    
$id $images[$img]['id'?
      
sprintf' id="%s" '$images[$img]['id') :
      
'';
    
# get the selected image's css class if one exists
    
$class $images[$img]['class'?
      
sprintf' class="%s" '$images[$img]['class') :
      
'';
    
# get selected image's dimensions
    
$size = @getimagesize$images[$img]['src');
    
# if an url was specified, output the opening A HREF tag
    
if ( $images[$img]['url'{
      $html 
sprintf(
        
'<a href="%s" title="%s">',
        
$images[$img]['url'],
        
$images[$img]['title']
      
);
    
}
    
# output the IMG tag
    
$html sprintf(
      
'<img src="%s" alt="%s" %s %s%s/>',
      
$images[$img]['src'],
      
$images[$img]['alt'],
      
$size[3],
      
$id,
      
$class
    
);
    
# if an url was specified, output the closing A HREF tag
    
if ( $images[$img]['url'{
      
echo('</a>');
    
}
  }
  $html 
"Hello";
return 
$html;
  
}
}
?>

The page still only shows the ‘Hello’ message from the bottom. Was I supposed to change the existing sprintf commands to something else?

Thanks in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sicarius
Jr. Member
 
Total Posts:  9
Joined:  2008-09-20
 

Remove this line:

$html "Hello";

You’re overwriting $html’s content there wink

And one more thing… you have two lines like this:

$html sprintf (something here);

If you don’t want the second one to overwrite the first, change the second one to this:

$html .= sprintf (nothing changes here);

It’s a “dot equals” between $html and sprintf. dot equals concatenates the content instead of overwriting it.

I’m pretty sure this will do the trick.

Best regards

 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

Sicarius, thanks for your continued help with this. I have made the amends you suggested but I now get this error on the front page:

Notice: Undefined variable: html in my domain/magento/app/code/local/Mayerwin/Custom/Block/Test.php on line 84
Trace:

etc

Line 84 is

return $html;

I think this is something simple, I can almost smell success around the corner. What is the last piece of the puzzle?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sicarius
Jr. Member
 
Total Posts:  9
Joined:  2008-09-20
 

I believe this error is caused because you’re defining $html inside the function showImage(), and returning it outside this function.

Try this… change this piece of code:

# if an url was specified, output the closing A HREF tag
    if ( $images[$img]['url'{
      
echo('</a>');
    
}
  }
  
return $html;
}

Into this:

# if an url was specified, output the closing A HREF tag
    if ( $images[$img]['url'{
      
echo('</a>');
    
}
   
return $html// Moved this line inside the showImage() function
 
}

return showImagenull ); // Added this line
}

BTW, you have to translate that “echo” line too, like this:

if ( $images[$img]['url'{
      $html 
.= '</a>';
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

Sicarius, you’re good smile

OK, made those amends, I now get the front page but the CMS section is blank apart from a message ‘Unable to read ini file.’ which obviously means that Magento is now ‘understanding’ the script you have specified.

However, I know the ini file is OK and in the correct location, so is there something else regarding the PHP that would make it read the images.ini file?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sicarius
Jr. Member
 
Total Posts:  9
Joined:  2008-09-20
 

Check the ini file permissions… set it to 777 in your ftp client, and double check the URL. wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

Sicarius, file is 777 (tried 755 too) and url definately correct (I pasted it into Firefox and it opened right up). Any other thoughts?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sicarius
Jr. Member
 
Total Posts:  9
Joined:  2008-09-20
 

For some reason, your ini file can’t be parsed. A problem in your ini file could cause this (maybe a syntax problem).

Double check you ini’s syntax, and/or create an additional (and smaller) ini file for testing purposes. Further information can be found here:

http://www.php.net/parse_ini_file

wink

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top