Tag Archives: PHP session

then you need create your class

here is the source code for your online stores class

<?php
class OnlineStore
{
    private $DBConnect = null;
    private $storeID = “”;
    private $inventory = array();
    private $shoppingCart = array();
    function __construct()
    {
        include(“inc_OnlineStoreDB.php”);
        $this->DBConnect = $DBConnect;
    }

    
    function __destruct()
    {
        if(!$this->DBConnect->connect_error)
        {
            $this->DBConnect->close();
        }
    }
    
    public function setStoreID($storeID)
    {
        if($this->storeID != $storeID)    
        {
            $this->storeID = $storeID;
            $string = “SELECT * FROM inventory WHERE storeID = ‘”.$this->storeID. “‘”;
            $result = @$this->DBConnect->query($string);
            if(!$result)
            {
                $this->storeID = “”;    
            }
            else
            {
                $this->inventory = array();
                $this->shoppingCart = array();
                while(($row = $result->fetch_assoc())!==null)    
                {
                    $this->inventory[$row[‘productID’]]=array();
                    $this->inventory[$row[‘productID’]][‘name’]=$row[‘name’];
                    $this->inventory[$row[‘productID’]][‘description’]=$row[‘description’];
                    $this->inventory[$row[‘productID’]][‘price’]=$row[‘price’];
                    $this->shoppingCart[$row[‘productID’]]=0;    
                }
            }
                
        }
        
    }
    
    public function getStoreInformation()
    {
        $retval = false;
        if($this->storeID != “”)    
        {
            $string = “SELECT * FROM store_info WHERE storeID = ‘”.$this->storeID . “‘”;
            $result = @$this->DBConnect->query($string);
            if($result)    
            {
                $retval = $result->fetch_assoc();    
            }
        }
        return($retval);
    }
    
    public function getProductList()
    {
        $retval = false;
        $subtotal = 0;
        if(count($this->inventory)>0)
        {
            echo “<table width=\”100%\”>”;
            echo “<tr><th>Product</th><th>Description</th><th>Price each</th><th># in Cart</th><th>Total Price</th><th>&nbsp;</th></tr>”;
            foreach($this->inventory as $ID => $Info)
            {
                echo “<tr><td>”    .htmlentities($Info[‘name’]).”</td>”;
                echo “<td>”.htmlentities($Info[‘description’]).”</td>”;
                printf(“<td class=\”currency\”>$%.2f</td>”,$Info[‘price’]);
                echo “<td class=’currency’>”.$this->shoppingCart[$ID].”</td>”;
                printf(“<td class=’currency’>$%.2f</td>”,$Info[‘price’]*$this->shoppingCart[$ID]);
                echo “<td><a href='”.$_SERVER[‘SCRIPT_NAME’].”?PHPSESSID=”.session_id() .”&ItemToAdd=$ID’>Add Item</a><br/>”;
                echo “<a href='”.$_SERVER[‘SCRIPT_NAME’].”?PHPSESSID=”.session_id() .”&ItemToRemove=$ID’>Remove Item</a></td>”;
                $subtotal += ($Info[‘price’] * $this->shoppingCart[$ID]);                
            }
            
            echo “<tr><td colspan=’4′>Subtotal</td>”;
            printf(“<td class=’currency’>$%.2f</td>”,$subtotal);
            echo “<td><a href='”.$_SERVER[‘SCRIPT_NAME’].”?PHPSESSID=”.session_id() .”&EmptyCart=true’>Empty Cart</a></td></tr>”;
            echo “</table>”;
            
        echo “<p align=’center’><a href=’Checkout.php?PHPSESSID=”.session_id().”&CheckOut=$storeID’>Checkout</a></p>”;    
        }
            
        return($retval);
    }
private function addItem()
{
    $ProdID = $_GET[‘ItemToAdd’];
    if (array_key_exists($ProdID,$this->shoppingCart))
    {
        $this->shoppingCart[$ProdID] += 1;    
    }
        
    
}

function __wakeup()
{
    include(“inc_OnlineStoreDB.php”);
    $this->DBConnect = $DBConnect;    
}

private function removeItem()
{
    $ProdID = $_GET[‘ItemToRemove’];
    if(array_key_exists($ProdID,$this->shoppingCart))
    {
        if($this->shoppingCart[$ProdID]>0)
            {
                $this->shoppingCart[$ProdID]-=1;                
            }
    }
    
}

private function emptyCart()
{
    foreach($this->shoppingCart as $key=>$value)
    {
        $this->shoppingCart[$key]=0;    
    }
    
}

public function processUserInput()
{
    if(!empty($_GET[‘ItemToAdd’]))
    {
        $this->addItem();    
    }
    if(!empty($_GET[‘ItemToRemove’]))
    {
        $this->removeItem();    
    }
    if(!empty($_GET[‘EmptyCart’]))
    {
        $this->emptyCart();    
    }
    
}

public function checkout()
{
        $ProductsOrdered = 0;
        foreach($this->shoppingCart as $productID =>$quantity)
        {
            if ($quantity > 0)
            {
                ++$ProductsOrdered;
                $SQLstring = “INSERT INTO orders ” .” (orderID, productID, quantity) ” .” VALUES(‘” . session_id() . “‘,” .”‘$productID’, $quantity)”;
                $QueryResult = $this->DBConnect->query($SQLstring);
            }
        }
        
        echo “<p><strong>Your order has been ” .”recorded.</strong></p>”;
}
}

?>

Use PHP session to create a online web survey program

Use PHP session to create a online web survey program

here is the source code.

you can go http://www.sandywebdesigner.com/con_str/web_survey.php see what it look like.

 

<?php
session_start();
$survey_questions = array(
1 => “Was the navigation straightforward and ” .” did all the links work?”,
2 => ” Was the selection of background color, ” .” font color, and font size appropriate?”,
3 => ” Were the images appropriate and did they ” .” complement the Web content?”,
4 => ” Were the descriptions of the PHP program ” .” complete and easy to understand?”,
5 => ” Was the PHP code structured properly and ” .” well commented?”);
$question_count = count($survey_questions);

if (isset($_SESSION[‘CurrentQuestion’]))
{
if (($_SESSION[‘CurrentQuestion’] > 0) &&(isset($_POST[‘response’])))
{
$_SESSION[‘Responses’][$_SESSION[‘CurrentQuestion’]]= $_POST[‘response’];
}
++$_SESSION[‘CurrentQuestion’];
}
else
$_SESSION[‘CurrentQuestion’] = 0;
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<meta name=”author” content=”Sandy Zhang” />
<meta name=”description” content=”A code demostration for php| chinese zodiac” />
<meta name=”keywords” content=”A code demostration for php| chinese zodiac” />
<title>Web Survey</title>
<style type=”text/css”>
body{

background-color:#bd4f39;
}
#main{
width:800px;
margin-left:auto;
margin-right:auto;
background-color:#dedbc8;
padding:15px;
}
</style>
</head>

<body>
<div id=”main”>
<h1>Web Survey</h1>
<?php
if ($_SESSION[‘CurrentQuestion’] == 0)
{
?>
<p>Thank you for reviewing the Chinese Zodiac Web site. Your can did responses to the following five questions will help improve the effectiveness of our PHP demonstration site.</p>
<?php
}
else
if ($_SESSION[‘CurrentQuestion’] > $question_count) {
?>
<p>thanks for completing the survey.</p>
<?php
}
else {
echo “<p>Question ” . $_SESSION[‘CurrentQuestion’] .”: ” . $survey_questions[$_SESSION[‘CurrentQuestion’]]. “</p>”;
}

if ($_SESSION[‘CurrentQuestion’] <= $question_count)
{
echo “<form method=’post’ action=’web_survey.php’>”;
echo “<input type=’hidden’ name=’PHPSESSID’ value=’ ” .session_id() . “‘ />”;
if ($_SESSION[‘CurrentQuestion’] > 0)
{
echo “<p><input type=’radio’ name=’response’ ” .” value=’Exceeds Expectations’ /> ” .” Exceeds Expectations<br />\n”;
echo “<input type=’radio’ name=’response’ ” .” value=’Meets Expectations'” .” checked=’checked’ /> ” .” Meets Expectations<br />\n”;
echo “<input type=’radio’ name=’response’ ” .” value=’Below Expectations’> ” .” Below Expectations</p>\n”;
}
echo “<input type=’submit’ name=’submit’ value='”;
if ($_SESSION[‘CurrentQuestion’] == 0)
echo “Start the survey”;
else if ($_SESSION[‘CurrentQuestion’] ==$question_count)
echo “Finished”;
else
echo “Next Question”;
echo “‘ />”;
echo “</form>”;
}
?>
</div>
</body>
</html>