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 .
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.
Once this page is loaded, it will automatically gets directed to the ‘Login’ page in 5 seconds. The code for this is…
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>    <!--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’.