Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我能';无法使此代码正常工作(购物车php)。。。i';我已经试了好几天了_Php_Cart_Shopping - Fatal编程技术网

我能';无法使此代码正常工作(购物车php)。。。i';我已经试了好几天了

我能';无法使此代码正常工作(购物车php)。。。i';我已经试了好几天了,php,cart,shopping,Php,Cart,Shopping,我正试图在PHP上制作一个购物车,但它根本不起作用,页面上什么也看不到,我已经检查了我的代码,甚至重做了它,但它根本不起作用。。。如果有人能帮我,那就太好了,我会尽我所能解释我的逻辑 <?php session_start(); // I use sessions! include('librerias/conexion.php'); // This calls the DB connection which is ok! if (isset($_SESSION['carrito'])){

我正试图在PHP上制作一个购物车,但它根本不起作用,页面上什么也看不到,我已经检查了我的代码,甚至重做了它,但它根本不起作用。。。如果有人能帮我,那就太好了,我会尽我所能解释我的逻辑

<?php
session_start(); // I use sessions!
include('librerias/conexion.php'); // This calls the DB connection which is ok!
if (isset($_SESSION['carrito'])){ //If the shopping cart is set...
  $arreglo=$_SESSION['carrito']; //an array will be set on the session variable
    $encontro=false; //this is a flag to see if the added item is already on the cart
    $numero = 0; // This will be used for index on the array
    for($i=0; $i<count($arreglo); $i++){  // This loop searchs the array to see if there's a matching ID, if there is, then the quantity goes up by one
        if($arreglo[$i]['Id']==$_GET['id']){
            $numero=$i;
            $encontro=true;        
    }
    if($encontro){
    $arreglo[$numero]['cantidad']+=1;
    }
    }
}else{ //If there's no session shopping cart...
    if(isset($_GET['id'])){ //it will check if it comes with an id from an item
        $nombre="";
        $precio="";
        $id = $_GET['id'];
        $sql="SELECT * FROM productos WHERE id=$id"; // This is my query to get data from that item
        print $sql;
        $res=  mysql_query($sql); //Query...
        print $res;
        while($fila=  mysql_fetch_array($res)){ //This will set the name of the item, the price, and the image
            $nombre=$fila['nombre'];
            $precio=$fila['precio'];
            $imagen = $fila['imagen'];
        }
        $arreglo[]= array('Id'=>$id,  //when all that is set, this array is created with all those values
                        'Nombre'=>$nombre,
                        'precio'=>$precio,
                        'imagen'=>$imagen,
                        'cantidad'=>1);

        $_SESSION['carrito']=$arreglo; // Finally the Session shopping cart s set to be the array
    }
}
?>
<?php
include("librerias/header.php");
?>
<?php if(isset($_SESSION['carrito'])){ //Now it checks if it's set again and creates a table
    $total=0;
    ?>
<table class="table"><th>Nombre</th><th>Precio</th><th>Imagen</th><th>Cantidad</th><th>Subtotal</th></table> 
<?php
    for($i=0; $i<count($_SESSION['carrito']); $i++){
        ?>
<tr>
    <td><?php echo $_SESSION['carrito']['Nombre'];?></td>
    <td><?php echo $_SESSION['carrito']['precio'];?></td>
    <td><img src=" <?php echo $_SESSION['carrito']['imagen'];?>"</td>
    <td><?php echo $_SESSION['carrito']['cantidad'];?></td>
    <td><?php echo $_SESSION['carrito']['precio']*$_SESSION['carrito']['cantidad'];?></td>
</tr>
<?php
    }
}else{
     echo "<center>El carrito está vacío</center>";
}

        ?>


<?php
include("librerias/footer.php");
?>


您正在将
$\u SESSION['carrito']
的值设置为包含
Nombre
precio
等键的关联
数组

然后你可以用这个做两件事中的一件

  • a、 )循环所有键以输出值
  • b、 )输出所需的特定键
看起来你想要B选项

因此,如果您只是删除已有的for循环,这应该可以工作

<tr>
    <td><?php echo $_SESSION['carrito']['Nombre'];?></td>
    <td><?php echo $_SESSION['carrito']['precio'];?></td>
    <td><img src=" <?php echo $_SESSION['carrito']['imagen'];?>"</td>
    <td><?php echo $_SESSION['carrito']['cantidad'];?></td>
    <td><?php echo $_SESSION['carrito']['precio']*$_SESSION['carrito']['cantidad'];?></td>
</tr>

"

由于您没有指定目标,请选择合适的目标

选择1 或者这个:

// from this
$_SESSION['carrito']=$arreglo; 
// to this
$_SESSION['carrito']=$arreglo[0];
选择2 如果我了解您试图执行的操作,请将while循环更改为:

while($fila=  mysql_fetch_array($res)){
    $nombre=$fila['nombre'];
    $precio=$fila['precio'];
    $imagen = $fila['imagen'];
    $arreglo[]= array('Id'=>$id, 'Nombre'=>$nombre, 'precio'=>$precio, 'imagen'=>$imagen, 'cantidad'=>1);
}
以及您的for循环:

<?php for($i=0; $i<count($_SESSION['carrito']); $i++){ ?>
        <tr>
            <td><?php echo $_SESSION['carrito'][$i]['Nombre'];?></td>
            <td><?php echo $_SESSION['carrito'][$i]['precio'];?></td>
            <td><img src=" <?php echo $_SESSION['carrito'][$i]['imagen'];?></td>
            <td><?php echo $_SESSION['carrito'][$i]['cantidad'];?></td>
            <td><?php echo $_SESSION['carrito'][$i]['precio']*$_SESSION['carrito'][$i]['cantidad'];?></td>
        </tr>


$\u会话不包含这些索引…您还没有设置它们,我会说您的$\u会话['carrito']是否为真,但其中没有数据?实际上我看到它$arreglo[]=array()意味着您访问$arreglo[0][key]等等
<?php for($i=0; $i<count($_SESSION['carrito']); $i++){ ?>
        <tr>
            <td><?php echo $_SESSION['carrito'][$i]['Nombre'];?></td>
            <td><?php echo $_SESSION['carrito'][$i]['precio'];?></td>
            <td><img src=" <?php echo $_SESSION['carrito'][$i]['imagen'];?></td>
            <td><?php echo $_SESSION['carrito'][$i]['cantidad'];?></td>
            <td><?php echo $_SESSION['carrito'][$i]['precio']*$_SESSION['carrito'][$i]['cantidad'];?></td>
        </tr>