【问题标题】:Return multiple vars inside loop PHP在循环PHP中返回多个变量
【发布时间】:2017-05-14 21:25:09
【问题描述】:

我想在loop 中返回多个通知。我有 2 个功能,第一个从数据库中提取通知,第二个用于仪表板标题。仪表板标题是显示通知的位置。

现在,问题是,由于某种原因,只会显示一个通知。我尝试将loop 中的return 更改为echo,但这会在dashboardTop() 的开头输出通知。

出了什么问题,我该如何解决?

public function loopNotifications() {
    $stmt = $this->conn->prepare("SELECT * FROM notification WHERE isto=:user_id");
    $stmt->execute(array(':user_id'=>$_SESSION['user_id']));

    while ($notification = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $from = $this->pullName($notification['isto']);
        return '<li><a href="javascript:;">' . $notification['content'] . '</a></li>';
    }
}

public function dashboardTop() {
    echo '
                <div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav navbar-right">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                <i class="material-icons">notifications</i>
                                <span class="notification">' . $this->notificationCount() . '</span>
                                <p class="hidden-lg hidden-md">Notifications</p>
                            </a>
                            <ul class="dropdown-menu">
                                ' . $this->loopNotifications() . '
                            </ul>
                        </li>
                        <li>
                            <a href="index.php?page=profile" class="dropdown-toggle" data-toggle="dropdown">
                               <i class="material-icons">person</i>
                               <p class="hidden-lg hidden-md">Profile</p>
                            </a>
                        </li>
                    </ul>
                </div>
    ';
}

【问题讨论】:

  • 你不能......
  • 你应该将“通知”作为参数传递给dashboardTop(),然后循环遍历它们
  • return 结束执行,所以无论有多少行,你只会得到一个。连接成一个字符串,然后返回
  • loopNotifications() 在第一次迭代后立即返回。

标签: php loops pdo return echo


【解决方案1】:
public function loopNotifications() {
    $stmt = $this->conn->prepare("SELECT * FROM notification WHERE isto=:user_id");
    $stmt->execute(array(':user_id'=>$_SESSION['user_id']));
    $out=''; //empty var tof concaternation
    while ($notification = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $from = $this->pullName($notification['isto']);
        $out.= '<li><a href="javascript:;">' . $notification['content'] . '</a></li>'; //concaternate each line in to string
    }
return $out; //return whole string
}

public function dashboardTop() {
    echo '
                <div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav navbar-right">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                <i class="material-icons">notifications</i>
                                <span class="notification">' . $this->notificationCount() . '</span>
                                <p class="hidden-lg hidden-md">Notifications</p>
                            </a>
                            <ul class="dropdown-menu">
                                ' . $this->loopNotifications() . '
                            </ul>
                        </li>
                        <li>
                            <a href="index.php?page=profile" class="dropdown-toggle" data-toggle="dropdown">
                               <i class="material-icons">person</i>
                               <p class="hidden-lg hidden-md">Profile</p>
                            </a>
                        </li>
                    </ul>
                </div>
    ';
}

【讨论】:

    猜你喜欢
    • 2014-06-02
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 2011-05-08
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多