我无法使用此代码(购物车 php)...我已经尝试修复它好几天了
I can't get this code to work (shopping cart php)... i've been trying to fix it for days
我正在尝试在 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");
?>
完成..
但我的结果是:
注意:未定义的索引:Nombre in C:\xampp\htdocs\TiendaJavier\carrito.php on line 52
注意:未定义索引:C:\xampp\htdocs\TiendaJavier\carrito.php 中的 precio 第 53 行
注意:未定义的索引:C:\xampp\htdocs\TiendaJavier\carrito.php 中的 cantidad 第 55
行
注意:未定义索引:C:\xampp\htdocs\TiendaJavier\carrito.php 中的 precio 第 56 行
注意:未定义索引:C:\xampp\htdocs\TiendaJavier\carrito.php 中第 56 行的 cantidad
而且我真的无法工作怎么了...在此先感谢
您正在将 $_SESSION['carrito']
的值设置为包含 Nombre
、precio
等键的关联 Array
然后您可以用它做 2 件事中的 1 件事。
- 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
$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>
我正在尝试在 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");
?>
完成..
但我的结果是:
注意:未定义的索引:Nombre in C:\xampp\htdocs\TiendaJavier\carrito.php on line 52
注意:未定义索引:C:\xampp\htdocs\TiendaJavier\carrito.php 中的 precio 第 53 行
注意:未定义的索引:C:\xampp\htdocs\TiendaJavier\carrito.php 中的 cantidad 第 55
行注意:未定义索引:C:\xampp\htdocs\TiendaJavier\carrito.php 中的 precio 第 56 行
注意:未定义索引:C:\xampp\htdocs\TiendaJavier\carrito.php 中第 56 行的 cantidad
而且我真的无法工作怎么了...在此先感谢
您正在将 $_SESSION['carrito']
的值设置为包含 Nombre
、precio
等键的关联 Array
然后您可以用它做 2 件事中的 1 件事。
- 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
$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>