[英]PHP: creating select options from MYSQL database?
I'm trying to create multiple select dropdowns from MYSQL database! 我正在尝试从MYSQL数据库创建多个选择下拉列表!
basically I need to create select dropdowns based on the given sub-categories and put every op_value
related to that sub_cate_name
under the created dropdown menu as options. 基本上我需要创建基于给定的子类别选择下拉列表,并把每
op_value
与该sub_cate_name
创建的下拉菜单中作为选项下。
at the moment, my code creates the dropdowns and select options BUT it will put them in separate select dropdwons and all over the place. 目前,我的代码创建了下拉菜单并选择了选项,但是它将把它们放置在单独的选择下拉菜单中,并且遍布整个地方。
For example: if we have 2 sub_cate_name
as Apples
and 3 op_value
as Reds, Greens, Yellows
it will create 2 select dropdown menus and split them op_value
which is (Reds, Greens, Yellows) as select options where ever it likes! 例如:如果我们有2个
sub_cate_name
作为Apples
,3个op_value
作为Reds, Greens, Yellows
和op_value
Reds, Greens, Yellows
它将创建2个select下拉菜单,并将op_value
拆分为(Reds,Greens,Yellows)作为op_value
选择的选择选项!
but i need it to make sure it creates 1 select dropdown as Apples and put ever op_value related to apples under the Apples dropdown as option! 但是我需要它来确保它创建1个选择下拉菜单作为Apples,并将与苹果相关的op_value放在Apples下拉菜单下作为选项!
This is code seems to have mind of its own as it works sometimes and it doesn't others! 这是代码似乎有自己的想法,因为有时它起作用,而其他人则没有!
this is the entire code: 这是完整的代码:
$drops ="";
$sql44 ="SELECT * FROM drop_options WHERE sub_cat_name='$currentproduct'";
$query44 = mysqli_query($db_conx, $sql44);
$productCount44 = mysqli_num_rows($query44);
if ($productCount44 > 0)
{
$op_name = '';
$first = 0;
while($row44 = mysqli_fetch_array($query44, MYSQLI_ASSOC))
{
$op_value = $row44["op_value"];
if($op_name != $row44["op_name"])
{
if($first)
{
$drops .='</select></div>';
}
$op_name = $row44["op_name"];
$drops .='<div class="col-md-3 col-xs-12">
<p class="margin-bottom-zero">'.$op_name.'</p>
<select name="keyword[]" class="selectpicker">
<option value="'.$op_value.'">'.$op_value.'</option>';
}
else
{
$drops .= '<option value="'.$op_value.'">'.$op_value.'</option>';
}
/*$first = 1;*/
$first++;
}
}
else
{
$drops ='';
}
$drops .='</select></div>';
echo $drops;
Could someone please advise on this issue? 有人可以建议这个问题吗?
any help would be appreciated. 任何帮助,将不胜感激。
EDIT: 编辑:
Okay, now i tried it this way and I can get the exact amount of the select drop downs created as it should: 好的,现在我以这种方式进行了尝试,我可以准确地获得创建的选择下拉列表的确切数量:
if($db_conx->connect_errno > 0){
die('Unable to connect to database [' . $db_conx->connect_error . ']');
};
// Perform queries
$sql45 = "SELECT DISTINCT op_name FROM drop_options WHERE sub_cat_name='Custom Booklets";
$result45 = $db_conx->query($sql45);
//echo '<li class="product-types">';
foreach ($result45 as $menu45)
{
// echo "<li>".$menu["cat_name"];
echo '<div class="col-md-3 col-xs-12">
<p class="margin-bottom-zero">'.$menu45["op_name"].'</p>
<select name="keyword[]" class="selectpicker">';
//echo "<ul>";
$menu_title45 = $menu45["sub_cat_name"];
$sql455 = "SELECT * FROM drop_options WHERE op_name='".$menu45["op_name"]."'";
$result455 = $db_conx->query($sq455);
foreach ($result455 as $submenu455)
{
//echo "<li>".$submenu["sub_cat_name"]."</li>";
echo '<option value="'.$submenu455["op_value"].'">'.$submenu455["op_value"].'</option>';
}
echo "</select>";
echo "</div>";
}
//echo "</ul>";
$db_conx->close();
BUT i get no select options created at all Also i get this error: Warning: Invalid argument supplied for foreach() in line 18
但是我根本没有创建任何选择选项。我也收到此错误:
Warning: Invalid argument supplied for foreach() in line 18
and this is on line 18 : foreach ($result45 as $menu45){
这是第18行:
foreach ($result45 as $menu45){
any idea what i'm doing wrong? 知道我在做什么错吗?
This is how you're code should look like- 这就是您的代码应该看起来像-
// Perform queries
$sql45 = "SELECT DISTINCT op_name FROM drop_options WHERE sub_cat_name='Custom Booklets'";
$result45 = $db_conx->query($sql45);
//echo '<li class="product-types">';
while ($menu45 = mysqli_fetch_array($result45,MYSQLI_ASSOC))
{
// echo "<li>".$menu["cat_name"];
echo '<div class="col-md-3 col-xs-12">
<p class="margin-bottom-zero">'.$menu45["op_name"].'</p>
<select name="keyword[]" class="selectpicker">';
//echo "<ul>";
$menu_title45 = $menu45["sub_cat_name"];
$sql455 = "SELECT * FROM drop_options WHERE op_name='".$menu45["op_name"]."'";
$result455 = $db_conx->query($sql455);
while($submenu455 = mysqli_fetch_array($result455,MYSQLI_ASSOC))
{
//echo "<li>".$submenu["sub_cat_name"]."</li>";
echo '<option value="'.$submenu455["op_value"].'">'.$submenu455["op_value"].'</option>';
}
echo "</select>";
echo "</div>";
}
//echo "</ul>";
$db_conx->close();
You may want to add if
condition to check whether there are actually select options so you wont end up with an empty select. 您可能需要添加
if
条件,以检查是否确实存在选择选项,因此最终不会出现空选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.