Advertisement
Faguss

Userspice get all users table example2

Jan 19th, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. <?php
  2. include("__header.php");
  3.  
  4. echo <<<end
  5. <table class="table table-striped">
  6.     <thead>
  7.         <tr>
  8.             <th>Username</th><th>Joined On</th><th>Group(s)</th>
  9.         </tr>
  10.     </thead>
  11.     <tbody>
  12. end;
  13.  
  14.  
  15.  
  16.  
  17. // Pagination
  18. $record_start = 0;
  19. $record_limit = 10;
  20. $current_page = 1;
  21.  
  22. if (isset($_GET["page"])  &&  is_numeric($_GET["page"]))
  23.     $current_page = (int) $_GET["page"];
  24.  
  25. if ($current_page < 1)
  26.     $current_page = 1;
  27.  
  28. $record_start = ($current_page-1) * $record_limit;
  29.  
  30.  
  31.  
  32.  
  33. // Get data from the database
  34. $db->query("
  35.     SELECT
  36.         users.username AS name,
  37.         users.join_date,
  38.         permissions.name AS perm_name
  39.        
  40.     FROM
  41.         (SELECT id, username, join_date FROM users LIMIT $record_start,$record_limit) AS users
  42.        
  43.         INNER JOIN
  44.             user_permission_matches
  45.         ON
  46.             user_permission_matches.user_id = users.id
  47.            
  48.         LEFT JOIN
  49.             permissions
  50.         ON
  51.             permissions.id = user_permission_matches.permission_id
  52. ");
  53.    
  54.    
  55.  
  56.    
  57. // Display query output in a table
  58. $result         = $db->results(true);
  59. $num_of_items   = $db->count();
  60. $non_atomic_key = ["perm_name"];
  61. $non_atomic_val = [];
  62. $user_count     = 0;
  63.  
  64. foreach ($non_atomic_key as $key)
  65.     $non_atomic_val[$key] = [];
  66.  
  67. for ($i=0; $i<$num_of_items; $i++) {
  68.     $current_user = $result[$i]["name"];
  69.     $next_user    = "";
  70.    
  71.     if ($i < $num_of_items-1)
  72.         $next_user = $result[$i+1]["name"];
  73.    
  74.     foreach ($non_atomic_key as $key)
  75.         $non_atomic_val[$key][] = $result[$i][$key];
  76.            
  77.     if ($current_user != $next_user) {
  78.         $user_count++;
  79.         echo "<tr>";
  80.        
  81.         foreach ($result[$i] as $key=>$value) {
  82.             if (in_array($key,$non_atomic_key))
  83.                 $value = implode(", ", $non_atomic_val[$key]);
  84.            
  85.             echo "<td>$value</td>";
  86.         };
  87.                
  88.         echo "</tr>";
  89.        
  90.         foreach ($non_atomic_key as $key)
  91.             $non_atomic_val[$key] = [];
  92.     }
  93. }
  94.  
  95.  
  96.  
  97.  
  98. echo <<<end
  99.     </tbody>
  100. </table>
  101. end;
  102.  
  103. // Link to previous and next page
  104. if ($current_page > 1)
  105.     echo "<BR /><A HREF=\"{$_SERVER[PHP_SELF]}?page=" . ($current_page-1) . "\">Prev</A>";
  106.  
  107. if ($user_count == $record_limit)
  108.     echo "<BR /><A HREF=\"{$_SERVER[PHP_SELF]}?page=" . ($current_page+1) . "\">Next</A>";
  109.  
  110. include("__footer.php");
  111. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement