10c05-2.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 59 行

PHP
59
字号
<?php// If this file was a submission, then output the valuesif (count($_POST)) {    echo "<p>You submitted the following music ID numbers:<br />1st - {$_POST['1st']}, 2nd - {$_POST['2nd']}, 3rd - {$_POST['3rd']}</p>";}// Create a function that will take an ID, and an array of ID's & Desc's as // parameters, and will output the options of a select list with the// proper one highlighted.function create_option_list($id, $data) {    // The blank option - automatically selected if nothing else is    $options = "<option value=\"\">Select one ...</option>\n";        // Now loop over all possibilities, adding them in:    foreach ($data as $x) {        // Determine if this one should be selected        $sel = ($id == $x['id']) ? ' selected' : '';                // Now add in the option        $options .=             "<option value=\"{$x['id']}\"{$sel}>{$x['desc']}</option>\n";    }    // Now return the option list    return $options;}// Array of options in a format that might have come from a database query:$types = array(    array('id' => 10, 'desc' => 'Rock'),     array('id' => 11, 'desc' => 'Alternative'),    array('id' => 12, 'desc' => 'Metal'),     array('id' => 20, 'desc' => 'Classical'),    array('id' => 33, 'desc' => 'Jazz'),     array('id' => 34, 'desc' => 'Blues'),    array('id' => 42, 'desc' => 'Ska'),     array('id' => 44, 'desc' => 'Folk'),    array('id' => 49, 'desc' => 'Blue Grass')    );// And now we can output our page, using this multiple times:// We will use an @ in front of the $_POST references in order to suppress//  the PHP notice that will occur due to it not being initialized at first.?><form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="f1"><p>Please choose your favorite types of music:</p><p>1st: <select name="1st"><?= create_option_list(@$_POST['1st'], $types) ?></select></p><p>2nd: <select name="2nd"><?= create_option_list(@$_POST['2nd'], $types) ?></select></p><p>3rd: <select name="3rd"><?= create_option_list(@$_POST['3rd'], $types) ?></select></p><p><input type="submit" /></p></form>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?