我有一个 mysql 表,其中的结果分配了一个类别 id,还有一个单独的表,按名称列出了所有类别及其各自的 id。所有结果均按 ID 分组,并且 ID 显示为标题。
如何将其他数据库中的 ID 显示为其各自的名称? 这是我正在使用的完整代码,其中有问题的行已被注释掉。非常感谢任何帮助。
$subcatQuery=mysql_query("select * from issubcat order by id");
$subcatResult=mysql_fetch_array($subcatQuery);
$query = "SELECT * from isgallery where galleryPage ='1'";
$resultSet = mysql_query($query);
if (mysql_num_rows($resultSet))
{
$gallArray = array();
while ($galleryResult = mysql_fetch_array($resultSet))
{
// if($galleryResult['subcatPage'] == $subcatResult['id'])
// {
// $gallSection = $subcatResult['subcat'];
// }
if (!isset($gallArray[$gallSection]))
{
$gallArray[$gallSection] = array();
}
$gallArray[$gallSection][] = $galleryResult;
}
foreach($gallArray as $gallSection => $gallItems)
{
echo '<div class="com-gallery">' . $gallSection . '</div>' . PHP_EOL;
echo '<ul class="photo-gallery">'. PHP_EOL;
foreach ($gallItems as $photoresult)
{
echo '<li><a rel="gallery" href="'.$wwwUrl.'images/properties/gallery/'.$photoresult['imagename'].'" ';
if($photoresult['galleryTitle'])
{
echo 'title="'.$photoresult['galleryTitle'].'"';
}
else
{
echo 'title="'.$photoresult['imagename'].'"';
}
echo '><img alt="" src="'.$wwwUrl.'images/properties/gallery/tn/'.$photoresult['imagename'].'" width="122" height="88" /></a></li>'. PHP_EOL;
}
echo '</ul><br /><br />' . PHP_EOL;
}
}
}
最佳答案
我不确定我是否遗漏了一些东西,但看起来这可以简单地通过 JOIN
来完成。
$query = "SELECT isgallery.*, issubcat.subcat
FROM `isgallery`
INNER JOIN `issubcat`
ON `isgallery`.`subcatPage` = `issubcat`.`id`
WHERE `galleryPage` = '1'";
$resultSet = mysql_query($query);
if(mysql_num_rows($resultSet))
{
$gallArray = array();
while ($galleryResult = mysql_fetch_array($resultSet))
{
$gallSection = $galleryResult['subcat'];
// The rest of your code...
关于PHP/MySQL 获取名称,其中 id = id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3958317/