我确信对于大多数了解 PHP 和 mysql 的人来说这很容易弄清楚,但我无法弄清楚。我会尽力以最好的方式解释
如果我错过了什么,请告诉我
相关表结构
Table | Fields category | id,group_n (more fields exist but are not required now) p_group | id,group_n
what I want to achieve:
Query to find currently selected group_n field in category table if available based on the current _isset id taken from the _get URL in relation to the category.
Example of work flow :
sample rows for category
ID | category | short | group_n 1 | Skin Whitening Pills | pills | Skin Whitening 2 | Skin Whitening Cream | cream | Skin Whitening 3 | Weight Gain / Loss | wgainl | Weight Gain / Loss
As you can see multiple categories can have a single group_n. I use the group_n to show an expert for that group.
- select option is populated with the field group_n from category
- select options are populated with all options from p_group (except selected so there is no duplicate"
category with id 2 has group_n as Skin Whitening edit_category.php?id=2 would display all input options for for id 2 that are currently in the table row in order to edit them and update the query. Now when the select options for the group_n are populated the selected should be Skin Whitening but all rows in p_group should be available to select.
code
<?php
$qry=mysql_query("SELECT group_n from category where id=$id", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
?>
<select name="group" id="group">
<?php
// First, define the desired default value
$default_value = $row['group_n'];
while($row=mysql_fetch_array($qry))
{
// Then you can mark that one as selected in your "while" loop
$selected = ($row['group_n'] == $default_value) ? ' selected' : '';
$qry2=mysql_query("SELECT * from p_group", $con);
while ($row2 = mysql_fetch_array($qry2))
{
echo "<option value='" . $row2['group_n'] . "'" . $selected . ">" . $row2['group_n'] . "</option>";
}
}
?>
</select>
我还尝试使用单个查询左连接两个表,但以下查询将仅列出一行,该行不会自动填充,但会给出正确的值。
$qry=mysql_query("
SELECT a.group_n,
b.group_n from p_group a
left join category b
on a.group_n=b.group_n
group by b.group_n where b.id=$id", $con);
我不确定我的查询或 php 是否需要更改,但我一次只能让其中一个查询与选择一起使用。我需要知道如何使两个查询都正常工作,因此选择的选项是类别表中的一个和 p_group 表中的下拉列表。
理想情况下,我希望类别表与 p_group 中的 id 相关联,但现在我愿意将其保留为文本。
最佳答案
在等待本周异常安静的用户社区的输入后,我四处搜索并摆弄代码,直到找到 UNION sql 连接。这基本上需要两组数据并呈现为一组,从而达到了目的。不确定这是否是最好的方法,也许有一个纯 PHP 的方法,但这确实成功了。
SQL 片段
$qry=mysql_query("select group_n as group_a from category where id=$id union select group_n from p_group group by group_n", $con);
基本上,这会选择左侧的查询并将其与右侧的查询结合起来并显示所有记录。
限制 1.选择的字段数必须匹配 2. UNION不会选择重复项 3. UNION ALL 将选择重复项
完整的相关代码:
$qry=mysql_query("select group_n as group_a from category where id=$id union select group_n from p_group group by group_n", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
// First, define the desired default value
$default_value = $row['group_a'];
while($row=mysql_fetch_array($qry))
{
$selected = ($row['group_a'] == $default_value) ? ' selected' : '';
// Then you can mark that one as selected in your "while" loop
echo "<option value='" . $row['group_a'] . "'" . $selected . ">" . $row['group_a'] . "</option>";
}
?>
</select>
关于php - 使用来自两个 mysql 查询的数据填充选择下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28266833/