Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 721343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:51:45+00:00 2026-05-14T05:51:45+00:00

I’ve been struggling for a while with this one; I’ll try to explain it

  • 0

I’ve been struggling for a while with this one; I’ll try to explain it here as simply as possible.

Consider this MySQL table:

+----------+-----------+---------+--------+
|status_id |session_id |pilot_id |present |
+----------+-----------+---------+--------+
|1         |61         |901      |1       |
|2         |63         |901      |1       |
|3         |62         |901      |0       |
|4         |62         |902      |1       |
|5         |63         |903      |1       |
+----------+-----------+---------+--------+

Both session_id and pilot_id are foreign keys making reference to a primary key in another table. The same pilot_id can be associated with different session_id, but every pilot_id–session_id combination is unique.

I need to make an HTML table (in PHP) that would display the data like this:

+----------+---------+---------+---------+
|          |61       |62       |63       |
+----------+---------+---------+---------+
|901       |X        |         |X        |
|902       |         |X        |         |
|903       |         |         |X        |
+----------+---------+---------+---------+

Hence, rows are pilot_id and columns are session_id. When a pilot_id–session_id combination has a present value of 1, the corresponding cell should be checked. (ie. when the row-combination is zero or the combination does not exist in the MySQL table, nothing should appear in the HTML table)

Phew.

Any ideas?

Thanks!


I’ve tried the answer proposed by erisco, but I’m quite confused. (the comment field is much too small for my explanation, hence this update to my question).

This is the actual data I am working with:

+----------+-----------+---------+--------+
|status_id |session_id |pilot_id |present |
+----------+-----------+---------+--------+
|7         |65         |33       |1       |
|8         |66         |33       |1       |
|9         |65         |17       |0       |
|10        |66         |16       |1       |
+----------+-----------+---------+--------+

I use $rows = mysqli_fetch_array($result);. I have confirmed the query is returning the right data.

However, when I use the answer proposed by ericso, I am getting seemingly arbitrary data. Here’s the generated HTML table:

+----------+---------+---------+---------+---------+
|          |1        |3        |6        |7        |
+----------+---------+---------+---------+---------+
|1         |X        |         |         |         |
|3         |         |         |         |         |
|6         |         |         |         |         |
|7         |         |         |         |         |
+----------+---------+---------+---------+---------+

Furthermore the ‘X’ position stays the same irrelevantly of present values.

Any ideas why this is happening?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:51:45+00:00Added an answer on May 14, 2026 at 5:51 am

    Luckily you only need one query. Presuming $rows is the format of your data withdrawn from the database:

    <?php
    
    $rows = array(
      array(
        'status_id' => 1,
        'session_id' => 61,
        'pilot_id' => 901,
        'present' => 1,
      ),
      array(
        'status_id' => 2,
        'session_id' => 63,
        'pilot_id' => 901,
        'present' => 1,
      ),
      array(
        'status_id' => 3,
        'session_id' => 62,
        'pilot_id' => 901,
        'present' => 0,
      ),
      array(
        'status_id' => 4,
        'session_id' => 62,
        'pilot_id' => 902,
        'present' => 1,
      ),
      array(
        'status_id' => 5,
        'session_id' => 63,
        'pilot_id' => 903,
        'present' => 1,
      )
    );
    
    $session_ids = array();
    $pilot_ids = array();
    $crosses = array();
    
    foreach ($rows as $row) {
      $session_ids[$row['session_id']] = $row['session_id'];
      $pilot_ids[$row['pilot_id']] = $row['pilot_id'];
      if ($row['present'] == 1) {
        $cross_index = $row['session_id'].'.'.$row['pilot_id'];
        $crosses[$cross_index] = $cross_index;
      }
    }
    
    sort($session_ids);
    sort($pilot_ids);
    
    ?>
    
    <table>
      <tr>
        <th></th>
      <?php foreach ($session_ids as $sess_id): ?>
        <th><?php echo $sess_id; ?></th>
      <?php endforeach; ?>
      </tr>
      <?php foreach ($pilot_ids as $pilot_id): ?>
      <tr>
        <th><?php echo $pilot_id; ?></th>
        <?php foreach ($session_ids as $sess_id): ?>
        <?php if (isset($crosses[$sess_id.'.'.$pilot_id])): ?>
        <td>X</td>
        <?php else: ?>
        <td></td>
        <?php endif; ?>
        <?php endforeach; ?>
      </tr>
      <?php endforeach; ?>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.