2010年2月18日木曜日

store.php?stor=1の処理

「store.php」
<?php
//共通クラスの呼び出し
require_once("Product.class.php");
//Smartyの呼び出し
require_once("MySmarty.class.php");
//セッションのスタート
session_start();
// MySmartyの「$o_smarty」に「getProducts」クラスを呼び出して取得した、
//($_GET['stor'])を「store.tpl」に渡しています
$o_smarty=new MySmarty();
$o_smarty->assign("products",Product::getProducts($_GET['stor']));
$o_smarty->display("store.tpl");
?>



「store.tpl」では、上記の($_GET['stor'])の値を獲得し、

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
<title>商品選択</title>
<!-- {literal} -->
<script language="JavaScript" src="./CheckUtil.js"></script>
<script language="JavaScript">
<!-- 各商品の購入個数の範囲をチェック -->
<!--
function chk(part){
strErr="";
    strErr+=rangeCheck(part.value,999,0,"個数");
    if(strErr==""){
        return true;
    }else{
        window.alert(strErr);
        return false;
    }
}
//-->
</script>
<!-- {/literal} -->
</head>
<body>
<form method="POST" action="storeCart.php" name="fm" onsubmit="return chk()">
<!-- <form method="POST" action="storeCart.php"> -->
<input type="submit" value="カートに入れる" />
<input type="reset" value="戻す" />
<table border="0" align="left" width="800" cellspacing="15">
<tr>
//Smatyの構文でproductの情報があるだけループさせます。
<!-- {foreach name=loop from=$products item=product} -->
    <td valign="top">
    <table border="1" height="180" bordercolordark="#FFffFF">
    <tr>
    <td valign="top">
<!--         {if $product->getPicture() eq ""}  -->
            <img src=".img/welcome.gif" alt="{$product->getName()}(画像なし)"
                width="100" height="100" />
<!--        {else} -->
            <img src="./img/{$product->getPicture()}"
                alt="{$product->getName()}"
                height="100" width="100" />
<!--        {/if} -->
    </td>
    <td valign="top" bgcolor="#ffffee">
        <div style="font-weight:bold;">{$product->getName()|escape}</div>
        <div align="right">
        ({$product->getPrice()}円)</div>
        <hr />
        {$product->getInfo()|escape}
        <br />
        <div align="right">
            <input type="text" name="num{$smarty.foreach.loop.iteration}"
                value="{$product->getNumber()|default:"0"}"
                size="4" maxlength="3" style="text-align:right;ime-mode:disabled;"
                onchange="return chk(this)" />個</div>
            <input type="hidden" name="code{$smarty.foreach.loop.iteration}"
                value="{$product->getCode()}" />
    </td>
    </tr>
    </table>
    </td>
        </tr><tr>
<!-- {/foreach} -->
</table>
<input type="hidden" name="cnt" value="{$smarty.foreach.loop.total}" />
</form>
</body>
</html>




//「Product.class.php」はProductを呼び出す「store.php」に商品テーブルの情報を渡す値を処理//しています。

<?php
class Product {
    public function __construct(){ /* コンストラクタ */ }
    /* 商品情報を格納するプライベート変数を宣言 */
    private $_code;
    private $_name;
    private $_price;
    private $_picture;
    private $_info;
    private $_number;
    /* プライベート変数にアクセスするためのアクセサメソッドを宣言 */
    public function getCode()   {return $this->_code;}
    public function getName()   {return $this->_name;}
    public function getPrice()  {return $this->_price;}
    public function getPicture(){return $this->_picture;}
    public function getInfo()   {return $this->_info;}
    public function getNumber() {return $this->_number;}
    public function setCode($code)      {$this->_code=$code;}
    public function setName($name)      {$this->_name=$name;}
    public function setPrice($price)    {$this->_price=$price;}
    public function setPicture($picture){$this->_picture=$picture;}
    public function setInfo($info)      {$this->_info=$info;}
    public function setNumber($number)  {$this->_number=$number;}
    /* getProductsメソッドは、指定された店舗IDをキーに商品情報を検索 *
     * その結果をProductオブジェクトの配列として返す                 */

    public static function getProducts($store){
//商品情報を入れる配列を準備し、
        $aryProduct=array();
        $db=new mysqli("localhost","dbpal","password","dbpal");
        //MySQL4.1への対応
        $db->query("SET NAMES UJIS");
//sql文は店舗番号に当てはまる商品情報を取り出す。
        $rs=$db->query("SELECT * FROM ec_product WHERE stor=".$store);
//取り出した商品情報を一つ一つループします。
        while($row=$rs->fetch_array(MYSQLI_ASSOC)){
//「$objPro」の中に格納し 「$aryProduct[]」渡し、
            $objPro=new Product();
            $objPro->setCode($row['code']);
            $objPro->setName($row['nam']);
            $objPro->setPrice($row['pric']);
            $objPro->setPicture($row['pic']);
            $objPro->setInfo($row['info']);
            $objPro->setNumber($_SESSION[$row['code']]);
            $aryProduct[]=$objPro;
        }
        $db->close();
//最後に呼び出し元である「store.php」に情報を渡します。
        return $aryProduct;
    }
    public static function getProductsInCart(){
        $aryProduct=array();
        $db=new mysqli("localhost","dbpal","password","dbpal");
        //MySQL4.1への対応
        $db->query("SET NAMES UJIS");

        $rs=$db->query("SELECT * FROM ec_product ORDER BY code ASC");
        while($row=$rs->fetch_array(MYSQLI_ASSOC)){
            $code=$row['code'];
            if($_SESSION[$code]!="" && $_SESSION[$code]!=0){
                $objPro=new Product();
                $objPro->setCode($row['code']);
                $objPro->setName($row['nam']);
                $objPro->setPrice($row['pric']);
                $objPro->setNumber($_SESSION[$row['code']]);
                $aryProduct[]=$objPro;
            }
        }
        $db->close();
        return $aryProduct;
    }
}
?>

0 件のコメント:

コメントを投稿