我当前正在开发一个应用程序,该应用程序允许用户从下拉菜单中选择一个选项,并基于该选项列出表数据,并动态地进行操作。我正在考虑使用搜索栏,允许用户搜索国家名称(在一个大陆内)并在搜索时列出该国家名称。让我感到困惑的部分是,我可以这样做吗,以便用户可以从下拉列表中选择一个大洲,然后在搜索栏中输入国家/地区名称,然后单击相同的按钮以接收列表?
我了解选择相关代码的重要性,但是我认为对于读者来说,同时看到我的主页和操作页面以了解我要实现的目标非常重要。
这是索引代码:
require_once('repeat_code.php');
$db = dbConn();
//Make an SQL statement
$sqlContinents = "SELECT DISTINCT ID as contID, Name as contName from w_Continent order by contName;";
//Execute SQL statement
$stmt = $db->query($sqlContinents);
//Start a form
echo "<form action='listCont.php' method='get'>\n";
//Start a select box
echo "<select name='contID'>\n";
//Loop through all continents
while ($continent = $stmt->fetchObject()) {
//Display each one as an option in the dropdown
echo "\t\t<option value='{$continent->contID}'> {$continent->contName} </option>\n";
}//end loop
// end select box
echo "</select>\n";
// display submit button
echo "<input type='submit' value='Find Country' />\n";
// end form
echo "</form>\n";
这是操作页面:
if(!empty($contID)) {
//Connect to the database
require_once('repeat_code.php');
$db = dbConn();
//Create SQL statement using ID
$sqlCountries = "SELECT w_Country.Name, w_Continent.Name as 'contName', w_Country.Region as 'regionname', w_Country.HeadOfState, w_Country.Capital
FROM w_Continent JOIN w_Country on w_Continent.ID = w_Country.Continent
WHERE w_Continent.ID = '$contID'";
//Execute statement to get a record set back
$stmt = $db->query($sqlCountries);
// Start a table
echo "<table border='1'>\n";
// Start a header row
echo "<tr><th>Country</th><th>Continent</th>\n";
//Loop through the record set
while ($continent = $stmt->fetchObject()) {
//Display each student in a row
echo "\t<tr><td>{$continent->Name}</td><td>$continent->contName</td>\n";
}//End loop
//end the table
echo "</table>";
感谢您的帮助。
答案 0 :(得分:0)
在一行中创建一个下拉菜单和输入框。
下拉菜单包含大陆,输入框为空。
在数据库中,以以下格式创建2个表。
大陆表
id | Continent_name
国家/地区表
id | country_name | continent_id
现在在第一个表中运行查询,并在下拉列表中显示所有大洲。
现在,当用户从下拉菜单中选择大洲名称并在搜索框中输入国家名称时,您就必须像这样进行查询。
<form method="post">
<input type="hidden" name="command" value="search">
<select name="select_box">
<option value ="1">Asia</option>
<option value ="2">Africa</option>
</select>
<input type ="text" name="search_box" placeholder="Type Country">
</form>
现在运行如下所示的PHP代码。
if($_REQUEST['command']=='search'){
$continent_id = $_REQUEST['select_box']; //suppose user select asia
$country_name = $_REQUEST['search_box']; // suppose user type "Pakistan"
//now execute below query in countries table.
$query ="select * from countries where continent_id='$continent_id' and country_name like '%$country_name%'";
}