1
Arowana
Smarty template with php in it
  • 2007/12/28 6:11

  • Arowana

  • Friend of XOOPS

  • Posts: 323

  • Since: 2004/8/6 2


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

2
nachenko
Re: Smarty template with php in it
  • 2007/12/28 10:20

  • nachenko

  • Quite a regular

  • Posts: 356

  • Since: 2005/1/18


I think your problem is the "else" statement.

It should be

} else {

3
mobydick
Re: Smarty template with php in it
  • 2007/12/28 16:43

  • mobydick

  • Just popping in

  • Posts: 13

  • Since: 2005/3/13


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";
}

4
Catzwolf
Re: Smarty template with php in it
  • 2007/12/28 17:42

  • Catzwolf

  • Home away from home

  • Posts: 1392

  • Since: 2007/9/30


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;

Login

Who's Online

372 user(s) are online (309 user(s) are browsing Support Forums)


Members: 0


Guests: 372


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits