我无法使用此代码(购物车php)...我已经试图修复它好几天了

时间:2015-08-21 00:53:26

标签: 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'])){ //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");
?>

完成..

但我的结果是:

注意:未定义的索引:第52行的C:\ xampp \ htdocs \ TiendaJavier \ carrito.php中的Nombre

注意:未定义的索引:第53行的C:\ xampp \ htdocs \ TiendaJavier \ carrito.php中的precio

注意:未定义的索引:第55行的C:\ xampp \ htdocs \ TiendaJavier \ carrito.php中的cantidad

注意:未定义的索引:第56行的C:\ xampp \ htdocs \ TiendaJavier \ carrito.php中的precio

注意:未定义的索引:第56行的C:\ xampp \ htdocs \ TiendaJavier \ carrito.php中的cantidad

我真的无法解决错误的事情......提前谢谢

2 个答案:

答案 0 :(得分:1)

您要将$_SESSION['carrito']的值设置为包含ArrayNombre等键的关联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 :(得分:1)

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

选项1

// from this
$arreglo[]= array('Id'=>$id, 'Nombre'=>$nombre, 'precio'=>$precio, 'imagen'=>$imagen, 'cantidad'=>1);
// to this
$arreglo= array('Id'=>$id, 'Nombre'=>$nombre, 'precio'=>$precio, 'imagen'=>$imagen, 'cantidad'=>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>