Php 使用mysql中的变量动态选择选项
在最终实现这一点后,我想我应该发布它,以防它可能Php 使用mysql中的变量动态选择选项,php,mysql,Php,Mysql,在最终实现这一点后,我想我应该发布它,以防它可能 我在填充几组下拉选择标签时遇到了问题。 我已经研究了许多类似的意见和解决方案,但我仍然找不到我一直在寻找的答案 @Ronser帮助我测试了我的查询,这使我进一步了解了数组的实际工作方式。我意识到我需要返回并将表1中的access列更新为access\u id。(我本来应该为这些建立索引的) 表1:app\u generalData 应用程序id, 标题, 状态标识, 类别id, 标签, 访问标识 表2:应用程序访问 访问id, 访问标题 所需结果
我在填充几组下拉选择标签时遇到了问题。
我已经研究了许多类似的意见和解决方案,但我仍然找不到我一直在寻找的答案 @Ronser帮助我测试了我的查询,这使我进一步了解了数组的实际工作方式。我意识到我需要返回并将表1中的access列更新为access\u id。(我本来应该为这些建立索引的) 表1:app\u generalData
应用程序id,
标题,
状态标识,
类别id,
标签,
访问标识 表2:应用程序访问
访问id,
访问标题 所需结果:
目标1:显示/回显所选选项(存储在应用访问表中) 目标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);
?>
Global\n公司\n本地站点\n
代码:<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);
?>
请尝试以下操作
检查“应用程序id”
打印sql查询并直接在mysql中运行
如果它没有返回行或错误,请验证sql查询。试试这个…
$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);
$options=“从“$column1.”按“$dropdown\u table\u id.”顺序选择“$column2.”;
$kk=0//初始化kk
while($row=mysqli\u fetch\u数组($options)){
$selected='';
如果($selected_option==$row[“$column2”]){
$selected='selected';
}
$optionsList[$kk++]='.$row[“$column2”].';
//试试这个变化。或者
echo'.$row[“$column2”].';//在这里自己打印
}
打印(选项列表);
检查打印($\u GET)代码>以验证数据流。。有错误吗?@Ronser我把打印($\u GET)放在直接在if(isset($\u-GET['app\u-id']){
之后编码。它生成了Array([app\u-id]=>1)
。对我来说很合适。print\r($\u-GET);
,print\r($\u-POST);
,print\r($\u-SESSION
通常用于顶部,以了解传递/初始化的元素是什么。请查看发布的更改并检查result@Ronser和@Bincy Kuriakose见上文
1.应用程序id打印了正确的数据。2.直接在mysql中测试的SQL查询-生成的第一个SQL查询从应用程序id所在的应用程序通用数据中选择访问=“1”
*mysql生成了corporate
,这是更正访问列的正确结果-生成的第二个sql查询“按访问id从app\u访问顺序中选择访问标题”*mysql查询生成:所需表的正确列我已在此片段中修补。打印结果为数组()
在尝试上面的代码后,它是一个空数组吗?
是的。你帮助我意识到我可以用print\r测试每一步,我回顾了一遍&…我想我发现了一些东西:我运行了一个print\r($selected\u选项);
它生成了mysqli\u结果对象([当前\u字段=>0[字段计数]=>1[长度]=>num\u行]=>1[type]=>0)
这似乎不对,所以我将mysqli\u fetch\u数组重新修饰为$row\u 1=mysqli\u fetch\u数组(**$selected\u option**);
print\r($row\u 1);`gived me数组([0]=>corporate[access]=>corporate)
。接近…我已将while
中的if
语句更改为if($row\u 1==
。仍然没有。希望您已接近…首先print\r($row);
了解值。您还可以使用echo”“;print\r($row);echo“”
为了对齐一个大数组…那么您是否尝试过上述解决方案?那么您的查询有问题,请先检查一下