dongxian6653
dongxian6653
2014-11-12 17:25
浏览 62

通过单击图像将值加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!!

图片转代码服务由CSDN问答提供 功能建议

我们,我需要你的帮助。 我有这个代码,它向我展示了我购物车中的产品:

  for($ i = 0; $ i&lt; count($ _ SESSION ['productos']  ); $ i ++)
 {
 $ id = $ _SESSION ['productos'] [$ i]; 
 $ prods = mysql_query('SELECT * from productos where idprod ='。$ id。''); \  n $ row = mysql_fetch_array($ prods); 
 echo“&lt; tr&gt;”; 
 echo“&lt; td&gt;”  。  $行[ 'NOMBRE'];  echo“&lt; / td&gt;”; 
 
 if if($ _ GET [action] ==“suma”)
 {
 $ _SESSION ['unidades'] [$ i] = $ _SESSION ['unidades']  [$ i] + 1; 
} 
 elseif($ _ GET [action] ==“resta”)
 {
 $ _SESSION ['unidades'] [$ i] = $ _SESSION ['unidades'] [  $ i]  -  1; 
} 
 
 echo“&lt; td&gt;&lt; input name =”。$ i。“type ='text'value =”。  $ _SESSION [ 'unidades'] [$ i]于;  echo“size ='5'/&gt;&lt; / td&gt;”;  
 echo“&lt; td&gt;&lt; a href ='carro_detalle.php?action = suma'&gt;&lt; img src ='images / flecharriba.png'width ='10x'height ='10px'/&gt;&lt;  ; / a&gt;&lt; / td&gt;“; 
 echo”&lt; td&gt;&lt; a href ='carro_detalle.php?action = resta'&gt;&lt; img src ='images / flechabajo.png'width ='  10px'height ='10px'/&gt;&lt; / a&gt;&lt; / td&gt;“; 
 echo”&lt; td&gt;“  。  $行[ 'PRECIO'];  echo“&lt; / td&gt;”; 
 echo“&lt; td&gt;”  。  $ row ['precio'] * $ _SESSION ['unidades'] [$ i];  echo“&lt; / td&gt;”; 
 echo“&lt; / tr&gt;”; 
} 
   
 
 

我需要将1个统一或1个统一 当我单击这两个图像时,只需对所选产品进行$ _SESSION ['unidades'] [$ i]。 当我点击它时,它会将我添加到所有产品中。 没有使用$ _GET变量的任何简单方法吗? 我对JavasScript不了解。 谢谢!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyuhui0418
    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>";
    }
    
    点赞 评论

相关推荐