• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

PHP - How To Add Session Array

Future

Low Latency Developer
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
123
Likes
186
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a How To Add Session Array using PHP. This code can be use for storing of list of data. 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...

Before we get 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 The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <?php
  3. session_start

    (

    )

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

    (

    ISSET

    (

    $_SESSION

    [

    'member'

    ]

    )

    )

    {
  33. foreach

    (

    $_SESSION

    [

    'member'

    ]

    as

    $key

    =>

    $value

    )

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

    echo

    $value

    [

    'firstname'

    ]

    ?>

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

    echo

    $value

    [

    'lastname'

    ]

    ?>

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

    echo

    $value

    [

    'address'

    ]

    ?>

    </td>
  39. </tr>
  40. <?php
  41. }
  42. }
  43. ?>
  44. </tbody>
  45. </table>
  46. </div>
  47. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  48. <div class="modal-dialog" role="document">
  49. <form action="save.php" method="POST">
  50. <div class="modal-content">
  51. <div class="modal-body">
  52. <div class="col-md-2"></div>
  53. <div class="col-md-8">
  54. <div class="form-group">
  55. <label>Firstname</label>
  56. <input class="form-control" type="text" name="firstname" required="required" />
  57. </div>
  58. <div class="form-group">
  59. <label>Lastname</label>
  60. <input class="form-control" type="text" name="lastname" required="required" />
  61. </div>
  62. <div class="form-group">
  63. <label>Address</label>
  64. <input class="form-control" type="text" name="address" required="required" />
  65. </div>
  66. </div>
  67. </div>
  68. <div style="clear:both;"></div>
  69. <div class="modal-footer">
  70. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  71. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  72. </div>
  73. </div>
  74. </form>
  75. </div>
  76. </div>
  77. </body>
  78. <script src="js/jquery-3.2.1.min.js"></script>
  79. <script src="js/bootstrap.js"></script>
  80. </html>

Creating the Main Function

This code contains the main function of the application. This code will store an array of data to session array. To do this just kindly copy and write these block of codes inside the text editor, then save it as save.php
  1. <?php
  2. session_start

    (

    )

    ;
  3. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. if

    (

    ISSET

    (

    $_SESSION

    [

    'member'

    ]

    )

    )

    {
  5. $member

    =

    array

    (
  6. 'firstname'

    =>

    $_POST

    [

    'firstname'

    ]

    ,
  7. 'lastname'

    =>

    $_POST

    [

    'lastname'

    ]

    ,
  8. 'address'

    =>

    $_POST

    [

    'address'

    ]
  9. )

    ;
  10. $id

    =

    count

    (

    $_SESSION

    [

    'member'

    ]

    )

    ;
  11. $_SESSION

    [

    'member'

    ]

    [

    $id

    ]

    =

    $member

    ;
  12. echo

    "<script>alert('Data Inserted')</script>"

    ;
  13. echo

    "<script>window.location='index.php'</script>"

    ;
  14. }

    else

    {
  15. $member

    =

    array

    (
  16. 'firstname'

    =>

    $_POST

    [

    'firstname'

    ]

    ,
  17. 'lastname'

    =>

    $_POST

    [

    'lastname'

    ]

    ,
  18. 'address'

    =>

    $_POST

    [

    'address'

    ]
  19. )

    ;

  20. $_SESSION

    [

    'member'

    ]

    [

    0

    ]

    =

    $member

    ;

  21. echo

    "<script>alert('Data Inserted')</script>"

    ;
  22. echo

    "<script>window.location='index.php'</script>"

    ;
  23. }
  24. }
  25. ?>

⚙ Live Demo

There you have it we successfully created How To Add Session Array 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.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,516

349,526

Top