1

I am having a tough time in writing a code and need your help. I am creating an application in which patient's data is stored in a DB by doctor. I know how to retrieve data from MySQL DB into HTML table using PHP. However I do not know how to dynamically populate HTML headers and data based to data retrieved. To be more specific, I want to display only that data which doctor has prescribed.

If the doctor has prescribed medicine then only medicine header should be populated.. If doctor has prescribed only spray, then only spray header should be populated and so on..

Havish
  • 33
  • 7

2 Answers2

0

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.

Community
  • 1
  • 1
J3Rik0
  • 11
  • 1
  • 3
  • I thought of doing what you told but then I think, I need to write lot of if queries and populate the data..Correct me if I am wrong. – Havish Jan 04 '14 at 10:59
  • Actually, you don't need to do a lot of queries, you can use cases to narrow down the select result set. http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html set a case to see which column returns results, the medicine column or the spray column. Also, you can do a count on the select * query and compare the column results. I'm going to fiddle around with the sql code and see if I can come up with a hands on example. – J3Rik0 Jan 04 '14 at 11:06
  • Let me have a look and I will get back. – Havish Jan 04 '14 at 11:11
0

i don't know what you mean by header but for populating the DOM according t the values is my making the control statements according to the rows reeturned

example:

<?php 
if(mysqli_num_rows($the_query_variable) >= 1){
 //the while loop for grabbing the data      
?>
 //these are the datas
<div>the results</div>

 <?php      
    } else {
 ?>
  //you don't even need to do this else statement if you don't want to say no results... 
 <div> no results found</div>
<?php 
 }

 ?>