0

I have a form with a dropdown selection whose option are determined from a table on a database. Periodically I will (manually) add more options to the dropdown and the database. Either I can make the dropdown selection get the options from the database like this:

<select class="form-control" name="category" id="category">

    <?php
    $sql = "SELECT id, category FROM categories;";
    $result = $GLOBALS['conn']->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $category = $row['category'];
            $id = $row['id'];
            echo "<option value='$id'>$category</option>";
        }
    }
    ?>
</select>

Which will give me less work because all I have to update now is the database and the form will update itself.

Or I can manually input the options into the form like this:

<select class="form-control" name="category" id="category">
    <option value='1'>option1</option>
    <option value='2'>option2</option>
    <option value='3'>option3</option>
</select>

This way will require more manual work but has the advantage of not having to connect to the database each time (less work for the servers).

My question is which method should I use? The method that gives me more work or the method that gives the server more work?

4

1 回答 1

0

有赞成和反对使用数据库进行选择的论据。至于 pro 参数,它相对容易更新 - 您只需插入一个新值,它就会出现在您的表单中。这种方法的问题在于,您的应用程序将在您每次呈现表单时进行查询。在拥有数百万次点击的网站中,这不是一个好主意。因此,使用的替代方法是将查询结果缓存在某个中间层中,尤其是当这些值没有太大变化时。根据我的经验,处理这些值的最佳方法是从配置文件中加载它们。这些是很少更改的值,可以在那里添加新值。这些仍然可以被缓存,并且不需要数据库命中。

于 2016-08-07T22:37:14.487 回答