无法调用php函数而无法显示结果

时间:2014-05-20 20:41:26

标签: php mysql smarty

有三个文件 1)。类/ header_featured.php

<?php
    class header_featured{
        function getfeatured() {
            global $db;
            $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;
            } 
        }
    }
?>

2)。 h_featured.php

<?php

require_once "include/include.php";
require_once "classes/header_featured.php";
global $db;
$smarty = new Smarty;
$smarty = common($smarty);

$obj = new header_featured();
$featured_ads = $obj->getfeatured();
$smarty->assign('featured_ads ', $featured_ads );

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

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

3)。模板/ h_featured.html

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

基本上我想要的是,我想在我的网站标题中显示精选广告,所以为此我创建了一个文件&#34; classes / header_featured.php&#34;其中我是数据库中的fetchings行,然后我在root中创建了另一个文件&#34; h_featured.php&#34;我在其中调用功能广告的功能并将变量分配给smarty文件&#34; templates / h_featured.html&#34;但它对我不起作用,看起来没有显示或没有调用功能或没有分配数据......

请帮助..

2 个答案:

答案 0 :(得分:3)

您的方法getfeatured不会返回任何内容。因此$featured_ads将为空。

答案 1 :(得分:0)

在你的文件中,classes / header_featured.php应为:

<?php
    class header_featured{
        function getfeatured() {
            global $db;
            $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;
            }
            return $ads;
        }
    }
?>