2010-06-19 4 views
1

У меня есть таблица под названием 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 | 
+--------------+-----------------------------------+ 
+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-таблицу, или на самом деле тире и + стиль.

+0

Спасибо большое ... –

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

или аналогичный

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

?> 
Смежные вопросы