title: How to group the same patient name into one cell using rowspan even if we have multiple rows returned to Ajax and displayed
body: <p>I need to re-format my html table that is displaying results from an Ajax response.</p>
  
  <p>Usually, each patient may have 1 or more medicine and the table would be like the following:</p>
  
  <p><a href="https://i.stack.imgur.com/LuILC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LuILC.png" alt="enter image description here"></a>        </p>
  
  <p>What I want is as long as we are displaying the same patient name, I need to group the 3 cells of <code>Patient 1</code> into one cell using rowspan.</p>
  
  <p>Here is how I am getting data from server and displaying them into html page using Ajax and jquery:</p>
  
  <pre><code>     $.ajax({
          url:'../php/getTemplateThreeData.php',
          type: 'POST',
          data: {monthVal, nationality},
          dataType: 'JSON',
          success:function(resp)
          {
              $("#tempThreeTable #fade").fadeOut(400);
              $.each(resp, function(key, result)
              {
                  if(result['nationality']=='something')
                  {
                      $('#after_tr').after("&lt;tr id='fade'&gt;&lt;td&gt;"
                          +result['patient_name_en']+"&lt;/td&gt;&lt;td&gt;"
                          +result['nationality']+"&lt;/td&gt;&lt;td&gt;"
                          +result['registration_number']+"&lt;/td&gt;&lt;td&gt;"
                          +result['gender']+"&lt;/td&gt;&lt;td style='width: fit'&gt;"
                          +result['dob']+"&lt;/td&gt;&lt;td&gt;"
                          +result['patient_address']+"&lt;/td&gt;&lt;td&gt;"
                          +result['med_name']+"&lt;/td&gt;&lt;td&gt;"
                          +result['given_quantity']+"&lt;/td&gt;&lt;/tr&gt;");
                  }
                  else
                  {
                      $('#after_tr').after("&lt;tr id='fade'&gt;&lt;td&gt;"
                          +result['patient_name_en']+"&lt;/td&gt;&lt;td&gt;"
                          +result['nationality']+"&lt;/td&gt;&lt;td&gt;"
                          +result['patient_id']+"&lt;/td&gt;&lt;td&gt;"
                          +result['gender']+"&lt;/td&gt;&lt;td style='width: fit'&gt;"
                          +result['dob']+"&lt;/td&gt;&lt;td&gt;"
                          +result['patient_address']+"&lt;/td&gt;&lt;td&gt;"
                          +result['med_name']+"&lt;/td&gt;&lt;td&gt;"
                          +result['given_quantity']+"&lt;/td&gt;&lt;/tr&gt;");
                  }
              });
          },
          error:function(resp)
          {
  
          }
  </code></pre>
  
  <p>The query is the following (I tried to add a subquery to it but it didn't worked because if the same patient appeared 3 times, I will have the count as 2 for the same medicine, and another row with 1 for the other medicine):</p>
  
  <pre><code>$getTempThree = "SELECT
      patient.patient_id,
      CONVERT(aes_decrypt(patient.patient_name_en, :encKey)USING utf8mb4) as patient_name_en,
      patient.nationality,
      CONVERT(aes_decrypt(patient.registration_number, :encKey) USING utf8mb4) as unhcr_registration_number,
      patient.gender,
      patient.dob,
      patient.patient_address,
      medication.med_name,
      sum(consultation_med.given_quantity) as given_quantity
  FROM
      patient
  LEFT JOIN
      visit
  ON
      patient.patient_id = visit.patient_id
  LEFT JOIN
      consultation
  ON
      consultation.visit_id = visit.visit_id
  LEFT JOIN
      consultation_med
  ON
      consultation_med.consultation_id = consultation.consultation_id
  LEFT JOIN
      med_pharmacy
  ON
      med_pharmacy.med_pharmacy_id = consultation_med.med_pharmacy_id
  LEFT JOIN
      medication
  ON
      medication.med_id = med_pharmacy.med_id
  WHERE
      consultation_med.clinic_id = :cid
  AND
      patient.nationality = :nationality
  AND
      DATE_FORMAT(visit.date_of_visit, '%Y-%m') = :getDate
  AND
      medication.med_type = :medType
  GROUP BY 
      patient.patient_id, 
      medication.med_name
  ORDER BY
      patient_name_en DESC
  ";
  </code></pre>
  
  <p>The JSON response:</p>
  
  <blockquote>
    <p>[{0: "someID", 1: "Patient1", 2: "Syrian", 3: "", 4: "female", 5:
    "1954-10-19",…},…] 0 : {0: "someID, 1: "Patient1", 2: "Syrian", 3: "",
    4: "female", 5: "1954-10-19",…} 1 : {0: "someID", 1: "Patient1", 2:
    "Syrian", 3: "", 4: "female", 5: "1954-10-19",…} 2</p>
  </blockquote>