Jump to content

Using smarty code inside PHP?


zomex

Recommended Posts

Hello everyone,

 

I probably won't word this very well as I don't know anything about PHP but for a text rotator script I'm using I'd like to display the users first name, the only issue is that it needs to be inside an the PHP code and isn't recognized.

 

Here's what I've got so far:

 

{if $loggedin}

{php}

$footer = rand(0,1);

$text[0] = '{$loggedinuser.firstname} example 1';

$text[1] = '{$loggedinuser.firstname} example 2';
{/php}

{/if}

<p>{php} echo($text[$footer]); {/php}</p><!-- display 1 selection at random -->

 

The script itself works perfectly but the smarty variable is displayed as plain text. Is there a way to make this work or is that just how smarty works?

 

Thanks very much for any assistance!

 

Jack

Link to comment
Share on other sites

php does not know what the smarty variables are there how you have it so it will just display the smarty tag itself... try it this way but it is very messy.

{if $loggedin}
{php}
$footer = rand(0,1);
$text[0] = '{/php}{$loggedinuser.firstname}{php} example 1';
$text[1] = '{/php}{$loggedinuser.firstname}{php} example 2';
{/php}
{/if}
<p>{php} echo($text[$footer]); {/php}</p><!-- display 1 selection at random -->  

You would be better to do it something like this

{if $loggedin}
{php}
$footer = rand(0,1);
$text[0] = $this->_tpl_vars["loggedinuser"]["firstname"].' example 1';
$text[1] = $this->_tpl_vars["loggedinuser"]["firstname"].' example 2';
{/php}
{/if}
<p>{php} echo($text[$footer]); {/php}</p><!-- display 1 selection at random -->  

Link to comment
Share on other sites

php does not know what the smarty variables are there how you have it so it will just display the smarty tag itself... try it this way but it is very messy.

{if $loggedin}
{php}
$footer = rand(0,1);
$text[0] = '{/php}{$loggedinuser.firstname}{php} example 1';
$text[1] = '{/php}{$loggedinuser.firstname}{php} example 2';
{/php}
{/if}
<p>{php} echo($text[$footer]); {/php}</p><!-- display 1 selection at random -->  

You would be better to do it something like this

{if $loggedin}
{php}
$footer = rand(0,1);
$text[0] = $this->_tpl_vars["loggedinuser"]["firstname"].' example 1';
$text[1] = $this->_tpl_vars["loggedinuser"]["firstname"].' example 2';
{/php}
{/if}
<p>{php} echo($text[$footer]); {/php}</p><!-- display 1 selection at random -->  

 

Thanks very much Sparky! I appreciate you taking the time out of your day to help me. I've been trying to figure this out all day.

 

The second option works perfectly but the first causes the white screen (I've caused many white screens today 8)).

 

The only thing left now is to work out how to display the name in a different order then it's all set. If you're busy it's not a problem as I'll play around with the code to see if it's possible but if you have some more time to assist me with this I'd be happy to make a donation via PayPal :)

 

Jack

Link to comment
Share on other sites

Forget about donations... that is not allowed here.

What do you mean display it in a different order?

you are generating a random number that will either be 0 or 1 and then just displaying 1 of the array values

 

Sure. I mean the order of the text vs name itself. For example:

 

'Welcome back {name}'

 

'{name} we are currently running a promotion .. ...'

 

Thanks.

Link to comment
Share on other sites

Your going to have to be alot clearer than that.

{name} won't display anything anyway!!

you need to use {php} echo($text[$footer]); {/php} or in the php just assign the random text to a smarty variable then you can use {$name}

{if $loggedin}

{php}

$footer = rand(0,1);

$text[0] = $this->_tpl_vars["loggedinuser"]["firstname"].' example 1';

$text[1] = $this->_tpl_vars["loggedinuser"]["firstname"].' example 2';

$this->assign('name',$text[$footer]);

{/php}

{/if}

<p>{$name}</p><!-- display 1 selection at random -->

Link to comment
Share on other sites

Your going to have to be alot clearer than that.

{name} won't display anything anyway!!

you need to use {php} echo($text[$footer]); {/php} or in the php just assign the random text to a smarty variable then you can use {$name}

 

Sorry Sparky I should have been more clear. So this is what I'm doing with the script:

 

Logged out example: http://www.zomex.com/images/layout/loggedout.png

Logged in example: http://www.zomex.com/images/layout/loggedin.png

 

When the user is logged out it displays a generic announcement which as you know is randomly selected from the options. Now when the user is logged in it will use a different set of announcements including the users first name for a more personal level.

 

The code you provided works perfectly if each announcement/message had the users name at the start. For example would it be possible to display the name of the visitor in-between an announcement rather than at the start such as:

 

'welcome back {name_of_user}. We have... ....'

 

Here is what all of the code looks like:

 

{if $loggedin}
{php}
$footer = rand(0,1);
$text[0] = $this->_tpl_vars["loggedinuser"]["firstname"].' example 1';
$text[1] = $this->_tpl_vars["loggedinuser"]["firstname"].' example 2';
{/php}

{else}

{php}

$footer = rand(0,2);

     $text[0] = 'Did you know that we have over 20 cPanel video tutorials? > <a href="/tutorials/cpanel.php">cPanel video tutorials</a>';
     $text[1] = '3 popular SEO practises that are unlikely to help your website in the search engines > <a href="/blog/seo/search-engine-optimization-a-different-perspective.php">Misleading SEO techniques</a>';
     $text[2] = 'Did you know that we have a growing knowledge base full of useful tips and answers to popular questions? > <a href="/clients/knowledgebase.php">Zomex Knowledge Base</a>';

{/php}

{/if}

<div class="bubble">
<p>{php} echo($text[$footer]); {/php}</p>
</div>

 

Everything works perfectly and all that's left is the ability to choose where the visitor's first name appears in the first set of announcements.

 

Thanks!

Edited by zomex
Link to comment
Share on other sites

Understand now... you mean like this

 

{if $loggedin} 
{php} 
    $footer = rand(0,1); 
    $text[0] = 'Welcome back ' . $this->_tpl_vars["loggedinuser"]["firstname"] . ' We have... ....'; 
    $text[1] = $this->_tpl_vars["loggedinuser"]["firstname"] . ' we are currently running a promotion .. ...';
{/php} 

{else} 

{php} 
     $footer = rand(0,2); 
     $text[0] = 'Did you know that we have over 20 cPanel video tutorials? > <a href="/tutorials/cpanel.php">cPanel video tutorials</a>'; 
     $text[1] = '3 popular SEO practises that are unlikely to help your website in the search engines > <a href="/blog/seo/search-engine-optimization-a-different-perspective.php">Misleading SEO techniques</a>'; 
     $text[2] = 'Did you know that we have a growing knowledge base full of useful tips and answers to popular questions? > <a href="/clients/knowledgebase.php">Zomex Knowledge Base</a>'; 
{/php} 

{/if} 

<div class="bubble"> 
<p>{php} echo($text[$footer]); {/php}</p> 
</div>

Link to comment
Share on other sites

Understand now... you mean like this

 

{if $loggedin} 
{php} 
    $footer = rand(0,1); 
    $text[0] = 'Welcome back ' . $this->_tpl_vars["loggedinuser"]["firstname"] . ' We have... ....'; 
    $text[1] = $this->_tpl_vars["loggedinuser"]["firstname"] . ' we are currently running a promotion .. ...';
{/php} 

{else} 

{php} 
     $footer = rand(0,2); 
     $text[0] = 'Did you know that we have over 20 cPanel video tutorials? > <a href="/tutorials/cpanel.php">cPanel video tutorials</a>'; 
     $text[1] = '3 popular SEO practises that are unlikely to help your website in the search engines > <a href="/blog/seo/search-engine-optimization-a-different-perspective.php">Misleading SEO techniques</a>'; 
     $text[2] = 'Did you know that we have a growing knowledge base full of useful tips and answers to popular questions? > <a href="/clients/knowledgebase.php">Zomex Knowledge Base</a>'; 
{/php} 

{/if} 

<div class="bubble"> 
<p>{php} echo($text[$footer]); {/php}</p> 
</div>

 

Thanks very much Sparky, that worked perfectly!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated