Php foreach循环中的Else

Php foreach循环中的Else,php,Php,如果在查询select中找不到结果,我希望显示else函数,如: else { $html .= 'You have not added stock at all'; } 因为在本例中我使用了html字符串,所以我不知道如何回显else语句 或多或少我的代码现在看起来是这样的(由于太长,我删除了很多部分) 谁能帮帮我吗!提前谢谢 首次获取$订单的计数 if (! empty($orders)) { foreach ($orders AS $order

如果在查询select中找不到结果,我希望显示else函数,如:

 else {
           $html .= 'You have not added stock at all';
       }
因为在本例中我使用了
html
字符串,所以我不知道如何回显else语句

或多或少我的代码现在看起来是这样的(由于太长,我删除了很多部分)



谁能帮帮我吗!提前谢谢

首次获取<代码>$订单的计数

if (! empty($orders)) {
 foreach ($orders AS $order_id => $order) {
  // YOUR CODE HERE
 }
}
else {
 $html .= 'Nothing'; // Append your `no records found` message to the `$html` variable:
}
所以foreach循环缺少“}”。希望它对你有用。如果您需要进一步帮助,请告诉我。

试试看

<?php

include("../actions/config.php");
$resultsClaim = $mysqli->query("SELECT");
$orders = array();
$html = '';
if (!empty($resultsClaim)) {
    while ($obj = $resultsClaim->fetch_object()) {
        $orders [$obj->id_cart][$obj->items] = array('status' => $obj->status);
    }

    foreach ($orders AS $order_id => $order) {
        $orderCount = count($order);
        $html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
        $row = 1;

        foreach ($order AS $item => $data) {
            if ($row > 1) {
                $html .= '</tr><tr>';
            }
            $html .= '<td>' . $item . '</td>';
            $row++;
        }
        $html .= '<div>
                            <div class="member-popUpeStock' . $data['id'] . ' member-PopUp">
                            <div class="member-PopUp-box">
                            <a href="" title="Close" class="close">X</a>

                            <div class="tablePopUp">
                            <div class="table-row">
                            <div class="col">: ' . $data['method'] . ' </div>';
    }
}
///HERE WHERE I WANT TO DO THAT////
else {
    echo 'Nothing';
}
echo $html;
?>

由于您要将订单数量带入一个变量,因此可以直接使用该变量查看订单是否存在

$orderCount = 0;  //Define orderCount in the beginning.

   //Rest of the code, till foreach

 foreach(){
       //Rest of the code here
    }
    if(intval($orderCount) <= 0)  //Using intval, for the case the for loop is not executed at all
    {
      $html = "Nothing";
    }
$orderCount=0//在开头定义orderCount。
//代码的其余部分,直到foreach
foreach(){
//剩下的代码在这里
}

if(intval($orderCount)
if($orders){foreach($orders…{…}}}else{}
我已经试过了,但它不起作用。到底什么
不起作用
?else语句不显示该语句,即使我显示pagesource尝试注释掉所有
$orders
填充行以进行调试,它应该起作用HM,php
版本向
foreach
添加的
else
分支是什么编程语言可以做到这一点。你可以做一个标志,然后在循环后做决定。我知道,python就是这么做的:)那么我现在应该做什么呢?它显示了else语句,但也给了我一个
注意:未定义的变量:orderCount in…
你可以在代码的开头定义orderCount
$orderCount=0我以前叫它
简单完成时
谢谢。
<?php

include("../actions/config.php");
$resultsClaim = $mysqli->query("SELECT");
$orders = array();
$html = '';
if (!empty($resultsClaim)) {
    while ($obj = $resultsClaim->fetch_object()) {
        $orders [$obj->id_cart][$obj->items] = array('status' => $obj->status);
    }

    foreach ($orders AS $order_id => $order) {
        $orderCount = count($order);
        $html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
        $row = 1;

        foreach ($order AS $item => $data) {
            if ($row > 1) {
                $html .= '</tr><tr>';
            }
            $html .= '<td>' . $item . '</td>';
            $row++;
        }
        $html .= '<div>
                            <div class="member-popUpeStock' . $data['id'] . ' member-PopUp">
                            <div class="member-PopUp-box">
                            <a href="" title="Close" class="close">X</a>

                            <div class="tablePopUp">
                            <div class="table-row">
                            <div class="col">: ' . $data['method'] . ' </div>';
    }
}
///HERE WHERE I WANT TO DO THAT////
else {
    echo 'Nothing';
}
echo $html;
?>
$orderCount = 0;  //Define orderCount in the beginning.

   //Rest of the code, till foreach

 foreach(){
       //Rest of the code here
    }
    if(intval($orderCount) <= 0)  //Using intval, for the case the for loop is not executed at all
    {
      $html = "Nothing";
    }