PHP / MySQL获取id = id的名称

时间:2010-10-18 10:39:03

标签: php mysql

我有一个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;        
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我不确定我是否遗漏了某些内容,但看起来这可以通过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...