PHP将数据回显到HTML下拉菜单中

时间:2013-12-17 23:51:15

标签: php html mysql

我有一个HTML等..标签现在我想要实现的是选择ie。我想将数据库中的相关信息加载到包含尽可能多标签的新标签中。

我现在正在使用PHP来实现这一点,例如我选择option1然后它后面的查询检索相关信息并将其存储在数组中,如果我选择option2完全相同的话。

我做的下一步是创建一个循环来显示来自array()的结果,但我正在努力想出正确的解决方案来将检索到的数据回显到等等。因为它不是我最强的一面。

希望你明白我想要实现的目标,下面的代码将会清除。

HTML:

<select id="workshop" name="workshop" onchange="return test();">
                <option value="">Please select a Workshop</option>
                <option value="Forex">Forex</option>
                <option value="BinaryOptions">Binary Options</option>
            </select>

PHP:

   $form = Array();

if(isset($_POST['workshop'])){

$form['workshop'] = $_POST['workshop'];
$form['forex'] = $_POST['Forex'];
$form['binary'] = $_POST['Binary'];


//Retrieve Binary Workshops 
if($form['workshop'] == 'Forex'){
    $sql2 = "SELECT id, course, location FROM courses WHERE course LIKE '%Forex%' OR  course LIKE '&forex%'";
        $query2 = mysqli_query($link, $sql2);
            while($result2 = mysqli_fetch_assoc($query2)){


               //The problem I am having is here :/ 

            echo "<select id='Forex' name='Forex' style='display: none'>";
            echo "<option value='oko'>.$result[1].</option>";
            echo "</select>";
            print_r($result2);echo '</br>';
        }
    }else{
        $sql = "SELECT id, course, location FROM courses WHERE course LIKE '%Binary%' OR course LIKE '%binary%'";
        $query = mysqli_query($link, $sql);
            while($result = mysqli_fetch_assoc($query)){
                print_r($result);echo '</br>';

        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这段代码:

$query2 = mysqli_query($link, $sql2);

echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
  echo "<option value='oko'>{$result['course']}</option>";

}
echo "</select>";
echo '</br>';

答案 1 :(得分:0)

从你的php顶部开始:

// not seeing uses of the $form I removed it from my answer

if(isset($_POST['workshop'])){

$workshop = $_POST['workshop'];
$lowerWorkshop = strtolower($workshop);
// neither of $_POST['Forex'] nor $_POST['Binary'] are defined in your POST. you could as well remove those lines?    

//Retrieve Binary Workshops HERE we can define the sql in a single line:
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%$workshop%' OR  course LIKE '&$lowerWorkhop%'";       
$query = mysqli_query($link, $sql); // here no need to have two results

// Here lets build our select first, we'll echo it later.
$select = '<select id="$workshop" name="$workshop" style="display: none">';
while($result = mysqli_fetch_assoc($query)){
    $select.= '<option value="' . $result['id'] . '">' . $result['course'] . '</option>';
// here I replaced the outer containing quotes around the html by single quotes.
// since you use _fetch_assoc the resulting array will have stroing keys. 
// to retrieve them, you have to use quotes around the key, hence the change
    }
$select.= '</select>';
}
echo $vSelect;

这将为任一查询返回的每一行输出一个包含一个选项的选项。顺便说一句,这个特殊的例子不会在屏幕上回显任何东西(因为你的选择显示设置为无)。但请参阅源代码以检索示例。