xoops forums

Arowana

Friend of XOOPS
Posted on: 2007/12/28 6:11
Arowana
Arowana (Show more)
Friend of XOOPS
Posts: 323
Since: 2004/8/6 2
#1

Smarty template with php in it

I am trying to get this to work the first one WORKS but the second one does not. As soon as I put an else or {else} in it then i get the smarty template error.

Can anyone help

Working Code:
{php}
    global 
$vars$db;

if(
$vars['product_id']=='') {
        
$Select $db->query("select * from {$db->config[prefix]}dbtable where product_id=''"); 
        
$num1=mysql_num_rows($Select);
        if(
$num1<5)
        {
        echo 
"Sold Out";
        }
    }    

{/
php}


Not working Code:
{php}
    global 
$vars$db;

if(
$vars['product_id']=='') {
        
$Select $db->query("select * from {$db->config[prefix]}dbtable where product_id=''"); 
        
$num1=mysql_num_rows($Select);
        if(
$num1<5)
        {
        echo 
"Sold Out";
        else
        echo 
"Buy Now";
        }
    }    

{/
php}
Arowana

nachenko

Quite a regular
Posted on: 2007/12/28 10:20
nachenko
nachenko (Show more)
Quite a regular
Posts: 356
Since: 2005/1/18
#2

Re: Smarty template with php in it

I think your problem is the "else" statement.

It should be

} else {

mobydick

Just popping in
Posted on: 2007/12/28 16:43
mobydick
mobydick (Show more)
Just popping in
Posts: 13
Since: 2005/3/13
#3

Re: Smarty template with php in it

As far as I know proper "if...else" syntax is:

if (expression) {
sentences
}
else {
sentences
}


Therefore I think that your code should read like this:

if($num1<5{
echo "Sold Out";
}
else{
echo "Buy Now";
}

Catzwolf

Home away from home
Posted on: 2007/12/28 17:42
Catzwolf
Catzwolf (Show more)
Home away from home
Posts: 1392
Since: 2007/9/30
#4

Re: Smarty template with php in it

On the whole point of this syntax and proper use of smarty templates.

You should always separate business logic from presentation at all times. Therefore there should not be any calls to the database within a template. Try and move that somewhere and not in the template.

Regarding the syntax, there are many different ways to do the if else statement, if you were to stay true to the pear standard then your example would be correct. However, I prefer a more C style syntax:

$ret = ( $num1<5 ) ? 'Sold Out' : 'Buy Now';
echo $ret;