dongxian6653 2014-11-12 17:25
浏览 63
已采纳

通过单击图像将值加1到值 - PHP

I guys, I need yo help for this. I have this code, wich shows me the products I have in my shopping cart:

for($i=0;$i<count($_SESSION['productos']);$i++)
{
    $id = $_SESSION['productos'][$i];
    $prods = mysql_query('SELECT * from productos where idprod='.$id.'');
    $row = mysql_fetch_array($prods);
        echo "<tr>";
        echo "<td>" . $row['nombre']; echo "</td>";

        if($_GET[action]=="suma")
        {
         $_SESSION['unidades'][$i] =  $_SESSION['unidades'][$i] + 1;
        }
        elseif($_GET[action]=="resta")
        {
        $_SESSION['unidades'][$i] =  $_SESSION['unidades'][$i] - 1;
        }

        echo "<td><input name=".$i." type='text' value=" . $_SESSION['unidades'][$i]; echo " size='5'/></td>";  
        echo "<td><a href='carro_detalle.php?action=suma'><img src='images/flecharriba.png' width='10x' height='10px'/></a></td>";
        echo "<td><a href='carro_detalle.php?action=resta'><img src='images/flechabajo.png' width='10px' height='10px'/></a></td>";
        echo "<td>" . $row['precio']; echo "</td>";
        echo "<td>" . $row['precio'] * $_SESSION['unidades'][$i]; echo "</td>";
            echo "</tr>";
}

I need to sum 1 unity or rest 1 unity to $_SESSION['unidades'][$i] just to the selected product, when I click the two images respectively. The thing is that when I click, it adds me 1 to all the products. Any easy way of doing this without using $_GET vars? I dont have high knowledge about JavasScript. Thanks!!

  • 写回答

1条回答 默认 最新

  • dongyuhui0418 2014-11-12 17:33
    关注

    You probably should remove your condition from the loop and put it outside of it:

    if($_GET[action]=="suma")
    {
        $_SESSION['unidades'][$i] =  $_SESSION['unidades'][$i] + 1;
    }
    elseif($_GET[action]=="resta")
    {
        $_SESSION['unidades'][$i] =  $_SESSION['unidades'][$i] - 1;
    }
    
    
    for($i=0;$i<count($_SESSION['productos']);$i++)
    {
    $id = $_SESSION['productos'][$i];
    $prods = mysql_query('SELECT * from productos where idprod='.$id.'');
    $row = mysql_fetch_array($prods);
        echo "<tr>";
        echo "<td>" . $row['nombre']; echo "</td>";
    
        echo "<td><input name=".$i." type='text' value=" . $_SESSION['unidades'][$i]; echo " size='5'/></td>";  
        echo "<td><a href='carro_detalle.php?action=suma'><img src='images/flecharriba.png' width='10x' height='10px'/></a></td>";
        echo "<td><a href='carro_detalle.php?action=resta'><img src='images/flechabajo.png' width='10px' height='10px'/></a></td>";
        echo "<td>" . $row['precio']; echo "</td>";
        echo "<td>" . $row['precio'] * $_SESSION['unidades'][$i]; echo "</td>";
            echo "</tr>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教