Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法调用php函数且无法显示结果_Php_Mysql_Smarty - Fatal编程技术网

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

无法调用php函数且无法显示结果,php,mysql,smarty,Php,Mysql,Smarty,有三个文件 1). class/header_.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

有三个文件 1). class/header_.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;
            } 
        }
    }
?>
<?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();
?>
基本上,我想在我的网站标题中显示特色广告,因此为此我创建了一个文件“classes/header\u featured.php”,其中我从数据库中获取行,然后我在根“h\u featured.php”中创建了另一个文件,其中我调用了特色广告的函数,并将变量分配给smarty文件“templates/h\u featured.html”但它对我来说不起作用,它看起来像没有显示或没有调用函数或没有分配数据


请帮助..

您的方法
getfeatured
不会返回任何内容。因此,
$featured\u ads
将为空。

在您的文件中,class/header\u 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;
        }
    }
?>

1)设计不好。2) 不要使用
mysql.*
函数。3) 您在getfeatured()函数中没有返回任何内容(请参见camelCase),感谢您指出。。但是你能为我的问题提供解决方案吗…?你最好了解正确的命名约定、用户定义函数、准备好的SQL语句等。在这种情况下,我不会简单地给你答案,那么你该怎么办呢。。。你能完成代码吗…没有成功。。。你相信其他代码可以吗…谢谢。。。它使用$obj=新标题_featured()$ads=$obj->getfeatured()$smarty->assign('ads',$ads);
<?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;
        }
    }
?>