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

PHP - Simple Shopping Cart

maxos

Social Polling Specialist
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
89
Likes
128
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Simple Shopping Cart 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...

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 Database

Open your database web server then create a database name in it db_shopping, after that click Import then locate the database file inside the folder of the application then click ok.
2018-07-05_15_48_08-localhost_127.0.0.1_db_shopping_phpmyadmin_4.7.0_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

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "db_shopping"

    )

    ;

  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 shown below.

index.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="URF-8" name="viewport" content="width=device-width, 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-7 well">
  14. <h3 class="text-primary">PHP - Simple Shopping Cart</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  17. <br /><br />
  18. <?php
  19. require

    'conn.php'

    ;

  20. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `product` ORDER BY `product_price` DESC"

    )

    ;
  21. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  22. ?>
  23. <div class="pull-left" style="height:350px; width:220px; margin:8px; border:1px solid #000; padding:5px; border-radius:5px;">
  24. <center><img src="<?php

    echo

    $fetch

    [

    'image'

    ]

    ?>

    " height="150px" width="180px"/></center>
  25. <center><h4><strong><?php

    echo

    $fetch

    [

    'product_name'

    ]

    ?>

    </strong></h4></center>
  26. <center><h4 class="text-danger">&#8369; <?php

    echo

    number_format

    (

    $fetch

    [

    'product_price'

    ]

    )

    ?>

    </h4></center>
  27. <form method="POST" action="addcart.php?id=<?php

    echo

    $fetch

    [

    'product_id'

    ]

    ?>

    ">
  28. <input type="hidden" name="product_name" value="<?php

    echo

    $fetch

    [

    'product_name'

    ]

    ?>

    "/>
  29. <input type="hidden" name="product_price" value="<?php

    echo

    $fetch

    [

    'product_price'

    ]

    ?>

    "/>
  30. <center><input type="number" name="qty"/></center>
  31. <br />
  32. <center><button class="btn btn-success" name="add"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</button></center>
  33. </form>
  34. </div>
  35. <?php
  36. }
  37. ?>
  38. </div>
  39. <div class="col-md-5">
  40. <div class="alert alert-info">
  41. <h2>Orders</h2>
  42. </div>
  43. <table class="table table-bordered">
  44. <thead>
  45. <tr>
  46. <th>Product Name</th>
  47. <th>Price</th>
  48. <th>Qty</th>
  49. <th>Total</th>
  50. <th>Action</th>
  51. </tr>
  52. </thead>
  53. <tbody>
  54. <?php
  55. session_start

    (

    )

    ;

  56. if

    (

    !

    empty

    (

    $_SESSION

    [

    "cart"

    ]

    )

    )

    {
  57. $total

    =

    0

    ;
  58. foreach

    (

    $_SESSION

    [

    "cart"

    ]

    as

    $keys

    =>

    $values

    )

    {

  59. ?>
  60. <tr>
  61. <td><?php

    echo

    $values

    [

    'product_name'

    ]

    ?>

    </td>
  62. <td>&#8369; <?php

    echo

    number_format

    (

    $values

    [

    'product_price'

    ]

    )

    ?>

    </td>
  63. <td><?php

    echo

    $values

    [

    'qty'

    ]

    ?>

    </td>
  64. <td>&#8369; <?php

    echo

    number_format

    (

    $values

    [

    'qty'

    ]

    *

    $values

    [

    'product_price'

    ]

    )

    ?>

    </td>
  65. <td><center><a class="btn btn-danger" href="delete.php?id=<?php

    echo

    $values

    [

    'product_id'

    ]

    ?>

    "><span class="glyphicon glyphicon-remove"></span></a></center></td>
  66. </tr>
  67. <?php
  68. $total

    =

    $total

    +

    (

    $values

    [

    'qty'

    ]

    *

    $values

    [

    'product_price'

    ]

    )

    ;
  69. }
  70. ?>
  71. <tr>
  72. <td colspan="3" align="right">Total</td>
  73. <td>&#8369; <?php

    echo

    number_format

    (

    $total

    )

    ?>

    </td>
  74. <td><a href="purchase.php" class="btn btn-info"><span class="glyphicon glyphicon-ok"></span> Buy</a></td>
  75. </tr>
  76. <?php

  77. }
  78. ?>
  79. </tbody>
  80. </table>

  81. </div>
  82. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  83. <div class="modal-dialog" role="document">
  84. <form action="save_query.php" method="POST" enctype="multipart/form-data">
  85. <div class="modal-content">
  86. <div class="modal-body">
  87. <div class="col-md-2"></div>
  88. <div class="col-md-8">
  89. <div class="form-group">
  90. <label>Product Name</label>
  91. <input class="form-control" type="text" name="product_name">
  92. </div>
  93. <div class="form-group">
  94. <label>Product Price</label>
  95. <input class="form-control" type="number" name="product_price">
  96. </div>
  97. <div class="form-group">
  98. <label>Product Photo</label>
  99. <input class="form-control" type="file" name="product_image">
  100. </div>
  101. </div>
  102. </div>
  103. <div style="clear:both;"></div>
  104. <div class="modal-footer">
  105. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  106. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  107. </div>
  108. </div>
  109. </form>
  110. </div>
  111. </div>
  112. </body>
  113. <script src="js/jquery-3.2.1.min.js"></script>
  114. <script src="js/bootstrap.js"></script>
  115. </html>

purchase.php
  1. <!

    DOCTYPE html>
  2. <

    html lang=

    "en"

    >
  3. <

    head>
  4. <

    meta charset=

    "URF-8"

    name=

    "viewport"

    content=

    "width=device-width, 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 -

    Simple Shopping Cart</

    h3>
  16. <

    hr style=

    "border-top:1px dotted #ccc;"

    />
  17. <

    center><

    h1>

    Thank you for

    Shopping!</

    h1></

    center>
  18. <

    center><

    a href=

    "session.php"

    >

    Back to prdouct</

    a></

    center>
  19. </

    div>

  20. </

    body>
  21. </

    html>

Creating PHP Query

This code contains the php query of the application. This code will sent 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_query.php.
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. if

    (

    !

    empty

    (

    $_POST

    [

    'product_name'

    ]

    )

    &&

    !

    empty

    (

    $_POST

    [

    'product_price'

    ]

    )

    )

    {
  5. $product_name

    =

    addslashes

    (

    $_POST

    [

    'product_name'

    ]

    )

    ;
  6. $product_price

    =

    $_POST

    [

    'product_price'

    ]

    ;

  7. $file

    =

    explode

    (

    "."

    ,

    $_FILES

    [

    'product_image'

    ]

    [

    'name'

    ]

    )

    ;
  8. $ext

    =

    array

    (

    "png"

    ,

    "gif"

    ,

    "jpg"

    ,

    "jpeg"

    )

    ;

  9. if

    (

    in_array

    (

    $file

    [

    1

    ]

    ,

    $ext

    )

    )

    {
  10. $file_name

    =

    $file

    [

    0

    ]

    .

    '.'

    .

    $file

    [

    1

    ]

    ;
  11. $tmp_file

    =

    $_FILES

    [

    'product_image'

    ]

    [

    'tmp_name'

    ]

    ;
  12. $location

    =

    "upload/"

    .

    $file_name

    ;
  13. $new_location

    =

    addslashes

    (

    $location

    )

    ;

  14. if

    (

    move_uploaded_file

    (

    $tmp_file

    ,

    $location

    )

    )

    {
  15. $conn

    ->

    query

    (

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

    ', '$product_price

    ', '$new_location

    ')"

    )

    ;
  16. echo

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

    ;
  17. echo

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

    ;
  18. }

  19. }

    else

    {
  20. echo

    "<script>alert('File not available')</script>"

    ;
  21. echo

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

    ;
  22. }

  23. }

    else

    {
  24. echo

    "<script>alert('Please complete the required field!')</script>"

    ;
  25. }


  26. }
  27. ?>

Creating Shopping Cart

This code contains the main function of the application. This code will store the database data to the PHP session, to be able to manipulate as an array. To do that just copy and write this block of codes inside the text editor, then save it as shown below.

addcart.php
  1. <?php
  2. session_start

    (

    )

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'add'

    ]

    )

    )

    {
  4. if

    (

    ISSET

    (

    $_SESSION

    [

    'cart'

    ]

    )

    )

    {
  5. $product_array_id

    =

    array_column(

    $_SESSION

    [

    "cart"

    ]

    ,

    "item_id"

    )

    ;
  6. if

    (

    !

    in_array

    (

    $_GET

    [

    'id'

    ]

    ,

    $product_array_id

    )

    )

    {
  7. $count

    =

    count

    (

    $_SESSION

    [

    'cart'

    ]

    )

    ;
  8. $product_array

    =

    array

    (
  9. 'product_id'

    =>

    $_GET

    [

    'id'

    ]

    ,
  10. 'product_name'

    =>

    $_POST

    [

    'product_name'

    ]

    ,
  11. 'product_price'

    =>

    $_POST

    [

    'product_price'

    ]

    ,
  12. 'qty'

    =>

    $_POST

    [

    'qty'

    ]
  13. )

    ;
  14. $_SESSION

    [

    'cart'

    ]

    [

    $count

    ]

    =

    $product_array

    ;
  15. echo

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

    ;
  16. }

    else

    {
  17. echo

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

    ;
  18. }
  19. }

    else

    {
  20. $product_array

    =

    array

    (
  21. 'product_id'

    =>

    $_GET

    [

    'id'

    ]

    ,
  22. 'product_name'

    =>

    $_POST

    [

    'product_name'

    ]

    ,
  23. 'product_price'

    =>

    $_POST

    [

    'product_price'

    ]

    ,
  24. 'qty'

    =>

    $_POST

    [

    'qty'

    ]
  25. )

    ;
  26. $_SESSION

    [

    'cart'

    ]

    [

    0

    ]

    =

    $product_array

    ;

  27. echo

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

    ;
  28. }
  29. }
  30. ?>

delete.php
  1. <?php
  2. session_start

    (

    )

    ;
  3. if

    (

    ISSET

    (

    $_GET

    [

    'id'

    ]

    )

    )

    {
  4. foreach

    (

    $_SESSION

    [

    'cart'

    ]

    as

    $keys

    =>

    $values

    )

    {
  5. if

    (

    $values

    [

    'product_id'

    ]

    ==

    $_GET

    [

    'id'

    ]

    )

    {
  6. unset

    (

    $_SESSION

    [

    'cart'

    ]

    [

    $keys

    ]

    )

    ;
  7. echo

    "<script>alert('Product Remove')</script>"

    ;
  8. echo

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

    ;
  9. }
  10. }
  11. }
  12. ?>

session.php
  1. <?php
  2. session_start

    (

    )

    ;
  3. session_destroy

    (

    )

    ;
  4. header

    (

    'location: index.php'

    )

    ;
  5. ?>

⚙ Live Demo

There you have it we successfully created a Simple Shopping Cart 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 hidden text.
 

450,491

323,080

323,089

Top