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

标签 php mysql

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

相关文章:

php - 从 php 调用 shell 脚本 - 无法正常工作

php - mysql 查询以获取匹配的记录

javascript - Jquery .html() 不工作 - 选择 html

php - 经常性付款计费周期问题

php $_POST 选择选项值不起作用未定义索引错误

PHP:获取所有类的连接和记录器对象的最佳方法

java - 是否可以编写跨数据库连接查询?

php - MYSQL PHP获取单个字段的平均值

mysql - 在 MySQL 查询中使用带有 LIMIT 的 SELECT 时如何计算所有行数?

php - 使用 jQuery.ajax 在 MySQL 中存储表单数据