简体   繁体   English

PHP / MySQL获取名称,其中id = id

[英]PHP/MySQL get name where id = id

I have one mysql table with results assigned a category id, and a seperate table that lists all of the categories by name with their respective id's. 我有一个mysql表,其结果分配了一个类别ID,还有一个单独的表,按名称列出了所有类别及其各自的ID。 All of the results are grouped by their id's and the id is displayed as the heading. 所有结果均按其ID分组,并且ID显示为标题。

How do I display the id as its respective name from the other database? 如何从另一个数据库中显示ID作为其各自的名称? Here is the full code I'm using, with the line in question commented out. 这是我正在使用的完整代码,其中有问题的行已被注释掉。 Any help greatly appreciated. 任何帮助,不胜感激。 S. 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;        
        }
    }
}

I'm not sure if I am missing something, but it looks like this can simply be done with a JOIN . 我不确定是否丢失了某些内容,但看起来可以简单地通过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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM