gpt4 book ai didi

php - 查询多个表输出重复的结果

转载 作者:行者123 更新时间:2023-11-29 06:13:06 24 4
gpt4 key购买 nike

我有一个ajax搜索引擎。如果没有打开任何过滤器,我想查询每个表中的标题。在此示例中,只有两个表。音乐页面和餐厅页面。

public function Any_Category_Name($search_title) {

$query = mysql_query("SELECT music_page.title AS music_title,
restaurant_page.title AS restaurant_title
FROM music_page,
restaurant_page
WHERE music_page.title
LIKE '%$search_title%' OR
restaurant_page.title
LIKE '%$search_title%'");
while($fetch = mysql_fetch_assoc($query)) {
$music_title = $fetch['music_title'];
$restaurant_title = $fetch['restaurant_title'];
echo '<b>Restaurant:</b> '.$restaurant_title.'<br />';
echo '<b>Music Spot:</b> '.$music_title.'<br />';
}

}

这是输入字母/搜索时的输出:

Restaurant: Giordanoes
Music Spot: jazz showcase
Restaurant: Giordanoes
Music Spot: The Green Mill
Restaurant: Giordanoes
Music Spot: ayooooo
Restaurant: Giordanoes
Music Spot: ayoooooooo
Restaurant: Giordanoes
Music Spot: new one

Giordanoes 是restaurant_page 表中的唯一行。它是为 music_page 中的每一行输出的。我该如何解决这个问题?

最佳答案

替代

while($fetch = mysql_fetch_assoc($query)) {
// Create the listing array
$listing[$fetch['restaurant_title']] = array($fetch['music_title'] => 'music_title');
}

// Check if we have Restaurants
if(is_array($listing)) {
// Loop through the new listings array
foreach($listing as $restaurant_title => $music_titles) {
echo '<b>Restaurant:</b> '.$restaurant_title.'<br />';

// Check if we have Music
if(is_array($music_titles)) {
// Loop through the Music
foreach($music_titles as $music_title => $placeholder) {
echo '<b>Music Spot:</b> '.$music_title.'<br />';
}
} else {
echo '<b>Music Spot:</b> No Music found<br />';
}
}
} else {
echo '<b>Restaurant:</b> No Restaurants found<br />';
}

关于php - 查询多个表输出重复的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8158625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com