在线阅读和查找后,我仍然不知道如何解决我的问题。而我无法做到这一点:
$select=mysql_query("select * from products_list categories where id='3'");
while($row=mysql_fetch_array($select)){
echo "whatever fields that I want to display onto the form before category field";
$select=mysql_query("select * from categories ORDER BY id ASC");
while($row=mysql_fetch_array($select)){
echo "compare with the result in categories table and select the option if it's true";
}
}
我是对的吗?
目前,我在数据库产品列表和类别中有2个表。
表products_list包含许多字段: ID,代码,价格,类别,状态等......
表类别只有: ID,类别(此表仅包含所有可用的类别选项集)
我有一个更新/编辑表单,我使用select语句并从Table products_list中获取用户先前保存所有数据的所有结果。
在表单本身的类别部分下面是一个多选列表框。
我现在的问题是:
我真的需要很多帮助,因为我以前从未这样做过,也不知道在线阅读这些文章或主题是什么。希望你们理解我的问题。提前谢谢。
目前更新的编码但仍然存在:
$result2= mysql_query("SELECT category FROM categories ORDER BY id ");
$all_cat = array();
while($row2 = mysql_fetch_assoc($result2)) {
$all_cat[] = $row2['category'];
}
//dropdownlist query
$query="SELECT * from products_list where id='100'";
$result = mysql_query ($query);
//populate the dropdownlist
while($row = mysql_fetch_assoc($result)) {
$sel_cats = explode(",", $row['category']);
//determine if the category is selected
foreach ($sel_cats as $sel_cat){
$selected = '';
if ($sel_cat == $all_cat ){
$selected =' selected="selected" ';
break;
}
}
foreach($all_cat as $i => $value)
{
echo '<option value="' . $value. '"' . $selected . '>'.$value.'</option>\n';
}
}
答案 0 :(得分:1)
// get all product, assuming the saved category is cat column in product_list table
$result2= mysql_query("SELECT * from products_list where id='3'");
$row2 = mysql_fetch_assoc($result2);
$sel_cat =$row2['cat'];
//dropdownlist query
$query="SELECT cat,id FROM categories ORDER BY id ";
$result = mysql_query ($query);
//populate the dropdownlist
while($row = mysql_fetch_assoc($result)) {
//determine if the category is selected
$selected = ($row['id '] == $sel_cat ) ? ' selected="selected" ' : NULL;
echo '<option value="' . $row['id']. '"' . $selected . '>'.$row['cat'].'</option>\n';
}
对于以串联字符串格式保存的类别。例如:黑色,白色,红色
// get all product, assuming the saved category is cat column in product_list table
$result2= mysql_query("SELECT * from products_list where id='3'");
$row2 = mysql_fetch_assoc($result2);
$sel_cats = explode(',', $row2['cat']);
//dropdownlist query
$query="SELECT cat,id FROM categories ORDER BY id ";
$result = mysql_query ($query);
//populate the dropdownlist
echo '<select name="cat" size="5" multiple="multiple">';
while($row = mysql_fetch_assoc($result)) {
//loop thru the sel_cat
foreach ($sel_cats as $sel_cat){
//determine if the category is selected
$selected = '';
if ($row['id '] == $sel_cat ){
$selected =' selected="selected" ';
break;
}
}
echo '<option value="' . $row['id']. '"' . $selected . '>'.$row['cat'].'</option>\n';
}
echo '</select>';
答案 1 :(得分:0)
使用join sql查询 喜欢 例如,
select * from products_list, categories where product_list.id=categories.id ORDER BY id ASC
select * from products_list where id=1 ORDER BY id ASC LIMIT 20
答案 2 :(得分:0)
我真的不明白你的问题,但我认为这是一个简单的Read语句。
接下来该做什么完全取决于你。
答案 3 :(得分:0)
$select=mysql_query(" select * from products_list where id='1' ORDER BY id ASC LIMIT 20 ");
while($row=mysql_fetch_array($select)){
$id = $row["id"];
echo $id;
}