0

I have a portion of my website where I can give awards to members. Right now I am working on creating individual description pages for each award. On that page I want to include which members have already received this award and display this data in an HTML table.

I have already passed the data into a Multidimensional array as seen below.

<?php 

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $currentAward = $awardid; // Current Page Award. Local value to the page.

    $result = $conn->query("SELECT * FROM vms_awardsgranted WHERE awardid='$currentAward'");

    $awardsList = array();
    while($pilotsList = $result->fetch_assoc()){
        $awardsList[ $pilotsList["id"] ] = $pilotsList;
    }

    echo nl2br("DEBUG: $currentAward \n");
    print_r(array_values($awardsList));

    $conn->close();
?>

Example Result

DEBUG: 8 
Array ( [0] => Array ( [id] => 28 [awardid] => 8 [pilotid] => 4 [dateissued] => 2015-10-14 20:12:21 ) [1] => Array ( [id] => 32 [awardid] => 8 [pilotid] => 1 [dateissued] => 2015-10-14 20:14:14 ) )

From here I am trying to parse this info and add it into the HTML table below but I honestly can't find the correct way of doing this with a Multidimensional array. Can anyone out here give me some insight? My HTML tables looks like below.

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ocean_table">
    <thead>
        <tr>
        <th>Pilot ID</th>
        <th>Pilot Name</th>
        <th>Date Awarded</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td align="center">

            </td>
            <td align="center">

            </td>
            <td align="center">

            </td>
        </tr>
    </tbody>
</table>

2 Answers 2

1

Basically, to loop through the result, your code could look like this:

  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="ocean_table">
      <thead>
          <tr>
          <th>Pilot ID</th>
          <th>Pilot Name</th>
          <th>Date Awarded</th>
          </tr>
      </thead>
      <tbody>
  <?php foreach($awardsList as $member):?>    

          <tr>
              <td align="center">
                <?=$pilot['pilotid']?>
              </td>
              <td align="center">
              </td>
              <td align="center">
                <?=$pilot['dateissued']?>
              </td>
          </tr>
  <?php endforeach;?>    

      </tbody>
  </table>

A couple of remarks, though.

  • You don't return the pilot name from the query. You should join on the table of members/pilots to get their name.
  • Your naming is confusing. $pilotList suggests a list of pilots, but instead that variable contains only the reference/junction between the award and one pilot. Dito for awardsList which actually contains a list of people that won the award.
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, my variable names are honestly so muttled together right now. I had got lost in my page code and started mixing up variables.. Thanks for pointing that out. I will get that cleaned up now. edit thanks for noticing the lack of pilot name in my table.
This works perfect. But instead of just copying your code could you tell me exactly whats going on here so I can learn? I see that your passing each row of array data as $pilot, but what is actually happening there? Are you defining each row as $pilot and then are you querying each row via the <?=$pilot['pilotid']?> call and its finding pilotid in that row?
Er yes. Quite simply, the code contains a foreach loop that iterates over $awardsList. On each iteration the next item is made available in the variable $pilot (since awardsList actually contains people, as I mentioned before). So that code just generates a table row for each item in that array. Then <?=$pilot['pilotid']?>. <?= is just a shorthand for <?php echo , so that code just echoes the values from $pilot. Apart from that it's just your code unmodified. The answer by @JulioSoares shows the same solution, only using a slightly different notation for the loop and the echo.
Thank you so much for taking the extra time to explain. Rather then abusing this great service and just having people write stuff for me I actually have this burning passion to learn. Thanks for that.
1

Something like

<tbody>
    <?php foreach($awardsList as $record){ ?>
    <tr>
        <td align="center">
              <?php echo $record['id']; ?>
        </td>
        <td align="center">
              <?php echo $record['awardid']; ?>
        </td>
        <td align="center">
              <?php echo $record['pilotid']; ?>
        </td>
    </tr>
    <?php } ?>
</tbody>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.