ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Execute PHP code in Template

November 20, 2012 2:13am

Subscribe [1]
  • #1 / Nov 20, 2012 2:13am

    somvannda

    37 posts

    Hi everyone,

    I have to execute below php in the template which to have a random images on the header. but when I used Ctrl + U to see the source code the php code is not execute and it doesnt say error, but it is display the same thing in my source code. I was wandering how can we execute such php code in the template? I tried to look for helps on internet, but it seemed lack of resources.

    <?php
    
     $num = Rand (0,6) ; 
    switch ($num) { 
        case 0: 
        $header = 'header_accountability.png';
        break; 
        case 1: 
        $header = 'header_courage.png';
        break; 
        case 2: 
        $header = 'header_democracy.png';
        break; 
        case 3: 
        $header = 'header_integrity.png';
        break; 
        case 4: 
        $header = 'header_justice.png';
        break; 
        case 5: 
        $header = 'header_solidarity.png';
        break; 
        case 6: 
        $header = 'header_transparency.png';
        break; 
    } 
    ?>
    
                    <div id="Header">

    Thanks
    S

  • #2 / Nov 20, 2012 9:24am

    glenndavisgroup

    436 posts

    Hi somvannda,

    This is probably because you haven’t set to allow PHP on your template. To do this, login to your CP and go to “Design > Templates > Template Manager”. Once the template management screen loads click on the template group where your template is stored under. Once the right side of screen loads with the templates click the “Edit Preferences” link to the right of the template you have this PHP code in. It will display a few options for you. Under the “Allow PHP?” option make sure “Yes” is selected. Once you set it to Yes then go and check your page again. This should fix it for you. I hope that helps.

    Mike

  • #3 / Nov 21, 2012 5:36pm

    Dan Decker

    7338 posts

    Hi somvannda,

    I think Mike is on the right track here. Have you enabled PHP in the template’s preferences?

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases