Advertisement
rfv123

53020396 how-to-display-grouped-data-in-separate-tables-with

Oct 28th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.69 KB | None | 0 0
  1. <?php // 53020396/how-to-display-grouped-data-in-separate-tables-with-a-php-loop
  2.  
  3. /* ---------------------------------------------------------------------------
  4.  * The input stream consists of an Ordered Iteration of:
  5.  *    A collection of Individual Records for each Student (StudentRecordGoup)
  6.  *    
  7.  *  Each StudentRecordGoup consists of a Sequence of:
  8.  *     Start of Group
  9.  *     Iteration of Student records belonging to the group
  10.  *     End of Group
  11.  *
  12.  *   Notice: There is no 'IF' statement anywhere in the control logic for a group!
  13.  *  
  14.  *   Please note: There is exactly one place in the code to assign any appropriate action!
  15.  *                i.e. The structure of the code exactly matched the structure of the data. :)  
  16.  *
  17.  *   This makes it easier to debug, maintain and amend?
  18.  *  
  19.  *   To do this we need 'state' information. Specifically that a record is part
  20.  *   of the current 'student record group' group or not. How do we do this?
  21.  *    
  22.  *   We always need a record to test! Including at the start! We never read a record
  23.  *   and then decide what to do with it. We always know where we are in the data
  24.  *   structure and either the current record belongs to the group or not.
  25.  *  
  26.  *    We need to use a technique called 'Read Ahead'. Literally, There is always
  27.  *    a record to process. You don't have to test it to know where you are.
  28.  *    Once you process it then you immediately read the next record from the input.
  29.  *    i.e. You end up reading a new record NOT JUST AT THE WND OF THE LOOP!
  30.  *    You cannot use 'foreach' loops.
  31.  *  
  32.  *   We have to include Initialisation to setup code and any termination code.
  33.  *  
  34.  *   I will put all the 'action place holders' in functions. That way it avoids
  35.  *   obscuring the high-level logic.
  36.  */
  37.  
  38. // Get the ordered student detail records
  39. $pdo = getPdoConnection();
  40. $pdoResultSet = prepareAndExecuteQuery($pdo);
  41.  
  42.  
  43. // Process all the students Record Groups  - 'read ahead' of the row  
  44. $curStudentRecord = $pdoResultSet->fetch();  // is assoc array
  45.  
  46. while ($curStudentRecord !== false)  { // process the student record groups
  47.  
  48.    // process one complete student group...
  49.    $curStudentRecordGroupId = $curStudentRecord['sid'];
  50.    startStudendRecordGroup($curStudentRecordGroupId);
  51.    while (   $curStudentRecord !== false  // check record belongs to the current group
  52.           && $curStudentRecord['sid'] === $curStudentRecordGroupId) {
  53.    
  54.         processStudentRecord($curStudentRecord);
  55.         $curStudentRecord = $pdoResultSet->fetch();  // read the next record
  56.     }
  57.  
  58.    endStudendRecordGroup($curStudentRecordGroupId); // ignore the current record
  59.                                                     // that is the next group!
  60. }
  61. // all groups have been processed
  62. exit;
  63.  
  64. /* --------------------------------------------------------------------------
  65.  * Student record group processing
  66.  */
  67. function startStudendRecordGroup($curStudentRecordGroupId)
  68. {
  69.     echo "<!-- new group: sid = $curStudentRecordGroupId -->";
  70.     echo "<table><tr><th>SID</th><th>Date</th><th>FName</th><th>LName</th>
  71.                     <th>activity</th><th>time</th><th>score</th></tr>";
  72. }
  73.  
  74. function processStudentRecord($curStudentRecord)
  75. {
  76.     echo "<!-- group record: sid = {$curStudentRecord['sid']} -->";
  77.     echo "<tr>
  78.                    <td>" . $curStudentRecord['sid'] . "</td>
  79.                    <td>" . $curStudentRecord['fname'] . "</td>
  80.                    <td>" . $curStudentRecord['lname'] . "</td>
  81.                    <td>" . $curStudentRecord['col3'] .  "</td>
  82.                    <td>" . $curStudentRecord['col4'] .  "</td>
  83.                    <td>" . $curStudentRecord['col4'] .  "</td>
  84.                </tr>";
  85. }
  86.  
  87. function endStudendRecordGroup($curStudentRecordGroupId)
  88. {
  89.     echo "<!-- end group: sid = $curStudentRecordGroupId -->";
  90.     echo "</table>";
  91. }
  92.  
  93. /* --------------------------------------------------------------------------
  94.  * Database access
  95.  */
  96.  
  97. // Execute query and return 'resultset'
  98. function prepareAndExecuteQuery(\PDO $pdo)
  99. {
  100.    $sql = 'SELECT id, sid, fname, lname, col3, col4, col5
  101.           FROM activity
  102.           ORDER BY sid, id';
  103.            
  104.    $stmt = $pdo->prepare($sql);
  105.    $allOk = $stmt->execute();
  106.    return $stmt;          
  107. }
  108.  
  109. // DB Connection
  110. function getPdoConnection()
  111. {
  112.     $opt  = array(
  113.                 \PDO::ATTR_ERRMODE            => \PDO::ERRMODE_EXCEPTION,
  114.                 \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
  115.                 \PDO::ATTR_EMULATE_PREPARES   => false,
  116.             );
  117.                
  118.     $pdo = new \PDO('mysql:host=localhost;dbname=notmydb;', 'notme', 'notmypass', $opt);
  119.     return $pdo;            
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement