Al Kasih
Al Kasih

Reputation: 886

Else in foreach looping

What I want if the result is not found from the query select, I want to show else function like:

 else {
           $html .= 'You have not added stock at all';
       }

Because in this case I used string of html, I don't know how to echo the else statement.

More or less my codes is looking like this right now (There are many parts I have removed since it's too long)

 <?php
 include("../actions/config.php");  
 $resultsClaim = $mysqli->query("SELECT");
 $orders = array();     
 $html = '';     
    if ($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;
        ?>

Can anyone help me, please! Thanks in Advance.

Upvotes: 0

Views: 74

Answers (4)

Venkata Krishna
Venkata Krishna

Reputation: 1776

Since you are taking the no of orders into a variable, you can make use of that directly to see if the orders exists or not.

$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";
    }

Here you dont need to concatenate it to $html since you are displaying the table content if order exists or else you displaying "Nothing". So $html will either have the table content or else "Nothing".

Hope this helps.

Upvotes: 2

Umair Ayub
Umair Ayub

Reputation: 21271

Give it a try

<?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;
?>

Upvotes: 0

Tristup
Tristup

Reputation: 3663

foreach ($orders AS $order_id => $order) { } else {}

So "}" is missing for foreach loop. Hope it will work for you. Let me know if you need further help.

Upvotes: 0

Pupil
Pupil

Reputation: 23958

First get count of $orders

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:
}

Upvotes: 0

Related Questions