简体   繁体   中英

Issue in php mysql code. How to get specific ordered data from db and dispay to inside <td> as a table view in php?

I used to get data from db and display to a table inside (see image) It display all the ordered data from the db to the (Item, Qty,Price,Total). But i need only specific ordereditem filtered by orderno. Whats wrong in below code?
Solve this issue and Thanks in advance. 在此处输入图像描述 Below is the php mysql method.

public function read()
  {
    $data = array();
    $query = "SELECT DISTINCT `orderdate`,`orderno`,`waitername`,`tablename` FROM `entrysales` WHERE `billstatus`='unbilled' ORDER BY id DESC";
    $execute = mysqli_query($this->conn,$query);
    while($row=mysqli_fetch_assoc($execute))
    {
      $data[]=$row;
      //$orderno = $row['orderno'];
    }
    return $data;
  }
  public function readitems(){
    $data1 = array();
  echo  $query = "SELECT DISTINCT `orderno` FROM `entrysales` WHERE `billstatus`='unbilled' ORDER BY id DESC";
    $execute = mysqli_query($this->conn,$query);
    while($row=mysqli_fetch_assoc($execute))
    {
      //$data[]=$row;
      $orderno = $row['orderno'];
      echo  $query1 = "SELECT * FROM `entrysales` WHERE `orderno`='$orderno' ORDER BY id DESC";
      $execute1 = mysqli_query($this->conn,$query1);
      while($row1=mysqli_fetch_assoc($execute1))
      {
        $data1[] = $row1;
      }      
      
    }    
    return $data1;
  }

And below is the php html code

$output = '';
          $data = $db2->read();
          $data1 = $db2->readitems();
foreach ($data as $row) {
                  $count++;
                  $output .='<tr class="text-center text-secondary">
                              <td>'.$count.'</td>
                              <td>'.$row['orderdate'].'</td>
                              <td>'.$row['orderno'].'</td>
                              <td>'.$row['waitername'].'</td>
                              <td>'.$row['tablename'].'</td>';
              
                   $output .='<td><table class="table">';
                   
                   foreach($data1 as $row1){              
                   $output .='<tr><td>'.$row1['orderitem'].'</td>
                              <td>'.$row1['orderqty'].'</td>
                              <td>'.$row1['unitprice'].'</td>
                              <td>'.$row1['orderprice'].'</td></tr>';
                   }
                   $output .='</table></td>';
                   $output .='<td>                                
                                  <a href="" title="Edit details" class="text-primary editEntrySalesBtn" id="'.$row['orderno'].'" data-toggle="modal" data-target="#addTakeOrderModal"><i class="fas fa-edit fa-lg"></i></a>&nbsp;&nbsp;
                                  <a href="" title="Delete details" class="text-danger delEntrySalesBtn" id="'.$row['orderno'].'"><i class="fas fa-trash-alt fa-lg"></i></a>
                              </td>
                              </tr>';
                  }
                  $output .='</tbody></table>';
                  echo $output;
public function readitems(){
    //$orderno = array();
    //$output='';
    //$execute1 = array();
  echo  $query = "SELECT DISTINCT `orderno` FROM `entrysales` WHERE `billstatus`='unbilled' ORDER BY id DESC";
    $execute = mysqli_query($this->conn,$query);
    while($row=mysqli_fetch_array($execute))
    {
      //$data1[]=$row;
      $orderno = $row['orderno'];
      
      echo  $query1 = "SELECT * FROM `entrysales` WHERE `orderno`='$orderno' ORDER BY id DESC";
      $execute1 = mysqli_query($this->conn,$query1);
      foreach($execute1 as $row1)
      {
             //echo "Order No:".$row['orderno']." Ordered item is ".$row1['orderitem']."<br>";
             echo $row1['orderitem'];
      }    
      //unset($row1);
    }  
    //return $row1; 
  }

here echo $row1['orderitem']; second query executes fine and it correctly displays ordered items based on orderno. But when i call this method it prints nothing in output below is php html code to print. When i check echo json_encode($execute1); it prints null whats wrong with below code?

$data = $db2->read();
$execute1 = $db2->readitems();
echo json_encode($execute1);
foreach ($data as $row) {
                  $count++;
                  $output .='<tr class="text-center text-secondary">
                              <td>'.$count.'</td>
                              <td>'.$row['orderdate'].'</td>
                              <td>'.$row['orderno'].'</td>
                              <td>'.$row['waitername'].'</td>
                              <td>'.$row['tablename'].'</td>';
              
                   $output .='<td><table class="table">';
                   $output .= $execute1;
                   $output .='</table></td>';
                   $output .='<td>                                
                                  <a href="" title="Edit details" class="text-primary editEntrySalesBtn" id="'.$row['orderno'].'" data-toggle="modal" data-target="#addTakeOrderModal"><i class="fas fa-edit fa-lg"></i></a>&nbsp;&nbsp;
                                  <a href="" title="Delete details" class="text-danger delEntrySalesBtn" id="'.$row['orderno'].'"><i class="fas fa-trash-alt fa-lg"></i></a>
                              </td>
                              </tr>';
                  }
                  $output .='</tbody></table>';
                  echo $output;

i've changed readitems method like below to get desired output as i wanted. Thanks to the one who tried to help. m.finger to the downvoters!

public function readitems()
  {
    $output = '';
    $output .='<table id="table8" class="table table-striped table-sm table-bordered">
              <thead>
                  <tr class="text-center">
                      <th>Sl.No</th>
                      <th>Order Date</th>
                      <th>Order No</th>
                      <th>Waiter Name</th>
                      <th>Table Name</th>
                      <th>
                        <table id="table9" class="table table-striped table-sm table-bordered">
                          <tr>
                            <th>Item</th>
                            <th>Qty</th>
                            <th>Rate</th>
                            <th>Total</th>
                          </tr>
                        </table>
                      </th>                                    
                      <th>Action</th>
                  </tr>
              </thead>
              <tbody>';
    $count = 0;
    $query = "SELECT DISTINCT `orderno`,`orderdate`,`waitername`,`tablename` FROM `entrysales` WHERE `billstatus`='unbilled' ORDER BY `orderno` DESC";
    $execute = mysqli_query($this->conn,$query);
    while($row3=mysqli_fetch_assoc($execute))
    {
      echo $orderno = $row3['orderno'];
      $count++;      
      $output .='<tr class="text-center text-secondary">
                              <td>'.$count.'</td>
                              <td>'.$row3['orderdate'].'</td>
                              <td>'.$row3['orderno'].'</td>
                              <td>'.$row3['waitername'].'</td>
                              <td>'.$row3['tablename'].'</td>';
      $output .='<td><table class="table">';
      echo  $query1 = "SELECT * FROM `entrysales` WHERE `orderno`='$orderno' ORDER BY id DESC";
      $execute1 = mysqli_query($this->conn,$query1);
      foreach($execute1 as $row)
      {
        $output .='<tr class="text-center text-secondary">
                    <td>'.$row['orderitem'].'</td>
                    <td>'.$row['orderqty'].'</td>
                    <td>'.$row['unitprice'].'</td>
                    <td>'.$row['orderprice'].'</td>
                  </tr>';                   
      }   
      $output .='</table></td>';
      $output .='<td>                                
                    <a href="" title="Edit details" class="text-primary editEntrySalesBtn" id="'.$row3['orderno'].'" data-toggle="modal" data-target="#addTakeOrderModal"><i class="fas fa-edit fa-lg"></i></a>&nbsp;&nbsp;
                    <a href="" title="Delete details" class="text-danger delEntrySalesBtn" id="'.$row3['orderno'].'"><i class="fas fa-trash-alt fa-lg"></i></a>
                  </td>
                  </tr>';     
    }

    $output .='</tbody></table>';
    return $output;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM