0

我有这样的mysql设置

MySQL

表:画廊

+-------------+------------+
| galleryname | coverphoto |
+-------------+------------+

表:多画廊

+-------------+-----------+
| galleryname | galleries |
+-------------+-----------+

我正在使用 php 中的复选框数组将画廊放入multigalleries.gallerieslike gallery1,gallery2,gallery3. 所有这一切都很好,我唯一想不通的是,当我拉出列表gallery.galleryname并用它引用它时multigalleries.galleries,如果它存在于,我如何返回检查状态multigalleries.galleries

任何帮助将不胜感激。

编辑// IM使用的实际代码,如果有帮助的话

$con = mysql_connect($host,$dbusername,$dbpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($databasename, $con);
$query = "SELECT galleries.gallery, galleries.coverphoto, multigalleries.multigallery,            multigalleries.galleries FROM galleries, multigalleries";
$result = mysql_query($query);

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<div class="gallery" style="background-image:none">';
echo '<input type="checkbox" name="galleriesm[]" value="'.$line[gallery].'">'.      $line[gallery].'';
echo '</div>';
4

1 回答 1

2

尝试这个:

$a = mysql_query("
    SELECT g.galleryname FROM gallery AS g, multigalleries AS m
    WHERE g.galleryname = 'gallery1' AND FIND_IN_SET(g.galleryname, m.galleries)
");

$b = mysql_fetch_assoc($a);

echo '<input type="checkbox" name="whatever"';

if($b['galleryname'])
{
    echo ' checked="checked"';
}

echo '>';

编辑:

$result = mysql_query("
    SELECT g.galleryname, m.galleryname AS name FROM gallery AS g 
    LEFT JOIN (SELECT galleryname, galleries FROM multigalleries) AS m ON 
    FIND_IN_SET(g.galleryname, m.galleries)
");

while($row = mysql_fetch_assoc($result))
{
    $checked = '';

    if($row['name'])
    {
        $checked = ' checked="checked"';
    }

    echo '<input type="checkbox" name="galleriesm[]" id="'.$row['galleryname'].'" value="'.$row['galleryname'].'"'.$checked.'>
    <label for="'.$row['galleryname'].'">'.$row['galleryname'].'</label>';
}
于 2012-08-23T20:12:12.740 回答