Advertisement
tok124

Select Array in Table

May 13th, 2022
1,378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.09 KB | None | 0 0
  1. <table border="1">
  2.   <tr>
  3.     <th>ID</th>
  4.     <th>Customer Name</th>
  5.     <th>Customer Email</th>
  6.     <th>Date/Time</th>
  7.     <th>Payment</th>
  8.   </tr>
  9. <?php
  10.   $conn = mysqli_connect("127.0.0.1", "root", "ascent", "world");
  11.  
  12.   $stmt = $conn->prepare("SELECT * FROM tablename");
  13.   $stmt->execute();
  14.   $result = $stmt->get_result();
  15.   if($result->num_rows > 0) {
  16.     while($rows = $result->fetch_all(MYSQLI_ASSOC)) {
  17.       $price = array_column($rows, 'price'); // <== price is column name of the price column. So you may replace this if the name of column is not price
  18.       $sumprice = array_sum($price);
  19.       foreach($rows as $row) {
  20.         echo "<tr><td>".$row['id']."</td><td>".$row['name']."</td><td>".$row['email']."</td><td>".$row['timestamp']."</td><td>".$row['payment']."</td></tr>"; // the values inside $row[] is column names. You need to replace these names with the column names you are using in your table
  21.       }
  22.       echo "<tr>";
  23.       echo "<td colspan='2'>Total Gross</td>";
  24.       echo "<td colspan='3'>".$sumprice."</td>";
  25.       echo "</tr>";
  26.     }
  27.   }
  28. ?>
  29. </table>
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement