连接mysql表以显示一个表字段,但根据类似字段的值选择另一个表字段 [英] join mysql tables to show one tables field but select another tables field based on the value of similar fields

查看:75
本文介绍了连接mysql表以显示一个表字段,但根据类似字段的值选择另一个表字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表格(与我的问题相关)

类别和产品



字段加入

类别 - 短,图片

产品 - 简短,图片



短=类别名称简称

image =图片名称





基本上我想根据产品中已经设置的图片选择一个类别的图像(所以当我选择我的下拉列表应该只有为该类别的产品设置的图像)



我的代码下面有两个问题 - 一个没有显示已经选中的类别中的字段和两个列出所有图像,而不仅仅是短字段值匹配的位置



代码:

<?php 
$ qry = mysql_query(SELECT * FROM product,$ con);
if(!$ qry)
{
die(Query Failed:。mysql_error());
}
?>


选择类别图像
< select name =imageid =image>
< option value =>< / option>
<?php
//首先,定义所需的默认值
$ default_value = $ row ['image'];
while($ row = mysql_fetch_array($ qry))
{
//然后你可以在你的while循环中选择那个选择
$ selected =($ row ['image'] == $ default_value)? '选中':'';
echo< option value ='。 $ row ['image']。 '。 $ selected。 > 中。 $ row ['image']。 < /选项> 中;
}
?>
< / select>

解决方案

qry = mysql_query(SELECT * FROM product,


CON);
if(!


qry)
{
die(Query Failed:。mysql_error());
}
?>


选择类别图像
< select name =imageid =image>
< option value =>< / option>
<?php
//首先,定义所需的默认值

i have the following tables (relevant to my question)
category and product

fields to join
category - short, image
product - short, image

short = short form of category name
image = image name


basically i want to select an image for category based on the images already set in product (so when i select my drop down should only have the images that were set for products for that category)

my code below has two problems - one it doesn't show the already selected field in category and two it lists all the images and not just where the short field value match

code:

<?php
$qry=mysql_query("SELECT * FROM product", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
?>


Select Category Image
<select name="image" id="image">
    <option value=""></option>
    <?php
    // First, define the desired default value
    $default_value = $row['image'];
    while($row=mysql_fetch_array($qry))
    {
        // Then you can mark that one as selected in your "while" loop
        $selected = ($row['image'] == $default_value) ? ' selected' : '';
        echo "<option value='" . $row['image'] . "'" . $selected . ">" . $row['image'] . "</option>";
    }
    ?>
</select>

解决方案

qry=mysql_query("SELECT * FROM product",


con); if(!


qry) { die("Query Failed: ". mysql_error()); } ?> Select Category Image <select name="image" id="image"> <option value=""></option> <?php // First, define the desired default value


这篇关于连接mysql表以显示一个表字段,但根据类似字段的值选择另一个表字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆