Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

php - 如何制作动态表格(How to make dynamic table)

I want to make a dynamic table

(我想做一个动态表)

$kriteria = array();
                foreach ($data['kriteria'] as $key => $val) {
                    $kriteria[$key] = $val['nama'];
                }
                ?>
                <thead>
                    <tr>
                        <th>Kriteria</th>
                        <?php
                        foreach ($kriteria as $val) {
                            echo '<th>' . $val . '</th>';
                        }
                        ?>
                    </tr>
                </thead>
                <tbody>
                    <?php $n = count($kriteria); ?>
                    <?php
                    for ($i = 0; $i < $n; $i++) {
                        echo '<tr>';
                        echo '<th>' . $kriteria[$i] . '</th>';

                        // echo '<div cla
                        for ($j = 0; $j < $n; $j++) {
                            // echo '<td>' . $matrix[$i][$j] . '</td>';

                            echo '<td><input type="number" min="1" max="9" class="form-control" id="$i $j" name="$i $j"></td>';
                        }
                        echo '</div>';
                        echo '</tr>';
                    }


                    ?>

                </tbody>
            </table>

i want the table like this

(我想要这样的桌子) 在此处输入图片说明

the blue side table value 1/the ride side table value and the green side value is 1 table row and column length is from value from the database

(蓝色边表值为1 /乘车边表值,绿色边值为1,表行和列长度来自数据库中的值)

  ask by Diyona Amelia translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I hope that is what you looking:

(我希望那是你的期待:)

         <table>
                <?php
                $data['kriteria'] = array('IPK', 'SEMESTER', 'PENGHASILAN', 'BEASISWA LAIN');
                $kriteria = array();
                foreach ($data['kriteria'] as $key => $val) {
                    $kriteria[$key] = $val['nama'];
                }
                ?>
            <thead>
                <tr>
                    <th>Kriteria</th>
                    <?php
                    foreach ($kriteria as $val) {
                        echo '<th>' . $val . '</th>';
                    }
                    ?>
                </tr>
            </thead>
            <tbody>
                <?php $n = count($kriteria); ?>
                <?php for ($i = 0; $i < $n; $i++): ?>
                <tr>
                    <th><?= $kriteria[$i] ?></th>
                    <?php for ($j = 0; $j < $n; $j++): ?>
                        <td><input type="number" min="1" max="9" class="form-control" id="<?= $kriteria[$j] . $kriteria[$i] ?>" name="<?= $j. $i ?>"></td>
                    <?php endfor; ?>
                </tr>
                <?php endfor; ?>
            </tbody>
        </table>

Result:

(结果:)

<table>
                            <thead>
                <tr>
                    <th>Kriteria</th>
                    <th>I</th><th>S</th><th>P</th><th>B</th>                </tr>
            </thead>
            <tbody>
                                                <tr>
                    <th>I</th>
                                            <td><input type="number" min="1" max="9" class="form-control" id="II" name="00"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="SI" name="10"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="PI" name="20"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="BI" name="30"></td>
                                    </tr>
                                <tr>
                    <th>S</th>
                                            <td><input type="number" min="1" max="9" class="form-control" id="IS" name="01"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="SS" name="11"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="PS" name="21"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="BS" name="31"></td>
                                    </tr>
                                <tr>
                    <th>P</th>
                                            <td><input type="number" min="1" max="9" class="form-control" id="IP" name="02"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="SP" name="12"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="PP" name="22"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="BP" name="32"></td>
                                    </tr>
                                <tr>
                    <th>B</th>
                                            <td><input type="number" min="1" max="9" class="form-control" id="IB" name="03"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="SB" name="13"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="PB" name="23"></td>
                                            <td><input type="number" min="1" max="9" class="form-control" id="BB" name="33"></td>
                                    </tr>
                            </tbody>
        </table>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...