Переформатирование запроса

Мне назвали таблицу 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    |
+--------------+-----------------------------------+
1
задан Gautam Kumar 19 June 2010 в 04:11
поделиться

3 ответа

Что-то вроде:

$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 или на самом деле тире и стиль +.

2
ответ дан 2 September 2019 в 23:40
поделиться

Вам нужно получить все «болезни» и для каждой «болезни» собрать соответствующий «симптом», как это (работает и проверено) :

<?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>';

?>
0
ответ дан 2 September 2019 в 23:40
поделиться
<?
foreach( $rows as $row ){
  if( $row[0] != $last )
    echo $row[0] . " ";
  echo $row[1]
  $last = $row;
}
?>

или аналогичном

0
ответ дан 2 September 2019 в 23:40
поделиться
Другие вопросы по тегам:

Похожие вопросы: