简体   繁体   中英

display sub-category under category with certain value

I have a table "tests".

Category     |   Heading  |    Tests      |
HB           |Hematology  |     HB        |
TC           |Hematology  |     TC        |
DC           |Hematology  |     Neutrophil|
DC           |Hematology  |     Monocyte  |
DC           |Hematology  |     Lymphocyte|
DC           |Hematology  |     Basophil  |
KFT          |Biochemistry| Urea          |
KFT          |Biochemistry| S.Creatinine  |
KFT          |Biochemistry| S.Uric Acid   |     
Lipid Profile|Biochemistry| Cholesterol   |
Lipid Profile|Biochemistry| Triglyceride  |
Lipid Profile|Biochemistry|HDL Cholesterol|
Lipid Profile|Biochemistry|LDL Cholesterol|
Albumin      |Biochemistry|   Albumin     | 
Globulin     |Biochemistry|   Globulin    |   
Amylase      |Biochemistry|   Amylase     |  
VDRL         |  Serology  |   VDRL        |  
HIV          |  Serology  |   HIV         |  
HCV          |  Serology  |   HCV         |  
Dengue       |  Serology  |   NS1 Antigen |  
Dengue       |  Serology  |   IgG Antibody|  
Dengue       |  Serology  |   IgM Antibody|  
Urine R/E    |     Urine  |   Color       |
Urine R/E    |     Urine  |   Transparency| 
Urine R/E    |     Urine  |   Reaction    | 

another table "patient"

Name     |    Tests      |    P_no
John     |     HB        |     1
Krish    |     HB        |     2
Krish    |     TC        |     2
Krish    |     DC        |     2
Krish    |     Urine R/E |     2
Krish    |     Dengue    |     2
Amy      |     HB        |     3
Amy      |     TC        |     3
Amy      |     DC        |     3
Amy      |     KFT       |     3
Amy      |     Albumin   |     3
Amy      |     HIV       |     3
Amy      |     Dengue    |     3
Amy      |     Urine R/E |     3

I have a form "Entry". I placed a textbox to enter patient's number to get the test details for report entry.

Patient Number : Textbox

          Submit Button

Putting number '3' in textbox and submitting the form, It has to display another form "Result.php" according to patient number as follows.

Patient No. : 3            Patient Name : Amy

                  Tests for Hematology

  HB                 HB             Textbox
  TC                 TC             Textbox
  DC             Neutrophil         Textbox
                 Monocyte           Textbox
                 Lymphocyte         Textbox
                 Basophil           Textbox

                Tests for Biochemistry

  KFT            Urea               Textbox
               S.Creatinine         Textbox
               S.Uric Acid          Textbox
  Albumin         Albumin           Textbox

                Tests for Serology

  HIV            HIV                Textbox
  Dengue         NS1 Antigen        Textbox
                 IgG Antibody       Textbox
                 IgM Antibody       Textbox

                Tests for Urine

  Urine R/E         Color           Textbox
                    Transparency    Textbox
                    Reaction        Textbox

Need help to get "Result.php" as it has shown. Any help will be appreciated.

a simple aproach would be to have a query like such:

SELECT tests.Category, tests.Heading, tests.Tests FROM patient
JOIN tests ON tests.Category = patient.Tests
WHERE patient.P_no = ?
ORDER BY tests.Heading, tests.Category

That way you can retreive the all the data you need to display. Then for displaying it it is possible to just loop over the result set and store what the last heading and category were and if the current element has different heading or category, print that, otherwise print an empty string there.

hope that helps with the concept.

the part on how to display the data is assuming that you are fetching data as an associative array named $rows .

$lastHeading = "";
$lastCategory = "";
foreach($rows as $row) {
    //check if there is a new heading (if so, print it and update the $lastHeading)
    if($lastHeading !== $row['Heading']) {
        echo $row['Heading'];
        $lastHeading = $row['Heading'];
    }
    else {
        //change this output to fit your output format
        echo "";
    }

    //check if there is a new category (if so, print it and update $lastCagetory
    if($lastCategory !== $row['Category']) {
        echo $row['Category'];
        $lastCategory = $row['Category'];
    }
    else {
        //change this output to fit your output format
        echo "";
    }

    //add the test name
    echo ($row['Tests']);
}

you should update markup/output to fit your actual layout/desired html. It could be a table, it could be some other type but i hope the concept is understandable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM