0
0

プルダウンを使ったarray_filter関数のサンプル

配列定義


// フルーツ種類定義 
define("APPLE",  1);
define("BANANA", 2);
define("ORANGR", 3);
define("CHERRY", 4);
define("GRAPE",  5);


// フルーツ配列
define(
	'FRUIT',
	array(
		APPLE  => 'りんご',
		BANANA => 'バナナ',
        ORANGR => 'オレンジ',
        CHERRY => 'さくらんぼ',
        GRAPE  => 'ぶどう',
	)
);

array_filterで範囲を指定

バナナまでの表示に設定

// リンゴからバナナに指定
$filtered_furuit = array_filter(
    FRUIT,
    function ($key) {
        return $key <= ORANGR;
    },
    ARRAY_FILTER_USE_KEY
);

表示

<th>フルーツ</th>
<td>
    <select name="furuit" class="chosen-select">
    <option value="">全て</option>
        <?php foreach ($filtered_furuit as $key => $val) { ?>
            <option value="<?php echo $key; ?>"<?php echo (isset($_POST['furuit']) && $_POST['furuit'] == $key) ? 'selected' : ''; ?>><?php echo $val; ?></option>
        <?php } ?>
    </select>
</td>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0