skip to Main Content

I have this MySQL table MONTHNAME generated by querying from the database using the following script using PHP.

$query = 'SELECT monthname(`dateAdded`) as MONTHNAME,sum(`shift1PinCount`+`shift2PinCount`) from `supervisorupdate` WHERE YEAR(`dateAdded`) = YEAR(CURDATE()) group by monthname(`dateAdded`) ORDER BY monthname(`dateAdded`) DESC';
$queryExecute = mysqli_query($conn, $query);

I want to show this table as it is into my HTML page using PHP. As from I know normally we use

while($row = mysqli_fetch_array($queryExecute)){
   echo $row['someColumnName'];
}

In this case, I can’t do that since I am doing some calculations and getting output as a table. Can someone guide me on how to show this table?

Edit 1

Here is a picture of the result that I am getting using phpMyadmin. So the HTML table should be similar to this

enter image description here

So for HTML side, it should show something like this

<table class="table">
  <thead>
    <tr>
      <th scope="col" id="month">Month</th>
      <th scope="col" id="sum">Sum</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row" id="jan">January</th>
      <td id="janPinCount">3456</td>
    </tr>
    <tr>
      <th scope="row" id="feb">February</th>
      <td id="febPinCount">443</td>
    </tr>

  </tbody>
</table>

2

Answers


  1. you can do this. put this after the table body

    $row = mysqli_fetch_array($queryExecute);
    do{
    echo '   <tr>
          <th scope="row" id="jan">.$row["MONTHNAME"].</th>
          <td id="janPinCount">$row["SUM"]</td>
        </tr> '
    
    
    }while($row = mysqli_fetch_array($queryExecute))
    
    Login or Signup to reply.
  2. Try this one, I added an alias on your sum(shift1PinCount+shift2PinCount) column as PinCount.

    <?php
    $query = 'SELECT monthname(`dateAdded`) as MONTHNAME, sum(`shift1PinCount`+`shift2PinCount`) as PinCount from `supervisorupdate` WHERE YEAR(`dateAdded`) = YEAR(CURDATE()) group by monthname(`dateAdded`) ORDER BY monthname(`dateAdded`) DESC';
    $queryExecute = mysqli_query($conn, $query);
    ?>
    
    
    <table class="table">
      <thead>
        <tr>
          <th scope="col" id="month">Month</th>
          <th scope="col" id="sum">Sum</th>
        </tr>
      </thead>
      <tbody>
        <?php while($row = mysqli_fetch_array($queryExecute)){ ?>
        <tr>
          <th scope="row" id="<?=$row['MONTHNAME']?>"><?=$row['MONTHNAME']?></th>
          <td id="<?=$row['MONTHNAME']?>PinCount"><?=$row['PinCount']?></td>
        </tr>
        <?php } ?>
      </tbody>
    </table>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search