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

Creating Simple Auto Archive Data in PHP

Evolution

Giggle Architect
E Rep
0
0
0
Rep
0
E Vouches
0
0
0
Vouches
0
Posts
117
Likes
21
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Learn how to create Simple Auto Archive Data using PHP. An advanced PHP technique that can archive data when it reaches the due date. This code can be used to store the data that have submitted to the archive section to be able to re-read the detail of that data.

In this tutorial we will create a Simple Auto Archive Data 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 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_archive. After that, click Import then locate the database file inside the folder of the application then click ok.

2019-02-12_23_02_45-localhost_127.0.0.1_db_archive_phpmyadmin_4.8.3.png

You can also create the database tables programmatically. To do this, copy/paste

the code below into your PHPMyAdmin SQL Tab

of your newly created database. Then click the GO



  1. CREATE

    TABLE

    `archive`

    (
  2. `archive_

    id`

    int

    (

    11

    )

    NOT

    NULL

    ,
  3. `product_

    id`

    int

    (

    11

    )

    NOT

    NULL

    ,
  4. `product_

    code`

    varchar

    (

    50

    )

    NOT

    NULL

    ,
  5. `product_

    name`

    varchar

    (

    100

    )

    NOT

    NULL

    ,
  6. `description`

    text

    NOT

    NULL

    ,
  7. `date_

    archived`

    varchar

    (

    20

    )

    NOT

    NULL


  8. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  9. INSERT

    INTO

    `archive`

    (

    `archive_

    id`

    ,

    `product_

    id`

    ,

    `product_

    code`

    ,

    `product_

    name`

    ,

    `description`

    ,

    `date_

    archived`

    )

    VALUES


  10. (

    1

    ,

    1

    ,

    '6632'

    ,

    'Milk'

    ,

    'Milk'

    ,

    '2019-02-12'

    )

    ;

  11. CREATE

    TABLE

    `product`

    (
  12. `product_

    id`

    int

    (

    11

    )

    NOT

    NULL

    ,
  13. `product_

    code`

    varchar

    (

    5

    )

    NOT

    NULL

    ,
  14. `product_

    name`

    varchar

    (

    100

    )

    NOT

    NULL

    ,
  15. `description`

    text

    NOT

    NULL

    ,
  16. `due_

    date`

    varchar

    (

    20

    )

    NOT

    NULL


  17. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  18. INSERT

    INTO

    `product`

    (

    `product_

    id`

    ,

    `product_

    code`

    ,

    `product_

    name`

    ,

    `description`

    ,

    `due_

    date`

    )

    VALUES


  19. (

    1

    ,

    '8845'

    ,

    'Chocolate'

    ,

    'Drinks'

    ,

    '2019-02-15'

    )

    ;

  20. ALTER

    TABLE

    `archive`
  21. ADD

    PRIMARY KEY

    (

    `archive_

    id`

    )

    ;
  22. ALTER

    TABLE

    `product`
  23. ADD

    PRIMARY KEY

    (

    `product_

    id`

    )

    ;
  24. ALTER

    TABLE

    `archive`
  25. MODIFY

    `archive_

    id`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,

    AUTO_INCREMENT

    =

    2

    ;
  26. ALTER

    TABLE

    `product`
  27. MODIFY

    `product_

    id`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,

    AUTO_INCREMENT

    =

    2

    ;

Creating the database connection

Open your any kind of text editor such as notepad++. Then just copy/paste

the code below then save it as conn.php

.

  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "db_archive"

    )

    ;

  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 paste

it into your text editor, then save it as shown below.

index.php


  1. <!DOCTYPE html>
  2. <?php

    require

    'archive_query.php'

    ?>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Auto Archive Data</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add product</button>
  19. <a href="archive.php" class="pull-right">Archive</a>
  20. <br /><br />
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <th>Product Code</th>
  25. <th>Product Name</th>
  26. <th>Description</th>
  27. <th>Due Date</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;">
  31. <?php
  32. require

    'conn.php'

    ;

  33. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `product`"

    )

    or die

    (

    mysqli_error

    (

    $conn

    )

    )

    ;
  34. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  35. ?>
  36. <tr>
  37. <td><?php

    echo

    $fetch

    [

    'product_code'

    ]

    ?>

    </td>
  38. <td><?php

    echo

    $fetch

    [

    'product_name'

    ]

    ?>

    </td>
  39. <td><?php

    echo

    $fetch

    [

    'description'

    ]

    ?>

    </td>
  40. <td><?php

    echo

    $fetch

    [

    'due_date'

    ]

    ?>

    </td>
  41. </tr>
  42. <?php
  43. }
  44. ?>
  45. </tbody>
  46. </table>
  47. </div>
  48. <div class="modal fade" id="form_modal" aria-hidden="true">
  49. <div class="modal-dialog">
  50. <div class="modal-content">
  51. <form method="POST" action="save_product.php">
  52. <div class="modal-header">
  53. <h3 class="modal-title">Add Product</h3>
  54. </div>
  55. <div class="modal-body">
  56. <div class="col-md-2"></div>
  57. <div class="col-md-8">
  58. <div class="form-group">
  59. <label>Product Code</label>
  60. <input type="text" name="product_code" class="form-control" required="required"/>
  61. </div>
  62. <div class="form-group">
  63. <label>Product Name</label>
  64. <input type="text" name="product_name" class="form-control" required="required"/>
  65. </div>
  66. <div class="form-group">
  67. <label>Description</label>
  68. <input type="text" name="description" class="form-control" required="required" />
  69. </div>
  70. <div class="form-group">
  71. <label>Due Date</label>
  72. <input type="date" name="due_date" class="form-control" required="required" />
  73. </div>
  74. </div>
  75. </div>
  76. <div style="clear:both;"></div>
  77. <div class="modal-footer">
  78. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  79. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  80. </div>
  81. </div>
  82. </form>
  83. </div>
  84. </div>
  85. </div>
  86. <script src="js/jquery-3.2.1.min.js"></script>
  87. <script src="js/bootstrap.js"></script>
  88. </body>
  89. </html>

archive.php


  1. <!DOCTYPE html>
  2. <?php

    require

    'archive_query.php'

    ?>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Auto Archive Data</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <a href="index.php" class="pull-right">Main Page</a>
  19. <br /><br />
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Product Code</th>
  24. <th>Product Name</th>
  25. <th>Description</th>
  26. <th>Date Archived</th>
  27. </tr>
  28. </thead>
  29. <tbody style="background-color:#fff;">
  30. <?php
  31. require

    'conn.php'

    ;

  32. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `archive`"

    )

    or die

    (

    mysqli_error

    (

    $conn

    )

    )

    ;
  33. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  34. ?>
  35. <tr>
  36. <td><?php

    echo

    $fetch

    [

    'product_code'

    ]

    ?>

    </td>
  37. <td><?php

    echo

    $fetch

    [

    'product_name'

    ]

    ?>

    </td>
  38. <td><?php

    echo

    $fetch

    [

    'description'

    ]

    ?>

    </td>
  39. <td><?php

    echo

    $fetch

    [

    'date_archived'

    ]

    ?>

    </td>
  40. </tr>
  41. <?php
  42. }
  43. ?>
  44. </tbody>
  45. </table>
  46. </div>
  47. </body>
  48. </html>

Creating the Save Query

This code contains the PHP query of the application. This code will store the data inputs to the database server. To do that just copy

and paste

this block of codes inside the text editor, then save it as save_product.php

.

  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. $product_code

    =

    $_POST

    [

    'product_code'

    ]

    ;
  5. $product_name

    =

    $_POST

    [

    'product_name'

    ]

    ;
  6. $description

    =

    $_POST

    [

    'description'

    ]

    ;
  7. $due_date

    =

    $_POST

    [

    'due_date'

    ]

    ;

  8. $insert

    =

    mysqli_query

    (

    $conn

    ,

    "INSERT INTO `product` VALUES('', '$product_code

    ', '$product_name

    ', '$description

    ', '$due_date

    ')"

    )

    or die

    (

    mysqli_error

    (

    $conn

    )

    )

    ;
  9. if

    (

    $insert

    )
  10. header

    (

    "location: index.php"

    )

    ;
  11. }
  12. ?>

Creating the Main Function

This code contains the main function of the application. This code will move the file to the archive section when the data reach the end date. To do that just copy

and paste

this block of codes inside the text editor, then save it as archive_query.php

.

  1. <?php
  2. date_default_timezone_set

    (

    "Etc/GMT+8"

    )

    ;

  3. require_once

    'conn.php'

    ;

  4. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `product`"

    )

    ;
  5. $date

    =

    date

    (

    "Y-m-d"

    )

    ;
  6. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  7. if

    (

    strtotime

    (

    $fetch

    [

    'due_date'

    ]

    )

    <

    strtotime

    (

    $date

    )

    )

    {
  8. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `archive` VALUES('', '$fetch[product_id]

    ', '$fetch[product_code]

    ', '$fetch[product_name]

    ', '$fetch[description]

    ', '$fetch[due_date]

    ')"

    )

    or die

    (

    mysqli_error

    (

    $conn

    )

    )

    ;
  9. mysqli_query

    (

    $conn

    ,

    "DELETE FROM `product` WHERE `product_id` = '$fetch[product_id]

    '"

    )

    or die

    (

    mysqli_error

    (

    $conn

    )

    )

    ;
  10. }
  11. }
  12. ?>

DEMO

There you have it we successfully created Simple Auto Archive Data using PHP. I hope that this simple tutorial helps you to what you are looking for and you'll find it useful for your future PHP Projects. 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,496

335,022

335,030

Top