0

我一直遇到一个问题,我是 mysql 和 php 的新手,这里是代码,所以我可以详细解释:

$metros = array(1,263);
foreach($metros as $metro_id) {
    $sql = "SELECT cuisine_id, cuisine_name_en FROM poi_restaurant_cuisines";
    $result = mysql_query($sql);
    $cuisine_id = array();
    $cusine_name = array();
    while($row = mysql_fetch_assoc($result)) {
        $cuisine_id[] = $row['cuisine_id'];
        $cuisine_name[] = $row['cuisine_name_en'];
    }

    foreach ($cuisine_id as $cuisine) {
        $sql = "
            SELECT COUNT(*) 
            FROM poi AS p 
                LEFT JOIN poi_restaurant AS pr USING (poi_id)
            WHERE p.poi_address_prefecture_id = '$metro_id' 
                AND pr.poi_restaurant_cuisine_id_array 
                AND  find_in_set('$cuisine', poi_restaurant_cuisine_id_array) 
                AND p.poi_status = 1";

        $result = mysql_query($sql);
        $count_cuisine = array();

        while($row = mysql_fetch_array($result)) {
            $count_cuisine[$metro_id][$cuisine] = $row['COUNT(*)'];
        }

        echo "<table border = 1 cellpadding= 5 cellspacing= 5 width= 100>";
        echo "<tr><th>CuisineID</th><th>Count</th></tr>";
        echo "<tr><td>";
        echo $cuisine;
        echo "</td><td>";
        echo $count_cuisine[$metro_id][$cuisine];
        echo "</td><td>";
        echo "</tr>";
        echo "</table>";
    }
}

poi_restaurant_cuisine_id_array包含 csv 值。我能够在网页上生成计数和美食 ID。我想用美食名称替换美食 ID。因为我是初学者,所以我不太擅长 sql 或 PHP。我希望我说得足够清楚。任何帮助都非常感谢......谢谢。

4

2 回答 2

0

试试这个:

echo '<table border="1" cellpadding="5" cellspacing="5" width="100">';
echo "<tr><th>Cuisine</th><th>Count</th></tr>";

$metros = array(1,263);
foreach($metros as $metro_id) {
    $sql = "SELECT cuisine_id, cuisine_name_en FROM poi_restaurant_cuisines";
    $result = mysql_query($sql);
    $cuisines = array();
    while($row = mysql_fetch_assoc($result)) {
        $cuisines[] = array(
            'id'    => $row['cuisine_id'],
            'name'  => $row['cuisine_name_en'],
        );
    }

    foreach ($cuisines as $cuisine) {
        $sql = "
            SELECT COUNT(*) 
            FROM poi AS p 
                LEFT JOIN poi_restaurant AS pr USING (poi_id)
            WHERE p.poi_address_prefecture_id = '$metro_id' 
                AND pr.poi_restaurant_cuisine_id_array 
                AND  find_in_set('{$cuisine['id']}', poi_restaurant_cuisine_id_array) 
                AND p.poi_status = 1";

        $result = mysql_query($sql);
        $count_cuisine = array();

        while($row = mysql_fetch_array($result)) {
            $count_cuisine[$metro_id][$cuisine['id']] = $row['COUNT(*)'];
        }

        echo "<tr>
            <td>{$cuisine['name']}</td>
            <td>{$count_cuisine[$metro_id][$cuisine['id']]}</td>";
        </tr>";
    }
}

echo "</table>";
于 2012-06-06T10:29:07.817 回答
0

假设 food_id 是一个唯一标识符,那么只需将其用作数组索引....

while($row = mysql_fetch_assoc($result)) {
    $cuisines[$row['cuisine_id']] = $row['cuisine_name_en'];
}
....
foreach ($cuisines as $cuisine_id=>$cuisine_name_en) {

但是,在单个列中存储多个值是一个非常糟糕的主意。

在循环中生成和运行查询是另一个非常糟糕的主意。

可以将其简化为单个查询,在内循环外部声明和调用,但由于您的数据没有规范化,这相当复杂。

于 2012-06-06T12:04:16.100 回答