Advertisement
cdsatrian

Adjecency List Category Count

Feb 8th, 2017
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.52 KB | None | 0 0
  1. <?php
  2. /*
  3. CREATE DATABASE IF NOT EXISTS test;
  4. USE test;
  5.  
  6. DROP TABLE IF EXISTS items;
  7. CREATE TABLE items(
  8.     id INT AUTO_INCREMENT PRIMARY KEY,
  9.     nama VARCHAR(50),
  10.     id_category INT
  11. );
  12.  
  13. INSERT INTO items(nama,id_category)
  14. VALUES
  15. ('innova',5),
  16. ('honda beat',3),
  17. ('lancer evo',4),
  18. ('honda vario',3),
  19. ('nmax',3),
  20. ('truck elf',6),
  21. ('truck gandeng',6);
  22.  
  23. SELECT * FROM items;
  24. +----+---------------+-------------+
  25. | id | nama          | id_category |
  26. +----+---------------+-------------+
  27. |  1 | innova        |           5 |
  28. |  2 | honda beat    |           3 |
  29. |  3 | lancer evo    |           4 |
  30. |  4 | honda vario   |           3 |
  31. |  5 | nmax          |           3 |
  32. |  6 | truck elf     |           6 |
  33. |  7 | truck gandeng |           6 |
  34. +----+---------------+-------------+
  35. 7 rows in set (0.00 sec)
  36.  
  37. DROP TABLE IF EXISTS category;
  38. CREATE TABLE category(
  39.     id INT AUTO_INCREMENT PRIMARY KEY,
  40.     nama VARCHAR(50),
  41.     parent INT
  42. );
  43.  
  44. INSERT INTO category(nama,parent)
  45. VALUES
  46. ('kendaraan',null),
  47. ('mobil',1),
  48. ('motor',1),
  49. ('sedan',2),
  50. ('suv',2),
  51. ('truck',2);
  52.  
  53. SELECT * FROM category;
  54. +----+-----------+--------+
  55. | id | nama      | parent |
  56. +----+-----------+--------+
  57. |  1 | kendaraan |   NULL |
  58. |  2 | mobil     |      1 |
  59. |  3 | motor     |      1 |
  60. |  4 | sedan     |      2 |
  61. |  5 | suv       |      2 |
  62. |  6 | truck     |      2 |
  63. +----+-----------+--------+
  64. 6 rows in set (0.00 sec)
  65. */
  66. $dbhost='localhost';
  67. $dbuser='root';
  68. $dbpass='';
  69. $dbname='test';
  70. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  71. $sql="
  72.     SELECT
  73.         a.*,SUM(IF(b.id IS NULL,0,1)) AS jml
  74.     FROM
  75.         category a
  76.         LEFT JOIN items b ON a.id=b.id_category
  77.     GROUP BY a.id
  78.     ORDER BY a.id";
  79. $result=$db->query($sql);
  80. $data=array();
  81. $index = array();
  82. while($row=$result->fetch_object()){
  83.     $id = $row->id;
  84.     $parent_id = $row->parent===null ? null : $row->parent;
  85.     $data[$id] = $row;
  86.     $index[$parent_id][] = $id;
  87. }
  88. function bottom_up($parent_id)
  89. {
  90.     global $data, $index;
  91.     $parent_id = $parent_id===null? null : $parent_id;
  92.     if (isset($index[$parent_id])) {
  93.         foreach ($index[$parent_id] as $id) {
  94.             bottom_up($id);
  95.             if($parent_id!==null) $data[$parent_id]->jml+=$data[$id]->jml;
  96.         }
  97.     }
  98. }
  99. bottom_up(NULL);
  100. ?>
  101. <!doctype html>
  102. <html>
  103. <head>
  104. <title>Latihan 2017-02-09</title>
  105. </head>
  106. <body>
  107. <table border='1'>
  108. <tr><th>Nama</th><th>Jumlah</th></tr>
  109. <?php
  110. foreach($data as $d)
  111.     echo "<tr><td>{$d->nama}</td><td>{$d->jml}</td></tr>";
  112. ?>
  113. </table>
  114. </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement