Search code examples
phpauto-increment

PHP count starting from 0


Hello I was just wondering how to auto increment count for each item in the shopping cart. How to auto increment for each item in cart so if theres two products product 1 will be [0] and product 2 will be [1]

This is the outcome I am after...

pickuppostcode=2000&pickupsuburb=SYDNEY+CITY&deliverypostcode=4000&deliverysuburb=BRISBANE&type[0]=Carton&width[0]=40&height[0]=35&depth[0]=65&weight[0]=2&items[0]=2&type[1]=BulkyItem&width[1]=50&height[1]=45&depth[1]=25&weight[1]=3&items[1]=3&type[2]=BulkyItem&width[2]=50&height[2]=45&depth[2]=25&weight[2]=3&items[2]=3

(current)

pickuppostcode=2000&pickupsuburb=SYDNEY+CITY&deliverypostcode=4000&deliverysuburb=BRISBANE&type[0]=Carton&width[0]=40&height[0]=35&depth[0]=65&weight[0]=2&items[0]=2&type[0]=BulkyItem&width[0]=50&height[0]=45&depth[0]=25&weight[0]=3&items[0]=3

Specific problem...(because I am hard coding the 0. Need to know how to auto-increment for each item in cart starting from 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";
    }
    }

Heres my code for my hole page..

<?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"); 
?>

Solution

  • Why "&width[0]=$width" ? try "&width[$i]=$width"