• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Filter And Display Data By Weekly

boo122002

Decentralized Voting Advocate
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
101
Likes
176
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will create a Filter And Display Data By Weekly using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_week, after that click Import then locate the database file inside the folder of the application then click ok.
2018-09-19_13_48_04-localhost_127.0.0.1_db_week_phpmyadmin_4.7.0.png

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'db_week'

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-widht, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Filter And Display Data By Weekly</h3>
  16. <hr style="border-top;1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Stock</button>
  18. <br /><br />
  19. <form method="POST" class="form-inline" action="">
  20. <select name="month" class="form-control" required="required">
  21. <option value="">---Select a month---</option>
  22. <option value="1">January</option>
  23. <option value="2">February</option>
  24. <option value="3">March</option>
  25. <option value="4">April</option>
  26. <option value="5">May</option>
  27. <option value="6">June</option>
  28. <option value="7">July</option>
  29. <option value="8">August</option>
  30. <option value="9">September</option>
  31. <option value="10">October</option>
  32. <option value="11">November</option>
  33. <option value="12">December</option>
  34. </select>
  35. <select name="year" class="form-control" required="required">
  36. <option value="">---Select a year---</option>
  37. <?php
  38. for

    (

    $i

    =

    date

    (

    "Y"

    )

    ;

    $i

    >=

    1965

    ;

    $i

    --

    )

    {
  39. echo

    "<option value='"

    .

    $i

    .

    "'>"

    .

    $i

    .

    "</option>"

    ;
  40. }
  41. ?>
  42. </select>
  43. <button class="btn btn-primary" name="filter"><span class="glyphicon glyphicon-search"></span> Filter</button>
  44. </form>
  45. <br />
  46. <?php

    include

    'get_week.php'

    ?>
  47. </div>
  48. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  49. <div class="modal-dialog">
  50. <form action="save_stock.php" method="POST" enctype="multipart/form-data">
  51. <div class="modal-content">
  52. <div class="modal-body">
  53. <div class="col-md-3"></div>
  54. <div class="col-md-6">
  55. <div class="form-group">
  56. <label>Item Name</label>
  57. <input type="text" class="form-control" name="item_name" required="required"/>
  58. </div>
  59. <div class="form-group">
  60. <label>Brand Name</label>
  61. <input type="text" class="form-control" name="brand_name" required="required"/>
  62. </div>
  63. <div class="form-group">
  64. <label>Date</label>
  65. <input type="date" class="form-control" name="date" required="required"/>
  66. </div>
  67. </div>
  68. </div>
  69. <div style="clear:both;"></div>
  70. <div class="modal-footer">
  71. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  72. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  73. </div>
  74. </div>
  75. </form>
  76. </div>
  77. </div>
  78. <script src="js/jquery-3.2.1.min.js"></script>
  79. <script src="js/bootstrap.js"></script>
  80. </body>
  81. </html>

Creating PHP Query

This code contains the php query of the application. This code will save the data inputs to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save_stock.php.
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. $item_name

    =

    $_POST

    [

    'item_name'

    ]

    ;
  5. $brand_name

    =

    $_POST

    [

    'brand_name'

    ]

    ;
  6. $date

    =

    $_POST

    [

    'date'

    ]

    ;

  7. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `stock` VALUES('', '$item_name

    ', '$brand_name

    ', '$date

    ')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  8. header

    (

    'location: index.php'

    )

    ;
  9. }
  10. ?>

Creating the Main Function

This code contains the main function of the application. This code will organize the data by week to make it easily to view. To do this just copy and write the code below inside the text editor, then save it as get_week.php.
  1. <?php
  2. require

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'filter'

    ]

    )

    )

    {
  4. $month

    =

    $_POST

    [

    'month'

    ]

    ;
  5. $year

    =

    $_POST

    [

    'year'

    ]

    ;
  6. $months

    =

    array

    (

    "January"

    ,

    "February"

    ,

    "March"

    ,

    "April"

    ,

    "May"

    ,

    "June"

    ,

    "July"

    ,

    "August"

    ,

    "September"

    ,

    "October"

    ,

    "November"

    ,

    "December"

    )

    ;


  7. ?>
  8. <?php
  9. echo

    "<h3>"

    .

    $months

    [

    $month

    -

    1

    ]

    .

    " "

    .

    $year

    .

    "</h3>"

    ;
  10. ?>
  11. <ul class="nav nav-tabs">
  12. <li class="active"><a data-toggle="tab" href="#week1">Week 1</a></li>
  13. <li><a data-toggle="tab" href="#week2">Week 2</a></li>
  14. <li><a data-toggle="tab" href="#week3">Week 3</a></li>
  15. <li><a data-toggle="tab" href="#week4">Week 4</a></li>
  16. </ul>
  17. <div class="tab-content">
  18. <div id="week1" class="tab-pane fade in active" style="padding:10px;">
  19. <table class="table table-bordered">
  20. <thead class="alert-success">
  21. <tr>
  22. <th>Item Name</th>
  23. <th>Brand Name</th>
  24. </tr>
  25. </thead>
  26. <tbody style="background-color:#fff;">
  27. <?php
  28. $data

    =

    [

    ]

    ;
  29. $week

    =

    [

    ]

    ;
  30. $i

    =

    0

    ;
  31. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT WEEK(date) AS `week`, item_name, brand_name FROM `stock` WHERE YEAR(date) = '$year

    ' && MONTH(date) = '$month

    ' ORDER BY `week` ASC"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  32. while

    (

    $fetch

    =

    mysqli_fetch_assoc

    (

    $query

    )

    )

    {
  33. $week

    [

    $i

    ]

    =

    $fetch

    [

    'week'

    ]

    ;
  34. $data

    [

    $i

    ]

    =

    array

    (

    'week'

    =>

    $fetch

    [

    'week'

    ]

    ,

    'item_name'

    =>

    $fetch

    [

    'item_name'

    ]

    ,

    'brand_name'

    =>

    $fetch

    [

    'brand_name'

    ]

    )

    ;
  35. $i

    ++;
  36. }
  37. $array

    =

    array_values

    (

    array_unique

    (

    $week

    )

    )

    ;
  38. foreach

    (

    $data

    as

    $section

    =>

    $list

    )

    {
  39. if

    (

    $list

    [

    'week'

    ]

    ===

    $array

    [

    0

    ]

    )

    {
  40. ?>
  41. <tr>
  42. <td><?php

    echo

    $list

    [

    'item_name'

    ]

    ?>

    </td>
  43. <td><?php

    echo

    $list

    [

    'brand_name'

    ]

    ?>

    </td>
  44. </tr>
  45. <?php
  46. }

  47. }

  48. ?>

  49. </tbody>
  50. </table>
  51. </div>
  52. <div id="week2" class="tab-pane fade">
  53. <table class="table table-bordered">
  54. <thead class="alert-success">
  55. <tr>
  56. <th>Item Name</th>
  57. <th>Brand Name</th>
  58. </tr>
  59. </thead>
  60. <tbody style="background-color:#fff;">
  61. <?php
  62. $data

    =

    [

    ]

    ;
  63. $week

    =

    [

    ]

    ;
  64. $i

    =

    0

    ;
  65. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT WEEK(date) AS `week`, item_name, brand_name FROM `stock` WHERE YEAR(date) = '$year

    ' && MONTH(date) = '$month

    ' ORDER BY `week` ASC"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  66. while

    (

    $fetch

    =

    mysqli_fetch_assoc

    (

    $query

    )

    )

    {
  67. $week

    [

    $i

    ]

    =

    $fetch

    [

    'week'

    ]

    ;
  68. $data

    [

    $i

    ]

    =

    array

    (

    'week'

    =>

    $fetch

    [

    'week'

    ]

    ,

    'item_name'

    =>

    $fetch

    [

    'item_name'

    ]

    ,

    'brand_name'

    =>

    $fetch

    [

    'brand_name'

    ]

    )

    ;
  69. $i

    ++;
  70. }
  71. $array

    =

    array_values

    (

    array_unique

    (

    $week

    )

    )

    ;
  72. foreach

    (

    $data

    as

    $section

    =>

    $list

    )

    {
  73. if

    (

    $list

    [

    'week'

    ]

    ===

    $array

    [

    1

    ]

    )

    {
  74. ?>
  75. <tr>
  76. <td><?php

    echo

    $list

    [

    'item_name'

    ]

    ?>

    </td>
  77. <td><?php

    echo

    $list

    [

    'brand_name'

    ]

    ?>

    </td>
  78. </tr>
  79. <?php
  80. }

  81. }

  82. ?>

  83. </tbody>
  84. </table>
  85. </div>
  86. <div id="week3" class="tab-pane fade">
  87. <table class="table table-bordered">
  88. <thead class="alert-success">
  89. <tr>
  90. <th>Item Name</th>
  91. <th>Brand Name</th>
  92. </tr>
  93. </thead>
  94. <tbody style="background-color:#fff;">
  95. <?php
  96. $data

    =

    [

    ]

    ;
  97. $week

    =

    [

    ]

    ;
  98. $i

    =

    0

    ;
  99. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT WEEK(date) AS `week`, item_name, brand_name FROM `stock` WHERE YEAR(date) = '$year

    ' && MONTH(date) = '$month

    ' ORDER BY `week` ASC"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  100. while

    (

    $fetch

    =

    mysqli_fetch_assoc

    (

    $query

    )

    )

    {
  101. $week

    [

    $i

    ]

    =

    $fetch

    [

    'week'

    ]

    ;
  102. $data

    [

    $i

    ]

    =

    array

    (

    'week'

    =>

    $fetch

    [

    'week'

    ]

    ,

    'item_name'

    =>

    $fetch

    [

    'item_name'

    ]

    ,

    'brand_name'

    =>

    $fetch

    [

    'brand_name'

    ]

    )

    ;
  103. $i

    ++;
  104. }
  105. $array

    =

    array_values

    (

    array_unique

    (

    $week

    )

    )

    ;
  106. foreach

    (

    $data

    as

    $section

    =>

    $list

    )

    {
  107. if

    (

    $list

    [

    'week'

    ]

    ===

    $array

    [

    2

    ]

    )

    {
  108. ?>
  109. <tr>
  110. <td><?php

    echo

    $list

    [

    'item_name'

    ]

    ?>

    </td>
  111. <td><?php

    echo

    $list

    [

    'brand_name'

    ]

    ?>

    </td>
  112. </tr>
  113. <?php
  114. }

  115. }

  116. ?>

  117. </tbody>
  118. </table>
  119. </div>
  120. <div id="week4" class="tab-pane fade">
  121. <table class="table table-bordered">
  122. <thead class="alert-success">
  123. <tr>
  124. <th>Item Name</th>
  125. <th>Brand Name</th>
  126. </tr>
  127. </thead>
  128. <tbody style="background-color:#fff;">
  129. <?php
  130. $data

    =

    [

    ]

    ;
  131. $week

    =

    [

    ]

    ;
  132. $i

    =

    0

    ;
  133. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT WEEK(date) AS `week`, item_name, brand_name FROM `stock` WHERE YEAR(date) = '$year

    ' && MONTH(date) = '$month

    ' ORDER BY `week` ASC"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  134. while

    (

    $fetch

    =

    mysqli_fetch_assoc

    (

    $query

    )

    )

    {
  135. $week

    [

    $i

    ]

    =

    $fetch

    [

    'week'

    ]

    ;
  136. $data

    [

    $i

    ]

    =

    array

    (

    'week'

    =>

    $fetch

    [

    'week'

    ]

    ,

    'item_name'

    =>

    $fetch

    [

    'item_name'

    ]

    ,

    'brand_name'

    =>

    $fetch

    [

    'brand_name'

    ]

    )

    ;
  137. $i

    ++;
  138. }
  139. $array

    =

    array_values

    (

    array_unique

    (

    $week

    )

    )

    ;
  140. foreach

    (

    $data

    as

    $section

    =>

    $list

    )

    {
  141. if

    (

    $list

    [

    'week'

    ]

    ===

    $array

    [

    3

    ]

    )

    {
  142. ?>
  143. <tr>
  144. <td><?php

    echo

    $list

    [

    'item_name'

    ]

    ?>

    </td>
  145. <td><?php

    echo

    $list

    [

    'brand_name'

    ]

    ?>

    </td>
  146. </tr>
  147. <?php
  148. }

  149. }

  150. ?>

  151. </tbody>
  152. </table>
  153. </div>
  154. </div>


  155. <?php
  156. }

    else

    {
  157. ?>
  158. <table class="table table-bordered">
  159. <thead class="alert-success">
  160. <tr>
  161. <th>Item Name</th>
  162. <th>Brand Name</th>
  163. <th>Date</th>
  164. </tr>
  165. </thead>
  166. <tbody style="background-color:#fff;">
  167. <?php
  168. require

    'conn.php'

    ;

  169. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `stock` ORDER BY `date` ASC"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  170. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  171. ?>
  172. <tr>
  173. <td><?php

    echo

    $fetch

    [

    'item_name'

    ]

    ?>

    </td>
  174. <td><?php

    echo

    $fetch

    [

    'brand_name'

    ]

    ?>

    </td>
  175. <td><?php

    echo

    $fetch

    [

    'date'

    ]

    ?>

    </td>
  176. </tr>
  177. <?php
  178. }
  179. ?>
  180. </tbody>
  181. </table>
  182. <?php
  183. }
  184. ?>

There you have it we successfully created Filter And Display Data By Weekly Using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,736

324,744

Top