Skip to content Skip to sidebar Skip to footer

Each Value Is Displayed In A New Row HTML Table

I'm currently working on a small website where my colleagues can see their work times. My 'rooster' table looks like this: rooster_id int(2) Auto_increment primary key. personeel_

Solution 1:

Here we go entire example:

<?php
    $con = mysqli_connect('localhost', 'root', '', 'test') or die(mysqli_error($con));
    $query = "SELECT * FROM personeel INNER JOIN rooster ON rooster.personeel_id = personeel.id";
    $result = mysqli_query($con, $query) or die(mysqli_error($con));

    if (mysqli_num_rows($result) > 0) {
        $arr = array();
        $nam = array();
        $day = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
        while ($row = mysqli_fetch_assoc($result)) {
            $nam[$row['id']] = $row['naam'];
            $arr[$row['id']][$row['dag']] = $row['start'] . ' - ' . $row['eind'];
        }
        echo '<table border="1">';
            echo '<tr>';
                echo '<td>Employees</td>';
                foreach($day as $d){
                    echo '<td>'.$d.'</td>';
                }
            echo '</tr>';
            foreach ($nam as $k=>$n){
                echo '<tr>';
                    echo '<td>'.$n.'</td>';
                    foreach ($day as $d){
                        if(isset($arr[$k][$d])){
                            echo '<td>'.$arr[$k][$d].'</td>';
                        }else{
                            echo '<td>Virj</td>';
                        }
                    }
                echo '</tr>';
            }
        echo '</table>';
    }
?>

Output is:

enter image description here

Some details:

$nam array stores all fetched names from personeel together with his id relation.

Array
(
    [1] => Tom
    [2] => Jack
)

$arr is main array which contains relation between personeel id and his data:

Array
(
    [1] => Array
        (
            [Tuesday] => 12:00 - 21:00
            [Wednesday] => 15:00 - 21:00
        )

    [2] => Array
        (
            [Monday] => 08:00 - 18:30
        )

)

$day is static array with week days.

Walking trough both $nam and $arr arrays gives you desired table without any duplicates. In addition provided solution uses mysqli_* functions, so you can see how to use them.

Hope this will help you to solve your problem. :)


Post a Comment for "Each Value Is Displayed In A New Row HTML Table"