程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

php foreach outside the function

发布于2023-01-19 16:47     阅读(1001)     评论(0)     点赞(29)     收藏(1)


i'm learning php.

i have a function with mysql query select.

than i use it with foreach list..

but it work me only if the foreach is inside the function. i don't know how to get it work outside the function..

what i'm doing wrong?

working code - https://phpbox.info/d3GCP

no working code:

function volaco ()
{
$query = $db->getQuery(true);

$query = "select a.id, count(i.id) as all_items, a.name,  SUM(i.state = '1') published, SUM(i.state = '0') unpublished"
    . " FROM item as i"
    . " JOIN application a ON a.id = i.application_id"
    . " group by i.application_id";

$db->setQuery($query);

$apps= $db->loadObjectList();

}

$apps = volaco();
?>

    <table >
        <?php if (count($apps)) : foreach ($apps as $app) : ?>
            <tr >
                <td  width="40%"><?php echo $app->name; ?></td>
                <td  width="20%" style="text-align: center;"><?php echo $app->all_items;?></td>
                <td  width="20%" style="text-align: center;"><?php echo $app->published; ?></td>
                <td  width="20%" style="text-align: center;"><?php echo $app->unpublished; ?></td>
            </tr>
        <?php endforeach; else : ?>
<?php endif; ?>
    </table>

thanks a lot


解决方案


You function volaco() need return type

return $apps= $db->loadObjectList();

Read Returning values



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.phpheidong.com/blog/article/476826/0642fe1a70818b50365b/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

29 0
收藏该文
已收藏

评论内容:(最多支持255个字符)