Sunday, January 1, 2012

Web application

Hi guys..,
       This time i thought of showing you guys a web application which we have done for one of our group course works. Its a social network called Ibuddies .This website is being implemented with plain HTML with the aid of CSS, PHP, AJAX and JavaScript. This is not a highly technical product .The main goal was to improve our knowledge and skills of developing a website using basic methods and to design advanced features such as weekly polls, quizzes and shopping cart.
 Our main goal is to provide a network for our colleagues of Informatics Institute of Technology to be interconnected with each other and to share resources easily. We presumed that such a website would be useful and helpful especially for the software engineering students in IIT.
The users will be able to send messages to each other stay connected with their friends and buy products from IIT using online purchasing.
And also this website contains of weekly polls and quizzes on modules we are learning and thereby we can polish our knowledge about the subject by referring to the answers and the ratings given by the results.
The user can send their feedbacks about the website and rate our website. Our website contains the following main features
·         Sign Up
·         Login
·         Main Page
o   Shopping Cart
o   Messages
o   Add Firends
o   Quizzes
o   Developers
o   Site Map
o   Contact Us
 I'll show you guys some of the main pages and there source codes .
INTRO Page

The introduction page consists of our logo and the motto of the site. 2 buttons are also included directing to the ‘Login’ page and the ‘About Us’ page.

<html>
    <head>
        <title>Intro</title>
        <style type="text/css">

            body {
                background: url(images/intro.jpg) no-repeat;
                background-size: 100%;
                background-repeat:repeat;
                background-attachment:fixed;
                overflow:scroll;
            }
            #logo{
                height:283px;
                width:800px;
            }
        </style>
    </head>
    <body>
        <center>
            <div id="welcome">
            <font color="#ffffff">
                <h1>Welcome to....</h1>
                </font>
            </div>
            <div>
                <img id="logo" src="images/logo.png"/> <!--Logo-->
            </div>
            <div id="aboutUsIntro">
                <input type=button onClick="parent.location='abtus.html'"
                 value='About Us'>
            <!--link to about us-->
            </div>
            <br/>
            <div id="skipToLogin">
                <input type=button onClick="parent.location='login.html'"
                value='Skip to Login page'> <!--Link to login page-->
            </div>
        </center>
        <meta http-equiv="refresh" content="5;url=login.html">
           <!--Goes to login page after 5 seconds-->
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <footer>
            <center>
                <div style="position:relative;bottom:0;margin:0 0 0 0">
                    <a href="abtus.html"><font color="#FFFFFF">
                         About Us</font></a>
                    <a href="helpUs.html">
                        <font color="#FFFFFF">Help</font></a>
                    <br/>
                    <font color="#FFFFFF">This website and its content is copyright of Informatics Institute of Technology - IIT 2011. All rights reserved.</font>
                </div>
            </center>
        </footer>
        </body>
</html>


Once this page is loaded, it will automatically gets directed to the ‘Login’ page in 5 seconds. The code for this is…
            <meta http-equiv="refresh" content="5;url=login.html">


               
Login Page
Shown below is the login page of our site, i buddies.  This page is the parent page in our map. People cannot login to ibuddies without having an account .The logo is on the top of the page. In this page there are two forms. On the left hand side of the page there is a login form. If the user is already registered to ibuddies then he or she can log in to our site by entering the email address and password. If the user is new then he or she will have to create an account. Incase if the user has forgotten the password, that user can click on “Forgot Password” link which will lead to a form where the user can enter the details of the problem. Thes details will then get saved in the database.

<form name="login" method="get" action="checkUser.php" id="login"
                              onsubmit="return validateLogin()">
    <table border="0" width= "0" cellpadding = "5">
       <tr>
           <td>E-mail address :</td> 
           <td> <input type="text" name="emailAddy" size="25"/> 
           <br/> </td> </tr> 
           <tr> <td></td><td></td> </tr> 
           <tr> <td> Password :</td><td><input type="password" name="password"                  
           size="25"/> </br> </td> </tr> </table> 
           <input type="submit" value="Login"> </br> 
           <a href="forgotPass.php" onclick="lostPass()"> Forgot                Password? </a> 
           </br> 
</form>

           
Shopping Cart
The shopping cart contains 3 topics which are
·         Show Cart
·         Leisure Items
·         Clothing Items

 

Show Cart

The cart is shown by the ‘showCart.php’ in which it acquires data from the ‘cart’ table in ‘iBuddies’ database and are displayed in a table. The table uses a css file for styling. The background of the table also changed according to the grand total of the cart.

Leisure Items

‘leisure.php’ file prints out the table which include the all the leisure items in the shopping process.

Clothing Items

‘clothing.php’ file prints out the table which include the all the clothing items in the shopping process.

The ‘leisure.php’ and ‘clothing.php’ files contains items in which the user is able to select the product they want to buy and also their quantity and press “Add to cart” button. The items are placed in a table which also resides inside a form. The “Add to cart” button is a submit button which will validate the quantities and send the information of the table to ‘addToCart.php’ using the “get” method. The ‘get’ method is used because the processing of the form is idempotent. Both ‘leisure.php’ and ‘clothing.php’ contains the same css file used to style the table in them.
leisure.php

<html>

<head>

<link href="../css/stylesShop.css" rel="stylesheet" type="text/css" /> <!--A css file for the tables in the shopping cart-->

<script type="text/javascript">

function validateQuantities(){

//alert("Hello");

if(document.forms["leisure"]["carromDiscAmount"].value<1 && (document.forms["leisure"]["carromDisc"].checked)){

alert("Please put a valid quantity for Carrom Disc item");

return false;

}

else if((document.forms["leisure"]["cSetAmount"].value)<1 && (document.forms["leisure"]["carromSet"].checked)){

alert("Please put a valid quantity for Carrom Men item");

return false;

}

else if((document.forms["leisure"]["TTballAmount"].value)<1 && (document.forms["leisure"]["ttB"].checked)){

alert("Please put a valid quantity for Table Tennis Ball item");

return false;

}

else if((document.forms["leisure"]["TTrackAmount"].value)<1 && (document.forms["leisure"]["ttR"].checked)){

alert("Please put a valid quantity for Table Tennis Racket item");

return false;

}

else{

return true;

}

}

</script>

</head>

<body>

<br/>

<font size=8 face= arial color=black><center>Leisure items</center></font>

<br/>

<center>

<form action="showCart.php"> <!--Goes to showCart.php-->

<input type="submit" value="Show Cart"/>

</form>

<form name="leisure" action="addToCart.php" method="get" onsubmit="return validateQuantities()"> <!--addToCart.php used to add items to the cart-->

<table id="mytable" cellspacing="0" style="text-align:center">

<tr>

<th scope="row" class="spec"><b>Product Name</b></th>

<th scope="row" class="specalt"><b>Price</b></th>

<th scope="row" class="spec"><b>Quantity</b></th>

<th scope="row" class="specalt"><b>Buy</b></th>

</tr>

<tr>

<td>Carrom disc<br/>

<img src="images/carromDisc.jpg"/>

<td>Rs.500/=</td>

<td><input type="text" size="3" value="1" name="carromDiscAmount"/></td>

<td><input type="checkbox" name="carromDisc" value="carromD"/>Buy</td>

</tr>

<tr>

<td>Carrom Draft Set<br/>

<img src="images/carromMen.jpg"/></td>

<td>Rs.700/=</td>

<td><input type="text" size="3" value="1" name="cSetAmount"/></td>

<td><input type="checkbox" name="carromSet" value="carromSet"/>Buy</td>

</tr>

<tr>

<td>Table Tennis Ball<br/>

<img src="images/ttBall.jpg" width="300" height="191"/></td>

<td>Rs.800/=</td>

<td><input type="text" size="3" value="1" name="TTballAmount"/></td>

<td><input type="checkbox" name="ttB" value="ttBall"/>Buy</td>

</tr>

<tr>

<td>Table Tennis Racket Set<br/>

<img src="images/ttRacket.jpg"/></td>

<td>Rs.1500/=</td>

<td><input type="text" size="3" value="1" name="TTrackAmount"/></td>

<td><input type="checkbox" name="ttR" value="ttrack"/>Buy</td>

</tr>

</table>

<input type="submit" value="Add to Cart" name="addCart"/>

</form>

</center>

<br/>

<br/>

</body>

</html>

addToCart.php

<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php
    $con = mysql_connect("localhost", "root", "");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("ibuddies");    //connecting to ibuddies database
    //getting cart table
    $result = mysql_query("SELECT * FROM cart");
    $result1 = mysql_query("SELECT * FROM cart");
    $result2 = mysql_query("SELECT * FROM cart");
    $result3 = mysql_query("SELECT * FROM cart");
    $result4 = mysql_query("SELECT * FROM cart");
    $result5 = mysql_query("SELECT * FROM cart");
    $result6 = mysql_query("SELECT * FROM cart");
    
    //SHIRT
    $inCart="false";
    if($_GET['Tshirt'] == 'shirt')
    {   
        while($row = mysql_fetch_array($result))
        {        
            if($row['id']==$_COOKIE["id"] && $row['product_id']=="1001"){   //searching if the user has bought this item before using his id
                $quan=$row['quantity']+$_GET['shirtAmount'];    //calculating total quantity of that item
                $sql="UPDATE cart SET quantity = $quan  
                WHERE product_id = '1001'"; 
//update quantity of that product in the database
                $inCart="true";
                if (!mysql_query($sql,$con))
                {
                    die('Error: ' . mysql_error());
                }
                break;
            }
        }
        if($inCart=="false"){
            $sql="INSERT INTO cart (id, product_id, quantity) 
VALUES('$_COOKIE[id]','1001','$_GET[shirtAmount]')";
            if (!mysql_query($sql,$con))
            {
                die('Error: ' . mysql_error());
            }
        }
    }
    
    //WRIST
    $inCart="false";
    if($_GET['bracelet'] == 'wrist')
    {   
        while($row = mysql_fetch_array($result1))
        {        
            if($row['id']==$_COOKIE["id"] && $row['product_id']=="1002"){
                $quan=$row['quantity']+$_GET['wristAmount'];
                $sql="UPDATE cart SET quantity = $quan
                WHERE product_id = '1002'";
                $inCart="true";
                if (!mysql_query($sql,$con))
                {
                    die('Error: ' . mysql_error());
                }
                break;
            }
        }
        if($inCart=="false"){
            $sql="INSERT INTO cart (id, product_id, quantity) 
VALUES('$_COOKIE[id]','1002','$_GET[wristAmount]')";
            if (!mysql_query($sql,$con))
            {
                die('Error: ' . mysql_error());
            }
        }
    }
    //CAP
    $inCart="false";
    if(($_GET['cap'] == 'cap'))
    {   
        while($row = mysql_fetch_array($result2))
        {        
            if($row['id']==$_COOKIE["id"] && $row['product_id']=="1003"){
                $quan=$row['quantity']+$_GET['capAmount'];
                $sql="UPDATE cart SET quantity = $quan
                WHERE product_id = '1003'";
                $inCart="true";
                if (!mysql_query($sql,$con))
                {
                    die('Error: ' . mysql_error());
                }
                break;
            }
        }
        if($inCart=="false"){
            $sql="INSERT INTO cart (id, product_id, quantity) 
VALUES('$_COOKIE[id]','1003','$_GET[capAmount]')";
            if (!mysql_query($sql,$con))
            {
                die('Error: ' . mysql_error());
            }
        }
    }
    

    //CLOTHING DONE!!!
    $inCart="false";
    if(($_GET['carromDisc'] == 'carromD'))
    {   
        while($row = mysql_fetch_array($result3))
        {        
            if($row['id']==$_COOKIE["id"] && $row['product_id']=="2001"){
                $quan=$row['quantity']+$_GET['carromDiscAmount'];
                $sql="UPDATE cart SET quantity = $quan
                WHERE product_id = '2001'";
                $inCart="true";
                if (!mysql_query($sql,$con))
                {
                    die('Error: ' . mysql_error());
                }
                break;
            }
        }
        if($inCart=="false"){
            $sql="INSERT INTO cart (id, product_id, quantity) 
VALUES('$_COOKIE[id]','2001','$_GET[carromDiscAmount]')";
            if (!mysql_query($sql,$con))
            {
                die('Error: ' . mysql_error());
            }
        }
    }
    //Carrom Men
    $inCart="false";
    if(($_GET['carromSet'] == 'carromSet'))
    {   
        while($row = mysql_fetch_array($result4))
        {        
            if($row['id']==$_COOKIE["id"] && $row['product_id']=="2002"){
                $quan=$row['quantity']+$_GET['cSetAmount'];
                $sql="UPDATE cart SET quantity = $quan
                WHERE product_id = '2002'";
                $inCart="true";
                if (!mysql_query($sql,$con))
                {
                    die('Error: ' . mysql_error());
                }
                break;
            }
        }
        if($inCart=="false"){
            $sql="INSERT INTO cart (id, product_id, quantity) 
VALUES('$_COOKIE[id]','2002','$_GET[cSetAmount]')";
            if (!mysql_query($sql,$con))
            {
                die('Error: ' . mysql_error());
            }
        }
    }
    
    //Table Tennis
    $inCart="false";
    if(($_GET['ttB'] == 'ttBall'))
    {   
        while($row = mysql_fetch_array($result5))
        {        
            if($row['id']==$_COOKIE["id"] && $row['product_id']=="2003"){
                $quan=$row['quantity']+$_GET['TTballAmount'];
                $sql="UPDATE cart SET quantity = $quan
                WHERE product_id = '2003'";
                $inCart="true";
                if (!mysql_query($sql,$con))
                {
                    die('Error: ' . mysql_error());
                }
                break;
            }
        }
        if($inCart=="false"){
            $sql="INSERT INTO cart (id, product_id, quantity) 
VALUES('$_COOKIE[id]','2003','$_GET[TTballAmount]')";
            if (!mysql_query($sql,$con))
            {
                die('Error: ' . mysql_error());
            }
        }
    }
    
    //Rackets
        $inCart="false";
    if(($_GET['ttR'] == 'ttrack'))
    {   
        while($row = mysql_fetch_array($result6))
        {        
            if($row['id']==$_COOKIE["id"] && $row['product_id']=="2004"){
                $quan=$row['quantity']+$_GET['TTrackAmount'];
                $sql="UPDATE cart SET quantity = $quan
                WHERE product_id = '2004'";
                $inCart="true";
                if (!mysql_query($sql,$con))
                {
                    die('Error: ' . mysql_error());
                }
                break;
            }
        }
        if($inCart=="false"){
            $sql="INSERT INTO cart (id, product_id, quantity) 
VALUES('$_COOKIE[id]','2004','$_GET[TTrackAmount]')";
            if (!mysql_query($sql,$con))
            {
                die('Error: ' . mysql_error());
            }
        }
    }
    
    //LEISURE DONE!!!!
    mysql_close($con);
    echo "<meta http-equiv=\"refresh\" content=\"0; URL=showCart.php\">";   //showing Cart
?>


Description

The ‘addToCart.php’ accepts the values entered from ‘lesiure.php’ and ‘clothing.php’ and adds them to the ‘cart’ table. For each of the item, the code runs through the database searching whether that user has bought this item earlier before. If the user has bought that specific item before, the quantity of that row will get updated according the user input. If the he or she hasn’t bought that item before a new record will be created in the database with the quantity, id of the user and the product id. After the content is updated to the database, ‘showCart.php’ will get directly opened.

 showCart.php

<?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("ibuddies");        //connecting to ibuddies database
    $result = mysql_query("SELECT * FROM cart");    //creating queries from cart table
    $result1 = mysql_query("SELECT * FROM cart");
    $result2 = mysql_query("SELECT * FROM cart");
    $result3 = mysql_query("SELECT * FROM cart");
    $result4 = mysql_query("SELECT * FROM cart");
    $result5 = mysql_query("SELECT * FROM cart");
    $result6 = mysql_query("SELECT * FROM cart");
?>
<html>
    <head>
        <link href="../css/showCart.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript">
            //Validating Quantities
            function validateQuantities(){
                if(document.forms["cart"]["shirt"].value<1  && !(document.forms["cart"]["checkbox1"].checked)){
                    alert("Please put a valid quantity for iBuddies Shirt item");
                    return false;
                }
                else if(document.forms["cart"]["wrist"].value<1  && !(document.forms["cart"]["checkbox2"].checked)){
                    alert("Please put a valid quantity for iBuddies Wrist Band item");
                    return false;
                }
                else if(document.forms["cart"]["cap"].value<1  && !(document.forms["cart"]["checkbox3"].checked)){
                    alert("Please put a valid quantity for iBuddies Cap item");
                    return false;
                }
                else if(document.forms["cart"]["disc"].value<1  && !(document.forms["cart"]["checkbox4"].checked)){
                    alert("Please put a valid quantity for Carrom Disc item");
                    return false;
                }
                else if((document.forms["cart"]["men"].value)<1  && !(document.forms["cart"]["checkbox5"].checked)){
                    alert("Please put a valid quantity for Carrom Men item");
                    return false;
                }
                else if((document.forms["cart"]["ball"].value)<1  && !(document.forms["cart"]["checkbox6"].checked)){
                    alert("Please put a valid quantity for Table Tennis Ball item");
                    return false;
                }
                else if((document.forms["cart"]["rack"].value)<1  && !(document.forms["cart"]["checkbox7"].checked)){
                    alert("Please put a valid quantity for Table Tennis Racket item");
                    return false;
                }
                else{
                    return true;
                }
            }
        </script>
    </head>
    <body>
        <center>
            <font size=8 face= arial color=black><center>My Shopping Cart</center></font>
            <br/>
            <br/>
                <form name="cart" method="get" onsubmit="return validateQuantities()" action="updateCart.php" >
                <?php
                    echo "<table id=\"mytable\" cellspacing=\"0\" style=\"text-align:center\"><tr><th scope=\"row\" class=\"spec\"><b>Product Name</b></th><th scope=\"row\" class=\"specalt\"><b>Quantity</b></th><th scope=\"row\" class=\"spec\"><b>Remove Product</b></th><th  scope=\"row\" class=\"specalt\"><b>Price</b></th></b></tr>";
                    $quantity=0;
                    $price=0;
                    $totalPrice=0;
                    //Printing out the each item, their quantities and price, if they are bought
                    while($row = mysql_fetch_array($result))
                    {        
                        if($row['id']==$_COOKIE["id"] && $row['product_id']=="1001"){
                            $quantity=$row['quantity'];
                            $price=1500*$quantity;  //calculating the total price of that item
                        }
                    }
                    if($price!=0){
                        $totalPrice+=$price;    //calculating the total price of the cart
                        echo "<tr>";
                        echo "<td>iBuddies Shirt</td>";
                        echo "<td><input type=\"text\" value=".$quantity." name=\"shirt\"/></td>";  //printing the quantity
                        echo "<td><input type=\"checkbox\" name=\"checkbox1\" value=\"checked\"/></td>";
                        echo "<td>Rs.".$price."</td>";  //printing price
                        echo "</tr>";
                    }
                    $quantity=0;
                    $price=0;
                    while($row1 = mysql_fetch_array($result1))
                    {       
                        if($row1['id']==$_COOKIE["id"] && $row1['product_id']=="1002"){
                            $quantity=$row1['quantity'];
                            $price=200*$quantity;
                        }
                    }
                    if($price!=0){
                        $totalPrice+=$price;
                        echo "<tr>";
                        echo "<td>iBuddies Wristband</td>";
                        echo "<td><input type=\"text\" value=".$quantity." name=\"wrist\"/></td>";
                        echo "<td><input type=\"checkbox\" name=\"checkbox2\" value=\"checked\"/></td>";
                        echo "<td>Rs.".$price."</td>";
                        echo "</tr>";
                    }
                    
                    $quantity=0;
                    $price=0;
                    while($row = mysql_fetch_array($result2))
                    {        
                        if($row['id']==$_COOKIE["id"] && $row['product_id']=="1003"){
                            $quantity=$row['quantity'];
                            $price=400*$quantity;
                        }
                    }
                    if($price!=0){
                        $totalPrice+=$price;
                        echo "<tr>";
                        echo "<td>iBuddies Cap</td>";
                        echo "<td><input type=\"text\" value=".$quantity." name=\"cap\"/></td>";
                        echo "<td><input type=\"checkbox\" name=\"checkbox3\" value=\"checked\"/></td>";
                        echo "<td>Rs.".$price."</td>";
                        echo "</tr>";
                    }
                    $quantity=0;
                    $price=0;
                    while($row = mysql_fetch_array($result3))
                    {        
                        if($row['id']==$_COOKIE["id"] && $row['product_id']=="2001"){
                            $quantity=$row['quantity'];
                            $price=500*$quantity;
                        }
                    }
                    if($price!=0){
                        $totalPrice+=$price;
                        echo "<tr>";
                        echo "<td>Carrom Disc</td>";
                        echo "<td><input type=\"text\" value=".$quantity." name=\"disc\"/></td>";
                        echo "<td><input type=\"checkbox\" name=\"checkbox4\" value=\"checked\"/></td>";
                        echo "<td>Rs.".$price."</td>";
                        echo "</tr>";
                    }
                    $quantity=0;
                    $price=0;
                    while($row = mysql_fetch_array($result4))
                    {        
                        if($row['id']==$_COOKIE["id"] && $row['product_id']=="2002"){
                            $quantity=$row['quantity'];
                            $price=700*$quantity;
                        }
                    }
                    if($price!=0){
                        $totalPrice+=$price;
                        echo "<tr>";
                        echo "<td>Carrom Men</td>";
                        echo "<td><input type=\"text\" value=".$quantity." name=\"men\"/></td>";
                        echo "<td><input type=\"checkbox\" name=\"checkbox5\" value=\"checked\"/></td>";
                        echo "<td>Rs.".$price."</td>";
                        echo "</tr>";
                    }
                    
                    $quantity=0;
                    $price=0;
                    while($row = mysql_fetch_array($result5))
                    {        
                        if($row['id']==$_COOKIE["id"] && $row['product_id']=="2003"){
                            $quantity=$row['quantity'];
                            $price=800*$quantity;
                        }
                    }
                    if($price!=0){
                        $totalPrice+=$price;
                        echo "<tr>";
                        echo "<td>Table Tennis Ball</td>";
                        echo "<td><input type=\"text\" value=".$quantity." name=\"ball\"/></td>";
                        echo "<td><input type=\"checkbox\" name=\"checkbox6\" value=\"checked\"/></td>";
                        echo "<td>Rs.".$price."</td>";
                        echo "</tr>";
                    }
                    
                    $quantity=0;
                    $price=0;
                    while($row = mysql_fetch_array($result6))
                    {        
                        if($row['id']==$_COOKIE["id"] && $row['product_id']=="2004"){
                            $quantity=$row['quantity'];
                            $price=1500*$quantity;
                        }
                    }
                    if($price!=0){
                        $totalPrice+=$price;
                        echo "<tr>";
                        echo "<td>Table Tennis Rackets</td>";
                        echo "<td><input type=\"text\" value=".$quantity."  name=\"rack\"/></td>";
                        echo "<td><input type=\"checkbox\" name=\"checkbox7\" value=\"checked\"/></td>";
                        echo "<td>Rs.".$price."</td>";
                        echo "</tr>";
                    }
                    echo "<tr>";
                    echo "<td colspan=\"3\">Grand Price : </td>";
                    echo "<td>Rs. ". $totalPrice ."</td>";  //printing total price
                    echo "</tr>";
                    echo "<script type=\"text/javascript\">     
                    if(".$totalPrice." < 5000){
                        document.body.style.backgroundColor=\"#81DAF5\";
                    }
                    else if(".$totalPrice." < 10000){
                        document.body.style.backgroundColor=\"#F5D0A9\";
                    }
                    else if(".$totalPrice." < 15000){
                        document.body.style.backgroundColor=\"#D0A9F5\";
                    }
                    else if(".$totalPrice." < 25000){
                        document.body.style.backgroundColor=\"#F6CEE3\";
                    }
                    else {
                        document.body.style.backgroundColor=\"#D8D8D8\";
                    }
                    
                    </script>"  //background color change javascript according to the price.
                ?>
                
            </table>
            <br/>
            <pre><input type="submit" value="Update Cart"/></pre>   <!--To update the cart after changing the values or after removing-->
            <a href="clothing.php"> Go to Clothing items page..</a>&nbsp&nbsp&nbsp  <!--link to clothing items-->
            <a href="leisure.php"> Go to Leisure items page..</a>   <!--link to leisure items-->
            </form>
        </center>
    </body>
</html>

Description

The ‘showCart.php’ file will print out the cart of the items that the user has bought. For each item, there will be loop running through the ‘cart’ table, searching if that specific user has bought that item using the user’s id. If the user has bought that item then a row including the name of the product, quantity and the price will be printed on the table. At the same time the total price or the grand price is also calculated. Finally at the end of the table the grand total is then printed. The table in the file is also being styled with a css file.
The user can also add more quantities or remove a product from their shopping cart by using the textboxes in the table and by the checkbutton in the table. After making all the necessary changes the user should click on the “Update Cart” button which will validate the quantities of the items and updates the contents in the database. The “Update Cart” button will execute ‘updateCart.php’ file.

 updateCart.php

<?php
    $con = mysql_connect("localhost", "root", "");  
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("ibuddies");    //selecting the databse
    $result = mysql_query("SELECT * FROM cart");    //selecting the table
    
?>

<?php
    //CHANGE QUANTITIES
    $sql="UPDATE cart SET quantity = '$_GET[shirt]' WHERE product_id = 
'1001' AND id='$_COOKIE[id]'";
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    
    $sql="UPDATE cart SET quantity = '$_GET[wrist]'
    WHERE product_id = '1002'";
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    
    $sql="UPDATE cart SET quantity = '$_GET[cap]'
    WHERE product_id = '1003'";
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    
    $sql="UPDATE cart SET quantity = '$_GET[disc]'
    WHERE product_id = '2001'";
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    
    $sql="UPDATE cart SET quantity = '$_GET[men]'
    WHERE product_id = '2002'";
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    
    $sql="UPDATE cart SET quantity = '$_GET[ball]'
    WHERE product_id = '2003'";
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    
    $sql="UPDATE cart SET quantity = '$_GET[rack]'
    WHERE product_id = '2004'";
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    
    //REMOVE ITEMS
    if($_GET['checkbox1'] == 'checked'){
        $sql=("DELETE FROM cart WHERE id='$_COOKIE[id]' AND 
product_id='1001'");
        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
    }
    
    if($_GET['checkbox2'] == 'checked'){
        $sql=("DELETE FROM cart WHERE id='$_COOKIE[id]' AND 
product_id='1002'");
        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
    }
    
    if($_GET['checkbox3'] == 'checked'){
        $sql=("DELETE FROM cart WHERE id='$_COOKIE[id]' AND 
product_id='1003'");
        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
    }
    
    if($_GET['checkbox4'] == 'checked'){
        $sql=("DELETE FROM cart WHERE id='$_COOKIE[id]' AND 
product_id='2001'");
        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
    }
    
    if($_GET['checkbox5'] == 'checked'){
        $sql=("DELETE FROM cart WHERE id='$_COOKIE[id]' AND 
product_id='2002'");
        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
    }
    
    if($_GET['checkbox6'] == 'checked'){
        $sql=("DELETE FROM cart WHERE id='$_COOKIE[id]' AND 
product_id='2003'");
        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
    }
    
    if($_GET['checkbox7'] == 'checked'){
        $sql=("DELETE FROM cart WHERE id='$_COOKIE[id]' AND 
product_id='2004'");
        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
    }

echo "<meta http-equiv=\"refresh\" content=\"0; URL=showCart.php\">";   //show cart
    mysql_close($con);
?>

As in the above code, if the user changes the quantity in the ‘showCart.php’, that quantity of the item will get updated. If the user chooses to remove the item, that will also get updated in this file.
Example for updating a row;

$sql="UPDATE cart SET quantity = '$_GET[disc]' WHERE product_id = '2001'";
Example for deleting a row;
$sql=("DELETE FROM cart WHERE id='$_COOKIE[id]' AND product_id='2002'");

Once the database is updated, it will get directed to ‘showPoll.php’.

Shopping Cart


Clothing Items



Leisure Items


Cart



Below are some of the screen shots of our pages .im not going to explain them with there codes,








Also we have used cookies for this project,

What is a Cookie?
A cookie is often used to identify a user. It is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

How to set a cookie?

Sytax
<?php setcookie(name, value, expire, path, domain); ?>
Example
<?php setcookie("user", "Your Name", 0); ?>

How to retrieve a cookie?

Syntax
<?php
    $_COOKIE["user"];
?>

Example

<?php
if (isset($_COOKIE["user"]))
  echo $_COOKIE["user"];
?>



Hope you guys learned something from this ....:) 

No comments:

Post a Comment