最后让它发挥作用后,我想我会发布它以防万一 我在填充几组下拉选择标签时遇到了麻烦 我研究了许多类似的提交和解决方案,但我仍然无法找到我一直在寻找的答案。
@Ronser帮助我测试了我的查询,这让我更多地了解了数组的实际工作方式。我意识到我需要返回并将表1 中的访问列更新为 access_id 。 (我应该将这些原始索引)。
表1: app_generalData
APP_ID,
标题,
STATUS_ID,
CATEGORY_ID,
标签,
access_id
表2: app_access
access_id,
access_title
期望的结果:
目标1:显示/回显所选选项(存储在 app_access 表中)
目标2:
使用变量构建这些查询,以便轻松更新以添加新的下拉列表。
结果HTML:
<select name="access"><option "">Global</option>\n<option " selected ">Corporate</option>\n<option "">Local Site</option>\n</select>
代码:
<?php
//connect to the database
require_once('connectvars.php');
// global variable for this populating this dropdown
$dropdown = "access";
$before_var = "app_";
$column1= $before_var.$dropdown;
$after_var = "_title";
$column2= $dropdown.$after_var;
$id_var= "_id";
$dropdown_table_id= $dropdown.$id_var;
$optionsList = array();
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die ('Error connecting to MySQL server.');
echo '<select name="' . $dropdown . '">';
// See if we're viewing a selected app or are we creating a new app
if (isset($_GET['app_id'])) {
// print_r($_GET); // GET is Successful
// 'if' [app_id] is appended in the url
// STEP 1: Get the stored value of the SELECTED from mysql
// Get "selected item" (id) from app_generalData (table) with the "selected app" (id)
$option = "SELECT ".$dropdown_table_id." FROM app_generalData WHERE app_id ='" . $_GET['app_id'] . "'";
// submit the select statement
// Get & store the value of "selected" <option>
$selected_option = mysqli_query($dbc, $option)
or die(mysql_error());
$row_1=mysqli_fetch_array($selected_option);
// STEP 2: Build the SELECT input
// Set the array of data to populate dropdown list <option>s
$options = "SELECT * FROM ".$column1." ORDER BY ".$dropdown_table_id."";
// NOTE: print_r($options)...SELECT access_title FROM app_access ORDER BY access_id
$selected_options = mysqli_query($dbc, $options)
or die(mysqli_error());
$kk = 0; //initialize kk
while($row_2 = mysqli_fetch_array($selected_options)) {
$selected ='';
if($row_1["$dropdown_table_id"]==$row_2["$dropdown_table_id"]) {
$selected=' selected ';
}
$optionsList[$kk++] ='<option "' . $selected . '">' . $row_2["$column2"] . '</option>';
}
// Echo the <option>s
$optionCount = count($optionsList);
for($i=0;$i<$optionCount;$i++) {
echo $optionsList[$i].'\n';
}
}
else {
// Action 'if' no [app_id] is appended in the url
};
// close the last <select> tag
echo '</select>';
// close the last database
mysqli_close($dbc);
?>
答案 0 :(得分:1)
请尝试以下方法......
检查&#39; app_id&#39;。
打印sql查询并直接在mysql中运行。
如果没有返回任何行或错误,请验证sql查询。
答案 1 :(得分:1)
试试这个......
$options = "SELECT ".$column2." FROM ".$column1." ORDER BY ".$dropdown_table_id."";
$kk = 0; //initialize kk
while($row = mysqli_fetch_array($options)) {
$selected ='';
if($selected_option==$row["$column2"]) {
$selected=' selected ';
}
$optionsList[$kk++] ='<option "' . $selected . '">' . $row["$column2"] . '</option>';
//try this change. or
echo '<option "' . $selected . '">' . $row["$column2"] . '</option>'; //print it here itself
}
print_r($optionsList);
答案 2 :(得分:0)
<?php
//connect to the database
require_once('connectvars.php');
// global variable for this populating this dropdown
$dropdown = "access";
$before_var = "app_";
$column1= $before_var.$dropdown;
$after_var = "_title";
$column2= $dropdown.$after_var;
$id_var= "_id";
$dropdown_table_id= $dropdown.$id_var;
$optionsList = array();
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die ('Error connecting to MySQL server.');
echo '<select name="' . $dropdown . '">';
// See if we're viewing a selected app or are we creating a new app
if (isset($_GET['app_id'])) {
// print_r($_GET); // GET is Successful
// 'if' [app_id] is appended in the url
// STEP 1: Get the stored value of the SELECTED from mysql
// Get "selected item" (id) from app_generalData (table) with the "selected app" (id)
$option = "SELECT ".$dropdown_table_id." FROM app_generalData WHERE app_id ='" . $_GET['app_id'] . "'";
// submit the select statement
// Get & store the value of "selected" <option>
$selected_option = mysqli_query($dbc, $option)
or die(mysql_error());
$row_1=mysqli_fetch_array($selected_option);
// STEP 2: Build the SELECT "selected" and <options>
// Set the array of data to populate dropdown list <option>s
$options = "SELECT * FROM ".$column1." ORDER BY ".$dropdown_table_id."";
// NOTE: print_r($options)...SELECT access_title FROM app_access ORDER BY access_id
$selected_options = mysqli_query($dbc, $options)
or die(mysqli_error());
$kk = 0; //initialize kk
while($row_2 = mysqli_fetch_array($selected_options)) {
$selected ='';
// Compare access_id from table1 against access_id from table2
if($row_1["$dropdown_table_id"]==$row_2["$dropdown_table_id"]) {
$selected=' selected ';
}
$optionsList[$kk++] ='<option "' . $selected . '">' . $row_2["$column2"] . '</option>';
}
// Echo the <option>s
$optionCount = count($optionsList);
for($i=0;$i<$optionCount;$i++) {
echo $optionsList[$i].'\n';
}
}
else {
// Action 'if' no [app_id] is appended in the url
};
// close the last <select> tag
echo '</select>';
// close the last database
mysqli_close($dbc);
?>