phpLD - How to write a php random number script in a .tpl file

Discussion in 'PHP' started by mangobyte, Oct 9, 2007.

  1. #1
    Hello everybody,

    I am trying to generate a random number using php inside a .tpl file (template file),

    <? $x = rand(0,3);

    if($x==0){
    EXEC THIS CODE.......
    }
    else if($x==1){
    EXEC THIS CODE.......
    }
    else {
    EXEC THIS CODE.......
    }

    ?>

    But it seems it doesn't take php inside a tpl. So, how to achieve this. Please anyone help me in this regard.

    Thanks!

    Regards,
    Mangobyte
     
    mangobyte, Oct 9, 2007 IP
  2. zenglider

    zenglider Peon

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    First, please press the submit button once. You have three copies of this post right after the other. Delete the dupes if you can.

    .tpl is generally associated with Smarty. Are you using that? If you are then you need to do it this way.

    
    {if $random == 1}
    ...
    {elseif $random == 3}
    ...
    {else}
    ...
    {/if}
    
    PHP:
    in the PHP file
    
    $random = rand(0,1);
    $smarty->assign('random', $random);
    $smarty->display('template_name.tpl');
    
    PHP:
    if you need actual PHP code to run then I suggest you take that out of the template and run it in the script and put the end result in to the template.
     
    zenglider, Oct 9, 2007 IP
  3. mangobyte

    mangobyte Well-Known Member

    Messages:
    435
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Ok. Thanks for the guidance.

    As far as I have understood that first code part will have to be written in a .tpl file.. But could you please let me know where to put this second php code. I mean in which .php file..?


    Thanks again!

    Regards,
    Mangobyte

    P.S.: I tried to delete those dupl. posts but it doesn't seem to have any option to delete those posts. So, I had edited one of the post last time like this .. http://forums.digitalpoint.com/showthread.php?t=508044
     
    mangobyte, Oct 9, 2007 IP