0

I have login data that is stored in a MySQL database. From that data I was successfully able to group the logins every month organized by gender. For example, in January 1400 male users and 1500 female users logged in.

Now I'm struggling to display the data in a table.

Currently, I have it set up like this:

<div role="tabpanel" class="tab-pane fade active in" id="tab_content1" aria-labelledby="home-tab">
  <table id="datatable-fixed-header" class="table table-striped table-bordered">
    <thead>
      <tr>
        <th>Month</th>
        <th>Male Logins</th>
        <th>Female Logins</th>
      </tr>
    </thead>
    <tbody>
      <?php
        foreach ($maleLogin as $maleLog){
          $mthDateTime = DateTime::createFromFormat('n', $maleLog->month);
          foreach ($femaleLogin as $femaleLog) {
      ?>
      <tr>
        <td><?php echo $mthDateTime->format('F').' '.$maleLog->year; ?></td>
        <td><?php echo $maleLog->count;?></td>
        <td><?php echo $femaleLog->count;?></td>
      </tr>
      <?php } }?>
    </tbody>
  </table>
</div>

The problem is with my nested for loop. Currently, it displays the data like so.

2
  • what's the structure of the table? I think that the best aproach is to get the count of male/female logs on the same object/array so you only should use only one for loop. Commented Jun 19, 2018 at 16:37
  • @MikeVelazco what do you mean structure? The reason I had to use two loops was because the login data for males and females comes from their receptive individual tables. Commented Jun 19, 2018 at 16:39

3 Answers 3

1

the issue is foreach male you are looping through every female i would suggest some sort of sql query that joins the male and female based on date

SELECT * from male m LEFT join female f on m.date = f.date

something along those lines

Sign up to request clarification or add additional context in comments.

3 Comments

The data is stored in two different tables. Not sure how I can use one sql statement to query data from two individual tables.
Like i showed in the example you can join 2 tables together using join (left join, inner join etc...) and you can have 1 variable with combined data w3schools.com/sql/sql_join_left.asp
This makes sense. However I ended up using DFriend's solution. More straightforward.
1

You are displaying every male login multiple times, for every female login that exists with the nested loops. Perhaps only use 1 foreach loop and display all info even if 0?

<?php
        foreach ($Login as $Log){
              if(maleLog)
                    do something for male login
              else
                    do something for female login
        }
?>

...something along those lines. The SQL statement above is probably your best choice though...

Comments

0

I believe this will work for you. It utilizes the $key => $value form of foreach. $key is used to obtain the desired female count value.

This only works if your two datasets actually do have the exact same number records and are sorted exactly the same.

Note: I'm using Alternative syntax for control structures because it's a habit and because, IMO, it is easier to read when you're jumping in and out of the PHP processor. Likewise, I use <?= instead of <?php echo because it's a lot less typing.

<tbody>
    <?php
    foreach ($maleLogin as $key => $maleLog):
        $mthDateTime = DateTime::createFromFormat('n', $maleLog->month);
        ?>
        <tr>
            <td><?= $mthDateTime->format('F').' '.$maleLog->year; ?></td>
            <td><?= $maleLog->count; ?></td>
            <td><?= $femaleLogin[$key]->count; ?></td>
        </tr>
    <?php endforeach; ?>
</tbody>

3 Comments

Im getting the following error: $femaleLogin is an undefined variable.
You used that variable in the question. How was it set there? My assumption is it's passed to the view from the controller. No?
Sorry you are right. It was a typo on my part. This worked.

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.