简体   繁体   中英

Smarty not displaying results in template file

I created file of code working perfect ... but problem is I added the file into my website header {include file="header_featured.html"} It is not displaying any thing ... But if write the url in browser with its .php file name like www.domain.com/header_featured.php it shows results what is the problem ...? even all of the other files of cms/script also included in header and displaying perfect results ..

header_featured.php

<?php

require_once "include/include.php";

global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);


$ads = array();

$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50"); 
while ($temp = mysql_fetch_assoc($featured_ads)) { 
    $record = array();
    $record['ad_id']      = $temp['id'];
    $record['ad_title']   = $temp['title'];

    //check for image if featured ad have image (Need to fetch only single image)
    $featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no"); 
    $img = mysql_fetch_assoc($featured_ads_images);
    $record['img_id']         = $img['id'];
    $record['ad_img']         = $img['picture'];
    $record['img_folder']     = $img['folder'];

    $ads[] = $record;
} 

$smarty->assign('ads', $ads);

$db->close();
if($db->error!='') { $db_error = $db->getError(); $smarty->assign('db_error',$db_error); }

$smarty->display('header_featured.html');
close();
?>

header_featured.html

 {foreach item=item  from=$ads} 
  {$item.ad_id}
  {$item.ad_title}
  {$item.img_id}
  {$item.ad_img}
  {$item.img_folder}
 {/foreach}

My way of including the file into header

{include file="header_featured.html"}

It seems you should simple in your PHP code write:

<?php include ('header_featured.php'); ?>

to display your content.

However in fact you should move the smarty part to index.php, include header_featured.php file and in index.php display Smarty template.

For example I would do it it this way:

index.php

<?php

require_once "include/include.php";

global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);

$header_featured = '';

require('header_featured.php');

$db->close();
if($db->error!='') { 
    $db_error = $db->getError(); 
    $smarty->assign('db_error',$db_error); 
}
else {
   $header_featured = $smarty->fetch('header_featured.tpl');
}

$smarty->assign('header_featured', $header_featured );

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


?>

header_featured.php

<?php 
$ads = array();

$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50"); 
while ($temp = mysql_fetch_assoc($featured_ads)) { 
    $record = array();
    $record['ad_id']      = $temp['id'];
    $record['ad_title']   = $temp['title'];

    //check for image if featured ad have image (Need to fetch only single image)
    $featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no"); 
    $img = mysql_fetch_assoc($featured_ads_images);
    $record['img_id']         = $img['id'];
    $record['ad_img']         = $img['picture'];
    $record['img_folder']     = $img['folder'];

    $ads[] = $record;
} 

$smarty->assign('ads', $ads);

index.tpl

<html>
<head>
....
<body>
{$header_featured}
...
</body>
</html>

header_featured.tpl

 {foreach item=item  from=$ads} 
  {$item.ad_id}
  {$item.ad_title}
  {$item.img_id}
  {$item.ad_img}
  {$item.img_folder}
 {/foreach}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM