重新格式化查询

时间:2010-06-19 04:11:47

标签: php mysql html

我有一张名为sym_dis的表。

select * from sym_dis给出了

 +--------------+-----------------------------------+
 | disease      | symptom                           |
 +--------------+-----------------------------------+
 | typhoid      | headache                          |
 | typhoid      | high fever                        |
 | typhoid      | pain in the abdomen               |
 | typhoid      | sore throat                       |
 | typhoid      | feeling of fatigue                |
 | typhoid      | weekness                          |
 | typhoid      | constipation                      |
 | polio        | headache                          |
 | polio        | nausea                            |
 | polio        | vomiting                          |
 | polio        | general discomfort                |
 | polio        | slight fever for upto three days  |
 | polio        | stiffness                         |
 | polio        | fever                             |
 | polio        | difficulty swallowing             |
 | polio        | muscle pain and spasms            |
 | yellow fever | high fever                        |
 | yellow fever | chills                            | 
 | yellow fever | headache                          |
 | yellow fever | muscle ache                       |
 | yellow fever | vomiting                          |
 | yellow fever | backache                          |
 | hepatitis B  | jaundice                          |
 | hepatitis B  | fatigue                           |
 | hepatitis B  | abdominal pain                    |
 | hepatitis B  | loss of appetite                  |
 | hepatitis B  | nausea                            |
 | hepatitis B  | vomiting                          |
 | hepatitis B  | joint pain                        |
 | hepatitis B  | dark coloured wine                |
 | hepatitis B  | yellowish tinged skin and eyes    |
 +--------------+-----------------------------------+

如何使用php和html重新格式化上表,以便获得以下输出?

+--------------+-----------------------------------+
| disease      | symptom                           |
+--------------+-----------------------------------+
| typhoid      | headache                          |
|              | high fever                        |
|              | pain in the abdomen               |
|              | sore throat                       |
|              | feeling of fatigue                |
|              | weekness                          |
|              | constipation                      |
| polio        | headache                          |
|              | nausea                            |
|              | vomiting                          |
|              | general discomfort                |
|              | slight fever for upto three days  |
|              | stiffness                         |
|              | fever                             |
|              | difficulty swallowing             |
|              | muscle pain and spasms            |
| yellow fever | high fever                        |
|              | chills                            | 
|              | headache                          |
|              | muscle ache                       |
|              | vomiting                          |
|              | backache                          |   
| hepatitis B  | jaundice                          |
|              | fatigue                           |
|              | abdominal pain                    |
|              | loss of appetite                  |
|              | nausea                            |
|              | vomiting                          |
|              | joint pain                        |
|              | dark coloured wine                |
|              | yellowish tinged skin and eyes    |
+--------------+-----------------------------------+

3 个答案:

答案 0 :(得分:2)

类似的东西:

$result = mysql_query("select * from sym_dis order by disease");
$lastDisease = '';
echo "<table>";
while ($row = mysql_fetch_assoc($result)) {
    echo "<tr><td>";
    if ($row['disease'] != $lastDisease) 
    { 
       echo $row['disease'];
       $lastDisease = $row['disease'];
    }
    echo "</td><td>";
    echo $row['symptom'];
    echo "</td></tr>";
}
echo "</table>";

我不确定你是否想要一个html表,或者实际上是破折号和+样式。

答案 1 :(得分:0)

<?
foreach( $rows as $row ){
  if( $row[0] != $last )
    echo $row[0] . " ";
  echo $row[1]
  $last = $row;
}
?>

或类似的

答案 2 :(得分:0)

你需要得到所有的“疾病”,并为每种“疾病”收集匹配的“症状”,如(工作和测试)

<?php

define("HOST", "localhost");

// Database user
define("DBUSER", "username");

// Database password
define("PASS", "password");

// Database name
define("DB", "database_name");

############## Make the mysql connection ###########

$conn = mysql_connect(HOST, DBUSER, PASS) or  die('Could not connect !<br />Please contact the site\'s administrator.');

$db = mysql_select_db(DB) or  die('Could not connect to database !<br />Please contact the site\'s administrator.');


$query = mysql_query(" SELECT DISTINCT disease FROM sym_dis ");

echo '<table>';

while ($data = mysql_fetch_array($query)) {

    echo '<tr><td valign="top">'.$data["disease"].'</td><td>';

    $query2 = mysql_query(" SELECT * FROM sym_dis WHERE disease = '".$data['disease']."' ");

    while ($data2 = mysql_fetch_array($query2)) {

        echo $data2["symptom"] . '<br>';

    }

    echo '</td></tr>';

}

echo '</table>';

?>