嗨,我有2个表,一个叫做标签,带有一堆名称,另一个是img,带有2个列,一个是与表标签的标记名匹配的标签,另一个是与图像链接匹配的img,所以我想做的是从其他表格标签中提取具有相应标签的所有图像...但是我仍然没有弄清楚如何设置哪些标签...到目前为止这是我的尝试...谢谢!
<?PHP
include('connect.php');
$options=mysql_query("SELECT * FROM tags");
echo "<form action='' metod='GET'>";
while($tags=mysql_fetch_array($options)) {
$tag=$tags['tagname'];
echo "<input type='checkbox' value='".$tag."'>".$tag."</input>";
$picts=mysql_query("SELECT * FROM imgs WHERE tagname=$tag");
while($imgs=mysql_fetch_array($picts)) {
$img = $imgs['img'];
echo "<img src='arimg/".$img."' height='300' width='300'>";
}
}
echo "<form>";
?>
最佳答案
使用mysql join在一个查询中获取它们:
$options=mysql_query("SELECT t.tagname tagname, i.img img FROM tags t join imgs i on i.tagname = t.tagname ORDER BY t.tagname ASC");
echo "<form action='' metod='GET'>";
$tags_displayed = array();
while($row=mysql_fetch_array($options)) {
$tag=$row['tagname'];
$img = $row['img'];
if(!isset($tags_displayed[$tag])){
echo "<input type='checkbox' value='".$tag."'>".$tag."</input>";
$tags_displayed[$tag]=1;//unique tag displayed only once
}
echo "<img src='arimg/".$img."' height='300' width='300'>";
}
echo "</form>";
并注意-不建议使用mysql_函数,请改用mysqli或PDO准备的语句。