简体   繁体   中英

PHP-Mysql : Php echo values from sql out put from 1st and second row

I am trying to build a web page (using css) that shows me values for today and tomorrow. I can successfully get the values for today but cannot see / get the values for tomorrow which are on the next row. I require the next row column Fajr Begins on my webpage (php code).

Mysql query:

select *  from prayers where Date IN (CURDATE(), DATE_ADD(CURDATE(), INTERVAL 1 DAY)) LIMIT 2;

output:

| Date       | Day  | Fajr Begins | Fajr Jamaat | Sunrise | Zohr Begins | Zohr Jamaat | Asr Begins | Asr Jamaat | Magrib Begins | Magrib Jamaat | Isha Begins | Isha Jamaat | Jumah 1 | Jumah 2 | Day of week |
+------------+------+-------------+-------------+---------+-------------+-------------+------------+------------+---------------+---------------+-------------+-------------+---------+---------+-------------+
| 2021-10-15 | Fri  | 05:33       | 06:00       | 07:23   | 12:50       | *           | 15:33      | 16:15      | 18:11         | 18:16         | 19:59                 |   | 13:20   | 14:15   |
| 2021-10-16 | Sat  | 05:34       | 06:15       | 07:24   | 12:50       | 13:30       | 15:32      | 16:00      | 18:09         | 18:14         | 19:57                 |   | 13:20   | 14:15   |
+------------+------+-------------+-------------+---------+-------------+-------------+------------+------------+---------------+---------------+------------+

All the column names are the same so I cannot alias them (which wouldve been easy)..

Here is my PHP Script (I've only taken a small snippet from the first entry of the html table):

 <?php
 include "dbConn.php"; // Using database connection file here
 $records = mysqli_query($db,"select *  from prayers where Date IN (CURDATE(), 
  DATE_ADD(CURDATE(), INTERVAL 1 DAY)) LIMIT 2;" ); // fetch data from database

  while($data = mysqli_fetch_array($records))
 {
 ?>

    <div class="u-table u-table-responsive u-table-1">
      <table class="u-table-entity">
        <colgroup>
          <col width="100%">
        </colgroup>
        <tbody class="u-table-body">
          <tr style="height: 118px;">
            <td class="u-align-center u-table-cell u-table-cell-1"><?php echo $data['Fajr 
   Begins']; ?></td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="u-table u-table-responsive u-table-2">
      <table class="u-table-entity">
        <colgroup>
          <col width="100%">
        </colgroup>
        <tbody class="u-table-body">
          <tr style="height: 118px;">
            <td class="u-align-center u-table-cell u-text-palette-3-base u-table-cell-2"><?php 
   echo $data['Fajr Jamaat']; ?></td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="u-table u-table-responsive u-table-3">
      <table class="u-table-entity">
        <colgroup>
          <col width="100%">
        </colgroup>
        <tbody class="u-table-body">
          <tr style="height: 72px;">
            **<td class="u-align-center u-table-cell u-text-palette-2-base u-table-cell-3">Tommorrow Data</td>**
          </tr>
        </tbody>
      </table>
    </div>

  <?php 
  }
  ?>

   <?php mysqli_close($db); // Close connection ?>

Webpage attached.

I've exhausted all my options searching but cannot find what the solution. Please help pro's!

Your query is returning the correct data and is limited to two rows. When you come to create the HTML tables your while loop is only reading one row at a time, so you never see tomorrow's data.

However, there's no guarantee which order the data is returned in, so you need to add an ORDER BY Date ASC clause.

Since you're only returning two rows you don't need a while statement. You just need two successive fetches, something like this:

<?php
 include "dbConn.php"; // Using database connection file here

// Query updated to include an ORDER BY clause

 $records = mysqli_query($db,"select *  from prayers where Date IN (CURDATE(), 
  DATE_ADD(CURDATE(), INTERVAL 1 DAY)) ORDER BY Date ASC LIMIT 2;" ); // fetch data from database

// read first row with today's data
$todaysData = mysqli_fetch_array($records);

// Create table for today here

// Now read second row, with tomorrow's data.
$tomorrowData = mysqli_fetch_array($records);

// Create table for tomorrow here.

mysqli_close($db);  // not strictly required since PHP will do this anyway


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