[英]PHP/MySQL get name where id = id
我有一個mysql表,其結果分配了一個類別ID,還有一個單獨的表,按名稱列出了所有類別及其各自的ID。 所有結果均按其ID分組,並且ID顯示為標題。
如何從另一個數據庫中顯示ID作為其各自的名稱? 這是我正在使用的完整代碼,其中有問題的行已被注釋掉。 任何幫助,不勝感激。 S.
$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...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.