doupinge9055 2015-09-07 11:27 采纳率: 100%
浏览 36
已采纳

PHP购物车 - 添加变量

I am creating a PHP cart for a photography business. This is the 1st Ecommerce site I have attempted (Newbie)

I have managed to add products to the cart (stored in the session) (Products: Print, Keyring, Magnet ect)

However I also need to add the imageID to the session

Here is my code

$product_id = $_GET[id];     //the product id from the URL 
    $imageId = $_GET[imageid];   //the image id from the URL 
    $action     = $_GET[action]; //the action from the URL 

    switch($action) {   //decide what to do 

        case "add":
            $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id 
        break;

Which saves the product to the basket enter image description here

The cart.php url is shown as:

cart.php?imageid=83&id=12&action=add

Could anyone help/advise how I add the Image ID to the product?

Thanks

  • 写回答

1条回答 默认 最新

  • douzhi3667 2015-09-07 11:54
    关注

    This is assuming one image can be used by different products? Otherwise, you should simply be able to deduce which image to use based on the product ID:

    case "add":
        $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
        $_SESSION['images'][$product_id] = $imageId; //Will map the image ID to the product being added
    break;
    

    Anyway, without seeing your actual markup, adding the image to the basket should be something like:

    <basket element>
        <item element>
            <img src="path/to/images/<?=$_SESSION['images'][$product_id]?>.filetype" />
        </item>
    </basket>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答