2

Im a "noob" in smarty. I need to execute following code in one of my .tpl files:

  <? // SELECT sql query
$sql = "SELECT 'id' , 'title' FROM `forum_posts` WHERE bid = '1' ORDER BY 'date' DESC LIMIT 4"; 

// perform the query and store the result
$result = query($sql);

// if the $result contains at least one row
if ($result->num_rows > 0) {
  // output data of each row from $result
  while($row = $result->fetch_assoc()) {
    echo '<tr>
        <td><a href="http://www.site.com/forum.php?topic='. $row['id']. '">'. $row['title']. '</a>  </td>  
        </tr>   ';
  }
}
else {
  echo 'No news';
}
?>

I've been trying for 3 hours now, surfing all over the web but without success. Help please!

3
  • 3
    Why are your templates executing queries? Commented May 30, 2013 at 15:50
  • 3
    You are approaching this incorrectly. Don't execute the query in your template file. Execute it in the PHP file which calls the template, then $smarty->assign() the rows fetched to an array which gets used in the template. Commented May 30, 2013 at 15:51
  • I have created a file news.php and included it into a tpl file with: {include file='newsacc.php'} but I guess it's wrong. I have no "smarty" idea whatsoever. I have this script which uses smarty and just want to add this widget. Any good examples about what you are saying? Thanks Commented May 30, 2013 at 17:48

2 Answers 2

1

You are using quotes instead of backtick for column name, simply change them to avoid the error

SELECT `id` , `title` FROM `forum_posts` WHERE `bid` = '1' ORDER BY `date` DESC LIMIT 4"; 
Sign up to request clarification or add additional context in comments.

Comments

0

require('../libs/SmartySQL.class.php');

$smarty = new SmartySQL( array('pdo_dsn' => 'mysql:dbname=db_name;host=localhost', 'pdo_username' => 'username', 'pdo_password' => 'password', 'pdo_driver_options' => array() ) );

$smarty->display('index.tpl');

1 Comment

You should not answer just by a piece of code. A bit of explanation are welcomed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.