Reformatting the query
I have a table called sym_dis
.
select * from sym_dis
gives
+--------------+-----------------------------------+
| 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 |
+--------------+-----------------------------------+
How can I reformat the above table using php and html so that I get the following output?
+--------------+-----------------------------------+
| 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 |
+--------------+-----------------------------------+
Something like:
$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>";
I wasn't sure if you wanted an html table, or actually the dashes and + style.
<?
foreach( $rows as $row ){
if( $row[0] != $last )
echo $row[0] . " ";
echo $row[1]
$last = $row;
}
?>
or similar
You need to get all "disease" and the, for each "disease" collect the matching "symptom", like this (working and tested):
<?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>';
?>
精彩评论