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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记