автоинкремент — счетчик PHP, начиная с 0

Здравствуйте, мне просто интересно, как автоматически увеличить счетчик для каждого элемента в корзине.
Как автоматически увеличивать для каждого элемента в корзине, так что если для двух продуктов продукт 1 будет [0], а продукт 2 будет [1]

Это результат, который я после …

pickuppostcode = 2000&pickupsuburb = СИДНЕЙ + ГОРОД&deliverypostcode = 4000&deliverysuburb = БРИСБЕН&тип [0] = Картон&Ширина [0] = 40&Высота [0] = 35&Глубина [0] = 65&Вес [0] = 2&пункты [0] = 2&типа [1] = BulkyItem&Ширина [1] = 50&Высота [1] = 45&Глубина [1] = 25&Вес [1] = 3&пунктов [1] = 3&типа [2] = BulkyItem&Ширина [2] = 50&высота [2] = 45&Глубина [2] = 25&Вес [2] = 3&пункты [2] = 3

(ток)

pickuppostcode = 2000&pickupsuburb = СИДНЕЙ + ГОРОД&deliverypostcode = 4000&deliverysuburb = БРИСБЕН&тип [0] = Картон&Ширина [0] = 40&Высота [0] = 35&Глубина [0] = 65&Вес [0] = 2&пункты [0] = 2&тип [0] = BulkyItem&Ширина [0] = 50&Высота [0] = 45&Глубина [0] = 25&Вес [0] = 3&пункты [0] = 3

Конкретная проблема … (потому что я жестко кодирую 0. Нужно знать, как автоматически увеличивать для каждого элемента в корзине, начиная с 0.

//for each product in the cart get the following
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){ //for each product in the cart get the following
$pid=$_SESSION['cart'][$i]['productID']; //productID
$sql="SELECT * FROM products_dimensions WHERE productID=$pid";
$result = mysqli_query($con, $sql) or die(mysqli_error($con)); //run the query
while ($row = mysqli_fetch_array($result)){

/* Dimensions */
$width  = $row['width'];
$height = $row['height'];
$depth  = $row['depth'];
$weight = $row['weight'];
$type = $row['type'];
$quantity=$_SESSION['cart'][$i]['qty'];

$ego_params .= "&width[0]=$width";
$ego_params .= "&height[0]=$height";
$ego_params .= "&depth[0]=$depth";
$ego_params .= "&weight[0]=$weight";
$ego_params .= "&type[0]=$type";
$ego_params .= "&items[0]=$quantity";
}
}

Вот мой код для моей дырочной страницы ..

<?php
session_start();
include "../includes/connect.php";
$calculator_url = "http://www.e-go.com.au/calculatorAPI2";
/* from/to postcodes */
$pickuppostcode   = 4508;
$pickupsuburb   = urlencode('DECEPTION BAY');

//To Destination
$deliverypostcode   = $_POST['postCode'];
$deliverysuburb = urlencode(strtoupper($_POST['suburb']));

$ego_params  = "?pickuppostcode=$pickuppostcode&pickupsuburb=$pickupsuburb";
$ego_params .= "&deliverypostcode=$deliverypostcode&deliverysuburb=$deliverysuburb";

//for each product in the cart get the following
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){ //for each product in the cart get the following
$pid=$_SESSION['cart'][$i]['productID']; //productID
$sql="SELECT * FROM products_dimensions WHERE productID=$pid";
$result = mysqli_query($con, $sql) or die(mysqli_error($con)); //run the query
while ($row = mysqli_fetch_array($result))
{
/* Dimensions */
$width  = $row['width'];
$height = $row['height'];
$depth  = $row['depth'];
$weight = $row['weight'];
$type = $row['type'];
$quantity=$_SESSION['cart'][$i]['qty']; //quantity

$ego_params .= "&width[0]=$width";
$ego_params .= "&height[0]=$height";
$ego_params .= "&depth[0]=$depth";
$ego_params .= "&weight[0]=$weight";
$ego_params .= "&type[0]=$type";
$ego_params .= "&items[0]=$quantity";
}
}
$ego_quote  = file($calculator_url . $ego_params);

foreach ($ego_quote as $num=>$quote) {
$quote = trim($quote);
$quote_field = explode("=", $quote);
echo "Field=" . $quote_field[0] . "\tValue=" . $quote_field[1] . "\n";
}
$_SESSION['quote'] = $quote;
echo $ego_params;
//header ("location: ../pages/shoppingcart.php");
?>

0

Решение

Зачем "&width[0]=$width" ? пытаться "&width[$i]=$width"

1

Другие решения

Других решений пока нет …