1

I have this code:

<?php 

if( isset($_POST['groups'])){ 

    $groups = $_POST['groups'];  
    $subject = $_POST['subject']; 

$sql="SELECT 
    a.groupcode, a.groupstudents, a.studentid, 
    b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno 

FROM    table_1 a, table_2 b

WHERE   b.groupcode = '$groups' AND b.coursename = '$subject' AND 
        (a.studentid = b.studentid AND a.groupcode = b.groupcode)";

$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql); ?>

<table width="100%" border="1" cellspacing="0" cellpadding="3" > 
<tr> 
    <td align="center"><strong><font size="2">Students</font></strong></td> 
    <td align="center"><strong><font size="2">Date</font></strong></td> 
    <td align="center"><strong><font size="2">Attendance</font></strong>    </td> 
</tr> 

<?php 
while($rows=mysqli_fetch_array($result)){ 

    $date = $rows['date']; $date2 = date("d-F-Y", strtotime($date));

    $class1 = $rows['class1'];
        if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
        if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";
?> 

<tr> 
<td align="center"><font size="2"><?php echo $rows['groupstudents']; ?></font>    </td>
<td align="center"><strong><font size="2"><?php echo $date2; ?></font></strong>    </td> 
<td align="center"><font size="2"><?php echo $class1; ?></font></td>
</tr>

<?php 
    }
?> 

which gives the below output. current

Now my question is how to modify my code (use nested loops?!) so the output is:

modified

Thank you kindly.

NB: Sorry, I do not have enough reputation to attach images. I have uploaded them on an external site.

3
  • Please, you're using mysqli which is good, but PLEASE PLEASE PLEASE use prepared statements! (incidentally, they're really better performance wise inside loops) Commented Mar 11, 2015 at 13:41
  • I WILL I WILL I WILL :) +1 Commented Mar 11, 2015 at 13:43
  • Thank you, prepared statements are the best. Commented Mar 11, 2015 at 14:01

1 Answer 1

1

It may not be the best solution, but I cannot think of something better right now.
In the pre-execution I create the grid you want, and in the layout this grid-array is displayed.

<?php 

if( isset($_POST['groups'])){ 

    $groups = $_POST['groups'];  
    $subject = $_POST['subject']; 

$sql="SELECT 
    a.groupcode, a.groupstudents, a.studentid, 
    b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno 

FROM    table_1 a, table_2 b

WHERE   b.groupcode = '$groups' AND b.coursename = '$subject' AND 
        (a.studentid = b.studentid AND a.groupcode = b.groupcode)";

$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql); 

$dates = array();
$display = array();

while ($rows=mysqli_fetch_array($result)) {
    if (!isset($display[$rows['groupstudents']])) {
        $display[$rows['groupstudents']] = array();
    }

    if (!isset($dates[strtotime($rows['date'])])) {
        $dates[strtotime($rows['date'])] = count($dates);
    }

    $class1 = $rows['class1'];
    if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
    if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";

    $display[$rows['groupstudents']][$dates[strtotime($rows['date'])]] = $class1;
}

echo '<table width="100%" border="1" cellspacing="0" cellpadding="3">';
echo '<tr>';
echo '<td align="center"><strong><font size="2">Students</font></strong></td>';
foreach ($dates as $date => $reversedIndex) {
    echo '<td align="center"><strong><font size="2">' . date("d-F-Y", $date) . '</font></strong></td>';
}
echo '</tr>';

foreach ($display as $student => $row) {
    echo '<tr>';
    echo '<td align="center"><font size="2">' .  $student . '</font></td>';

    foreach ($dates as $date => $index) {
        echo '<td align="center"><font size="2">';
        if (isset($row[$index])) {
            echo $row[$index];
        } else {
            echo '';
        }
        echo '</font></td>';
    }
    echo '</tr>';
}

echo '</table>';
?>
Sign up to request clarification or add additional context in comments.

1 Comment

It only shows the student names. The other columns are just blank!

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.