1

我知道这里有类似的问题,我已经阅读了一些帖子和答案,并尝试了其中的一些,但无论是我对 PHP 的了解有限还是我的案例的特殊性,我都需要问这个。

我正在构建字典(id、英语、保加利亚语、主题 ID)并希望根据主题 ID 对搜索结果进行分组。我ORDER BY theme_id, id在我的查询中使用,但我最终显示了每个结果的主题,而我想将它们分类如下:

主题 1 - 结果 1 - 结果 2 主题 2 - 结果 3 - 结果 4 .....

这是我的代码的相关部分:

while($row = mysql_fetch_array($result)) {
    //$id       = $row['id'];
    $english    = $row['english'];
    $bulgarian  = $row['bulgarian'];
    $theme_id   = $row['theme_id'];

    $theme_name = "theme_".$lang;
    $theme_query= mysql_query("SELECT theme_id,".$theme_name." FROM ".DICTIONARY_THEMES." WHERE theme_id = ".$theme_id."");
    $theme_row  = mysql_fetch_array($theme_query);
    $theme      = $theme_row[$theme_name];

    if($source == "english") {

        foreach($keywords as $keyword) {
            $english = preg_replace("|($keyword)|Ui", "<span style=\"color:#780223\">".$keyword."</span>", $english);
        }

        $print .= "<li class=\"results-row\">".$theme.": ".$english." = ".$bulgarian."</li>";
    }

    elseif($source == "bulgarian") {
        foreach($keywords as $keyword) {
            $bulgarian = preg_replace("|($keyword)|Ui", "<span style=\"color:#780223;\">".$keyword."</span>", $bulgarian);
        }

        $print .= "<li class=\"results-row\">".$theme.": ".$bulgarian." = ".$english."</li>";
    }
}//end while

编辑:已解决,一位朋友帮助改进了我的代码。

while($row = mysql_fetch_array($result)) {
    $english    = $row['english'];
    $bulgarian  = $row['bulgarian'];
    $theme_id   = $row['theme_id'];

    $theme_name = "theme_".$lang;
    $theme_query= mysql_query("SELECT theme_id,".$theme_name." FROM ".DICTIONARY_THEMES." WHERE theme_id = ".$theme_id."");
    $theme_row  = mysql_fetch_array($theme_query);
    $theme      = $theme_row[$theme_name];

    // add all results to an array
    $results[] = array(
        'english'   => $english,
        'bulgarian' => $bulgarian,
        'theme'     => $theme
    );

}//end while

$theme = null;
foreach ($results as $result) {
    if ($theme != $result['theme']) {
        $theme = $result['theme'];
        $print .= "<h3>" . $result['theme'] . "</h3>";
    }

    if ($source == "english") {
        foreach ($keywords as $keyword) {
            $result['english'] = preg_replace("|($keyword)|Ui", "<span style=\"color:#780223\">" . $keyword . "</span>", $result['english']);
        }
        $print .= "<li class=\"results-row\">" . $result['english'] . " = " . $result['bulgarian'] . "</li>";
    } elseif ($source == "bulgarian") {
        foreach ($keywords as $keyword) {
            $result['bulgarian'] = preg_replace("|($keyword)|Ui", "<span style=\"color:#780223;\">" . $keyword . "</span>", $result['bulgarian']);
        }
        $print .= "<li class=\"results-row\">" . $result['bulgarian'] . " = " . $result['english'] . "</li>";
    }
}
4

1 回答 1

0

根据您的代码,您应该首先将所有项目添加到数组中,然后在单独的函数/块中打印该数组。

在你的第二个 MySQL 查询之后,输入类似

$output[$theme_id] = mysql_fetch_array( $theme_query );

并将以下所有代码移出外部 result-while-loop ( while($row = mysql_fetch_array($result)))。

但事实上,我会尝试将 MySQL 查询放在一起,以获得所有语言中所有选定主题的有序、分组结果,而无需在循环内查询数据库。

或者您使用现有的东西,例如这个单例 Lexicon 类

于 2012-10-17T18:51:00.897 回答