简体   繁体   中英

PHP Planning board table

I want to make an planning board in PHP, I want it to look like this (tables):

Name1 X
Name2
Name3
Name4 X
Monday | Tuesday | Wednesday

I have 2 foreaches, 1 for the names and 1 for the data.

foreach($names) {
    foreach($data) {
        <tr>
        <td> $names </td>
        <td> if data['data'] == 1 { X }</td>
        <td> if data['data'] == 2 { X }</td>
        <td> if data['data'] == 3 { X }</td>
         </tr>
    }
}

But this code duplicates the names. When I put the tr and the td $names in the other foreach it creates more fields than 4. How can I combine these foreaches?

I hope someone can help me.

Not sure what your arrays contain, but for starters, you're outputting all the names values, should be something like this.

Now that we have the arrays

$data = array(array("id" => 1, "data" => 0), array("id" => 2, "data" => 1))

Your code wants to be something like..

<?php
    foreach($names as $key => $value) {
        $d = $data[$key]['data']; ?>
        <tr>
        <td> <?php echo $value; ?> </td>
        <td> <?php if ($d == 1) { echo 'X'; } ?></td>
        <td> <?php if ($d == 2) { echo 'X'; } ?></td>
        <td> <?php if ($d == 3) { echo 'X'; } ?></td>
        </tr><?php
    }

?>

I think in this instance you'll need the 2 foreach loops, nested recursion isn't a bad thing unless you've nested things several levels deep and it's hard to debug.

What you probably need is something like this:

//each row is a name
foreach($names as $name){
    echo "<tr>";//<-- new row
    $x = 1;//counter
    //each data is a column
    foreach($data as $d){
        echo "<td>";//<-- new column
        if($d == $x){// is $d equal to 1,2,3,4,[n]
            echo "X";
        }
        echo "</td>";
        $x++;//increment counter
    }
    echo "</tr>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM