• 2007-08-16

    php版带翻页的购物车代码(兼容ie&ff) - [技术空间]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://www.blogbus.com/lily64-logs/7865426.html

    写了两个文件,实现php版带翻页的购物车功能。存档。

    <title>books info</title>
    <style>
    body{background:silver}
    table{font-size:15pt}
    form{margin:0}
    </style>
    <table border=1 cellspacing=0 cellpadding=5 width=800>
    <s c r i p t>
    win=null;
    function mysubmit()
    {
    if(win==null)
    win=window.open("","cartwin","");
    }
    mydata="name1,pic1,10.0;name2,pic2,22;name3,pic3,6;name4,pic4,25.5;name5,pic5,29;name6,pic6,19.8;name7,pic7,38.2;name8,pic8,18.8";
    cols=3;
    count=mydata.split(";").length;
    k=0;
    for(i=0;i<Math.ceil(count/cols);i++)
    {
    document.write("<tr>");
    for(j=0;j<cols;j++)
    {
    document.write("<td>");
    if(k<count)
    document.write("<form action='cart.php' target='cartwin' method='post' onsubmit=mysubmit()>"+mydata.split(";")[k].split(",")[1]+"<br><input type=text name=bookname style='border:none;font-size:15pt;background:silver' readonly value='"+mydata.split(";")[k].split(",")[0]+"'><br><input type=text name=price align=bottom style='border:none;font-size:15pt;background:silver' readonly value='¥"+mydata.split(";")[k].split(",")[2]+"'><br><input type=submit value=buy onclick=mysubmit()><input type=hidden name=myid value="+k+++"></form>");
    else
    document.write("&nbsp;");
    document.write("</td>");
    }
    document.write("</tr>");
    }
    </s c r i p t>
    </table>

    cart.php
    <title>shopping cart</title>
    <style>
    body{background:silver}
    table{font-size:15pt}
    div{font-size:15pt}
    form{margin:0}
    </style>
    <s c r i p t>
    function mycheck(obj)
    {
    if(obj.value==obj.parentNode.lastChild.value)
    return false;
    if(!(!isNaN(obj.value)&&(parseInt(obj.value)==obj.value)&&obj.value>0))
    {
    obj.value=obj.parentNode.lastChild.value;
    obj.focus();
    obj.select();
    return false;
    }
    obj.parentNode.childNodes[2].value="change";
    return true;
    }
    function mysubmit(obj)
    {
    if(obj.value=="del")
    {
    if(!confirm("are you sure?"))
    return false;
    }
    else if(obj.value=="change")
    {
    if(!mycheck(obj.parentNode.previousSibling.firstChild.firstChild))
    return false;
    }
    else
    {
    if(!mycheck(obj.parentNode.firstChild))
    return false;
    }
    obj.parentNode.previousSibling.firstChild.childNodes[2].value=obj.value;
    obj.parentNode.previousSibling.firstChild.submit();
    }
    function myclear(obj)
    {
    if(!confirm("are you sure?"))
    return;
    obj.previousSibling.value="clear";
    document.getElementById("myform").submit();
    }
    </s c r i p t>
    <body onload=window.focus()>
    <?php

    session_start();

    @$myid=$_POST["myid"];

    @$pageno=$_POST["pageno"];

    @$opertype=$_POST["opertype"];

    $pagesize=2;

    if("clear"==$opertype)
    unset($_SESSION["shop"]);
    else
    {

    if(""!=$myid)
    {

    @$number=$_POST["number"];

    $findbook=false;

    if(@is_array($_SESSION["shop"]))

    foreach ($_SESSION["shop"] as $i => &$value) {

     $key = array_search($myid, $value);

     if($key===0)
     {
      if(""==$opertype)
      {
           $value[3]++;
       $pageno=ceil(($i+1)/$pagesize);
      }
      else if("change"==$opertype)
      {
       $value[3]=$number;

       echo <<< Jsalert
    <script>
    document.write("<div style='position:absolute;left:300px;background:yellow;border:1px solid gray' align=center>be changed!<br><input type=button value=close onclick=\"this.parentNode.style.display='none'\" id='closebtn'></div><script>function myclose(){document.getElementById('closebtn').click();}setTimeout('myclose()',2000);</"+"script>");
    </script>
    Jsalert;
      }
      else if("del"==$opertype)
      {
       unset($_SESSION["shop"][$i]);
       $_SESSION["shop"] = array_values($_SESSION["shop"]);
      }
     $findbook=true;
     break;
     }
    }

    if(!$findbook&&""==$opertype)
    {
    @$bookname=$_POST["bookname"];
    @$price=$_POST["price"];
    $price=substr($price,2);
    $_SESSION["shop"][]=array($myid,$bookname,$price,"1");
    $pageno=ceil(count($_SESSION["shop"])/$pagesize);
    }

    }
    else if(""==$pageno)
    exit;

    }

    echo "<table width=700 border=1 cellspacing=0 cellpadding=5><tr><td>book name</td><td>price</td><td>number</td><td>operation</td></tr>";

    if($pageno<1)
    $pageno=1;
    else if($pageno>@ceil(count($_SESSION["shop"])/$pagesize))
    $pageno=@ceil(count($_SESSION["shop"])/$pagesize);

    $total=0;
    if(@is_array($_SESSION["shop"]))
    {
    foreach ($_SESSION["shop"] as $i => &$value)
    $total+=$value[2]*$value[3];
    }

    $currenttotal=0;
    for($i=$pagesize*($pageno-1);$i>=0&&$i<@count($_SESSION["shop"])&&$i<$pagesize*$pageno;$i++)
    {
    echo "<tr><td>".$_SESSION["shop"][$i][1]."</td><td>¥".$_SESSION["shop"][$i][2]."</td><td><form method=post onsubmit='return mysubmit(this.firstChild)'><input type=text style='border:1px solid;font-size:15pt;background:silver' name=number onkeyup=mycheck(this) onblur=mycheck(this) onchange=mycheck(this) value='".$_SESSION["shop"][$i][3]."'><input type=hidden name=myid value='".$_SESSION["shop"][$i][0]."'><input type=hidden name=opertype><input type=hidden name=pageno value=$pageno><input type=hidden value='".$_SESSION["shop"][$i][3]."'></form></td><td><input type=button value=change onclick=mysubmit(this)>&nbsp;<input type=button value=del onclick=mysubmit(this)></td></tr>";

    $currenttotal+=$_SESSION["shop"][$i][2]*$_SESSION["shop"][$i][3];
    }

    echo "</table><p><table width=700><tr><td>current total: ¥$currenttotal</td><td>pages:</td>";

    for($i=1;$i<=@ceil(count($_SESSION["shop"])/$pagesize);$i++)
    {
    echo "<td><form method=post><input type=hidden name=pageno value=$i><input type=submit style='border:none";
    if($pageno==$i)
    echo ";color:red";
    echo "' value=' $i '></form></td>";
    }
    echo "</tr></table></p><p><form method=post id=myform><table width=700><tr><td>total prices: ¥$total</td><td><input type=button value=pay></td><td align=right><input type=hidden name=opertype><input type=button value='clear all' onclick=myclear(this)></td></tr></table></form></p>";

    ?>
    </body>

    分享到:

    评论

  • 欢迎多交流。
  • 代码不错,很简洁,谢了啊!!!
  • 呵呵,不客气,共享是网络的精神嘛。欢迎多交流。
  • 谢谢LZ提供,无私共享~~!!!