English 中文(简体)
我如何在PHP中增加一个阵列?
原标题:How do I add to an array in PHP?
  • 时间:2012-04-25 18:58:14
  •  标签:
  • php

I m doing the following but it doesn t work. it only has 1 item in the array no matter how many i add

一个人能够告诉我,什么是错做的。

session_start();

$pid = mysql_real_escape_string(trim($_GET["pid"]));
$price = mysql_real_escape_string(trim($_GET["price"]));
$quantity = mysql_real_escape_string(trim($_GET["quantity"]));

if (!isset($_SESSION[ cart ])) {
    $_SESSION[ cart ] = array();

    $_SESSION[ cart ][ pid ] = $pid;
    $_SESSION[ cart ][ total_price ] = $price;
    $_SESSION[ cart ][ total_items ] = $quantity;

}else{
    $_SESSION[ cart ][ pid ] = $pid;
    $_SESSION[ total_price ] += $price;
    $_SESSION[ total_items ]  += $quantity;
}
最佳回答

它像你一样,正在重新确定阵列中的价值观。 每当你确定“SESSION[t ][ pid]美元时,你都会重写最后价值。 然而,你的总价格和总产值很可能是正确地增加吗?

使用<代码>_SESSION [ cart ][ pid ][] = pid; 相反。 你们需要大量的补助,以便你们能够拥有多个物品。 []运营商将价值视为阵列,并将新的价值推到阵列的末尾。

EDIT: Your initialization under the if should look like the following so that your [ pid ] is an array of pid s:

$_SESSION[ cart ] = array();
$_SESSION[ cart ][ pid ] = array(); //this might be redundant...but I always initialize my variables
$_SESSION[ cart ][ pid ][] = $pid;
$_SESSION[ cart ][ total_price ] = $price;
$_SESSION[ cart ][ total_items ] = $quantity;

在<代码>下 您将:

$_SESSION[ cart ][ pid ][] = $pid;
$_SESSION[ cart ][ total_price ] += $price;
$_SESSION[ cart ][ total_items ] += $quantity;

注:You forgot [ cart] on the total_price and total_items under the else, as was referred to in other response.

问题回答

Looks like you forgot to add the [ cart ] to the bottom two $_SESSION setters:

$_SESSION[ total_price ] += $price;
$_SESSION[ total_items ]  += $quantity;

变化

$_SESSION[ cart ][ total_price ] += $price;
$_SESSION[ cart ][ total_items ]  += $quantity;
$_SESSION[ cart ][ pid ] = $pid; 
$_SESSION[ total_price ] += $price; 
$_SESSION[ total_items ]  += $quantity; 

您在座右铭上 for。 应:

$_SESSION[ cart ][ pid ] = $pid; 
$_SESSION[ cart ][ total_price ] += $price; 
$_SESSION[ cart ][ total_items ]  += $quantity; 

您在最后一届会议期间希望其他声明的变量。

See this, for a list of range reference functions in php.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签