Having a bit of a rough time visualizing your intention, but from the top of my head, you need to add a set of if
statements in your loop when building the header.
if (row[1] == "spray") {
echo "<tr><th>Spray</th></tr>";
}
Again, this is just a rough example, with row[1]
being the column that contains either medicine or spray. This way, you can do more if the doctor has prescribed both (i.e add the medicine rows on a new tab, just an idea) or you can leave one out altogether.
I'll edit my comment if anything else comes to mind.
EDIT:
So I've built a quick table 'patients' with a medicine column and a spray column.
select if (spray is null, medicine, spray) as type from patients where medicine is not null
This will display all medicine rows except null
. You can rename the type
column to something else and use it in PHP when you build the HTML header.
I've built the table here, http://phpfiddle.org/, go to the 'Resources' tab and click on the "Aiding Tools =>" button and change to MySQL. Click on the "Show tables" button and search for the "patients" table, click on it and copy paste my query to see the results.
Edit:
Forgot to mention that you can add other columns after as type
for alternative to select *
Edit 3 (lol):
You can also look around stackoverflow on other answers and take small things from them, like building dynamic column names, by altering and chaining if
statements for each column (SQL: Selecting columns based on column value from another table).
Quick tip, I also learned the hard way that you can do much more with mysql and not let all the heavy lifting to PHP, it's faster, cleaner and safer to get print ready result sets from MySQL rather than formatting them with PHP after you did a Select *
, especially when you have tables with a lot of data, and it scales better.
Hope this helps.