Guest User

Untitled

a guest
Feb 9th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. <?php
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $dbname = "dashboard";
  6.  
  7. // Create connection
  8. $conn = mysqli_connect($servername, $username, $password, $dbname);
  9. // Check connection
  10. if (!$conn) {
  11. die("Connection failed: " . mysqli_connect_error());
  12. }
  13.  
  14. $sql = "SELECT p.id, p.company, p.status
  15. FROM product p
  16. where p.startDate between '2019-02-01' and '2019-02-08'";
  17. $result = mysqli_query($conn, $sql);
  18.  
  19. if (mysqli_num_rows($result) > 0) {
  20. // output data of each row
  21. echo "<table border='1'>
  22. <tr>
  23. <th>company</th>
  24. <th>total product count company wise</th>
  25. <th>total count with y status</th>
  26. <th>total count with approved status - NO</th>
  27. </tr>";
  28.  
  29. $totalProductCountCompanyWise = array();
  30. $countAllY = 0;
  31.  
  32. while($row = mysqli_fetch_array($result)) {
  33. $key = $row['company'];
  34. if(!array_key_exists($key,$totalProductCountCompanyWise)){
  35. $totalProductCountCompanyWise[$key] = 1;
  36. } else{
  37. $totalProductCountCompanyWise[$key] += 1;
  38. }
  39.  
  40. if($row['status'] == "y"){
  41. $countAllY++;
  42. }
  43.  
  44. $sql2 = "SELECT p.id, p.company, p.status , ps.approved
  45. FROM product p
  46. join productstatus ps on p.id = ps.id
  47. where p.company = '".$key."' and ps.id = '".$row['id']."' ";
  48. $result2 = mysqli_query($conn, $sql2);
  49. $countNO = 0;
  50. while($row2 = mysqli_fetch_array($result2)) {
  51. if($row2['approved'] == "no"){
  52. $countNO++;
  53. }
  54. }
  55.  
  56. $companyData = array(
  57. array("company" => $row['company'],
  58. "totalProductCountCompanyWise" => $totalProductCountCompanyWise[$key],
  59. "totalCountWithYStatus" => $row['company'],
  60. "totalCountWithApprovedStatusNO" => $row['company']
  61. )
  62. );
  63.  
  64.  
  65. echo "<tr>";
  66. echo "<td>" . $row['company'] . "</td>";
  67. echo "<td>" . $totalProductCountCompanyWise[$key] . "</td>";
  68. echo "<td>" . $countAllY . "</td>";
  69. echo "<td>" . $countNO . "</td>";
  70. echo "</tr>";
  71. }
  72. echo "</table>";
  73. }
  74. else {
  75. echo "0 results";
  76. }
  77. mysqli_close($conn);
  78.  
  79. //tables for reference -
  80. /*
  81. CREATE TABLE `product` (
  82. `id` int(11) NOT NULL,
  83. `company` varchar(255) NOT NULL,
  84. `productID` int(11) NOT NULL,
  85. `status` varchar(255) NOT NULL,
  86. `startDate` datetime NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  87. INSERT INTO `product` (`id`, `company`, `productID`, `status`, `startDate`)
  88. VALUES
  89. (1, 'ABC', 13245, 'y', '2019-02-01 00:00:00'),
  90. (2, 'amazon', 13215, 'n', '2019-02-02 00:00:00'),
  91. (3, 'google', 13345, 'y', '2019-02-03 00:00:00'),
  92. (4, 'amazon', 13145, 'y', '2019-02-04 00:00:00'),
  93. (5, 'amazon', 13145, 'y', '2019-02-04 00:00:00'),
  94. (6, 'google', 13188, 'n', '2019-02-07 00:00:00'),
  95. (7, 'IBM', 13177, 'n', '2019-02-08 00:00:00');
  96.  
  97. ALTER TABLE `product`
  98. ADD PRIMARY KEY (`id`);
  99. ALTER TABLE `product`
  100. MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
  101.  
  102. CREATE TABLE `productstatus` (
  103. `id` int(11) NOT NULL,
  104. `approved` varchar(11) NOT NULL
  105. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  106. INSERT INTO `productstatus` (`id`, `approved`)
  107.  
  108. VALUES(1, 'yes'),(2, 'yes'),(3, 'no'),
  109. (4, 'yes'),(5, 'no'),(6, 'yes'),(7, 'yes');
  110.  
  111. ALTER TABLE `productstatus`
  112. ADD PRIMARY KEY (`id`);
  113.  
  114. ALTER TABLE `productstatus`
  115. MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
  116. */
  117.  
  118. ?>
  119.  
  120. <?php
  121. echo "Output Should be like below - ";
  122. echo "</br>";
  123. echo "</br>";
  124. echo "<table border='1'>
  125. <tr>
  126. <th>company</th>
  127. <th>total product count company wise</th>
  128. <th>total count with y status</th>
  129. <th>total count with approved status - NO</th>
  130. </tr>
  131. <tr>
  132. <th>ABC</th>
  133. <th>1</th>
  134. <th>1</th>
  135. <th>0</th>
  136. </tr>
  137. <tr>
  138. <th>amazon</th>
  139. <th>3</th>
  140. <th>2</th>
  141. <th>1</th>
  142. </tr>
  143. <tr>
  144. <th>Google</th>
  145. <th>2</th>
  146. <th>1</th>
  147. <th>1</th>
  148. </tr>
  149. <tr>
  150. <th>IBM</th>
  151. <th>1</th>
  152. <th>0</th>
  153. <th>0</th>
  154. </tr>
  155. ";
  156. ?>
Add Comment
Please, Sign In to add comment