Page 1 of 1

help with "look up" arrays

Posted: Tue Jan 06, 2009 10:14 pm
by mallen
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I am building an estimate form that will display the individual price of a CD, including the label and packaging costs and then a grand totol. Works fine. But what I want to add is "You selected xxxxxx for printing, xxxxxx for packaging etc.

Someone told me I need to use a "look up" array. I have tried searching for anything that might get me started. I can't find anything on the php manual that would help. Can someone help me?

Code: Select all

 
<?php 
ini_set ('display_errors',1);
error_reporting (E_ALL & ~E_NOTICE);
 
 
//accept the quantity: 
$quantity = $_POST['cdQuantity']; 
//check if it's valid: 
if ( !is_numeric($quantity) || intval($quantity) == 0 ) { 
    die("You entered an invalid quantity: '$quantity' must be a whole number."); 
} 
 
//accept the label selection: 
$label = $_POST['cdLabel']; 
//also numeric, but with a specific range: 
if ( !is_numeric($label) || intval($label) == 0 || $label < 0 || $label > 4 ) { 
    die("INVALID SELECTION"); 
} 
$package = $_POST['cdPackage'];
$printing = $_POST['cdPrint'];
 
//get the row designation based on the quantity: 
if ( $quantity > 0 && $quantity <= 10 ) { 
    $key = "1-10";
    $cdEach = 8;  
} elseif ( $quantity > 10 && $quantity <= 24 ) { 
    $key = "11-24";
    $cdEach = 7;
} elseif ( $quantity > 24 && $quantity <= 49 ) { 
    $key = "25-49";
    $cdEach = 6;
} elseif ( $quantity > 49 && $quantity <= 99 ) { 
    $key = "50-99";
    $cdEach = 5;
} elseif ( $quantity > 99 && $quantity <= 249 ) { 
    $key = "100-249";
    $cdEach = 3;
} elseif ( $quantity > 249 && $quantity <= 499 ) { 
    $key = "250-499";
    $cdEach = 2;
} elseif ( $quantity > 499 && $quantity <= 999 ) { 
    $key = "500-999";
    $cdEach = 1.50;
} elseif ( $quantity > 999 && $quantity <= 2499 ) { 
    $key = "1,000-2,499"; 
    $cdEach = 1.15;
} elseif ( $quantity > 2499 && $quantity <= 4999 ) { 
    $key = "2,500-4,999"; 
    $cdEach = 0.75;
} elseif ( $quantity > 4999 && $quantity <= 9999 ) { 
    $key = "5,000-9,999";
    $cdEach = 0.60;
} else { 
    die("$quantity is an invalid quantity, please phone for a quote."); 
} 
 
 
//build pricing array as a two dimensional array.   
//using $key as the key for the row, and $label as the key for the columns 
 
 
$labelPricing = array( 
    "1-10" => array( 
        1 => 0, 
        2 => 0.1, 
        3 => 0.5, 
        4 => 0.6, 
    ), 
    "11-24" => array( 
        1 => 0, 
        2 => 0.1, 
        3 => 0.5, 
        4 => 0.6, 
    ), 
    "25-49" => array( 
        1 => 0, 
        2 => 0.1, 
        3 => 0.5, 
        4 => 0.6, 
    ), 
    "50-99" => array( 
        1 => 0, 
        2 => 0.1, 
        3 => 0.5, 
        4 => 0.6, 
    ), 
    "100-249" => array( 
        1 => 0, 
        2 => 0.08, 
        3 => 0.4, 
        4 => 0.45, 
    ), 
    "250-499" => array( 
        1 => 0, 
        2 => 0.07, 
        3 => 0.4, 
        4 => 0.45, 
    ), 
    "500-999" => array( 
        1 => 0.0, 
        2 => 0.06, 
        3 => 0.35, 
        4 => 0.4, 
    ), 
    "1,000-2,499" => array( 
        1 => 0, 
        2 => 0.06, 
        3 => 0.35, 
        4 => 0.4, 
    ), 
    "2,500-4,999" => array( 
        1 => 0.20, 
        2 => 0.50, 
        3 => 0.30, 
        4 => 0.35, 
    ), 
    "5,000-9,999" => array( 
        1 => 0.02, 
        2 => 0.05, 
        3 => 0.2, 
        4 => 0.22, 
    ), 
); 
//end label price array ********
 
//begin packaging array *****************************************
$packagePricing = array( 
    "1-10" => array( 
        1 => 0, 
        2 => 0, 
        3 => 0, 
        4 => 0,
        5 => 0.25,
        6 => 0.75, 
    ), 
    
    "11-24" => array( 
        1 => 0, 
        2 => 0, 
        3 => 0.25, 
        4 => 0.25,
        5 => 0.4,
        6 => 0.75, 
    ),
      "25-49" => array( 
        1 => 0, 
        2 => 0, 
        3 => 0.25, 
        4 => 0.25,
        5 => 0.4,
        6 => 0.75, 
    ), 
     "50-99" => array( 
        1 => 0, 
        2 => 0, 
        3 => 0.25, 
        4 => 0.25,
        5 => 0.4,
        6 => 0.65, 
    ), 
     "100-249" => array( 
        1 => 0, 
        2 => 0, 
        3 => 0.25, 
        4 => 0.25,
        5 => 0.4,
        6 => 0.65, 
    ), 
     "250-499" => array( 
        1 => 0, 
        2 => 0, 
        3 => 0.25, 
        4 => 0.25,
        5 => 0.4,
        6 => 0.6, 
    ), 
     "500-999" => array( 
        1 => 0, 
        2 => 0, 
        3 => 0.23, 
        4 => 0.25,
        5 => 0.4,
        6 => 0.6, 
    ), 
     "1,000-2,499" => array( 
        1 => 0, 
        2 => 0.03, 
        3 => 0.23, 
        4 => 0.23,
        5 => 0.38,
        6 => 0.55, 
    ), 
     "2,500=4,999" => array( 
        1 => 0, 
        2 => 0.03, 
        3 => 0.22, 
        4 => 0.22,
        5 => 0.36,
        6 => 0.55, 
    ), 
     "5,000-9,999" => array( 
        1 => 0, 
        2 => 0.03, 
        3 => 0.22, 
        4 => 0.22,
        5 => 0.35,
        6 => 0.5, 
    ), 
); 
//end packaging array ********
 
//begin printing array *****************************************
$printingPricing = array( 
    "1-10" => array( 
        1 => 0.5, 
        2 => 1, 
        3 => 1.25,
        4 => 2.50,
        5 => 4,
        6 => 5,
        7 => 8.50,
    ), 
    "11-24" => array( 
        1 => 0.45, 
        2 => 1,
        3 => 1,
        4 => 2,
        5 => 3.8,
        6 => 4.5,
        7 => 7.5,
    ),
     "25-49" => array( 
        1 => 0.45, 
        2 => 0.85,
        3 => 0.85,
        4 => 1.9,
        5 => 3.6,
        6 => 4,
        7 => 6,
    ),
    "50-99" => array( 
        1 => 0.4, 
        2 => 0.8,
        3 => .75,
        4 => 1.8,
        5 => 3.5,
        6 => 3.6,
        7 => 5, 
    ), 
    "100-249" => array( 
        1 => 0.4, 
        2 => 0.75,
        3 => 0.75,
        4 => 1.75,
        5 => 3.25,
        6 => 3.3,
        7 => 4.5, 
    ),
    "250-499" => array( 
        1 => 0.4, 
        2 => 0.6,
        3 => .65,
        4 => 1.65,
        5 => 2.9,
        6 => 2.95,
        7 => 3.75,
    ), 
     "500-999" => array( 
        1 => 0.35, 
        2 => 0.5,
        3 => 0.6,
        4 => 1.5,
        5 => 2.75,
        6 => 2.8,
        7 => 3.4, 
    ), 
    "1,000-2,499" => array( 
        1 => 0.3, 
        2 => 0.35,
        3 => 0.45,
        4 => 1.1,
        5 => 2.45,
        6 => 2.5,
        7 => 2.85,
    ), 
     "2,500-4,999" => array( 
        1 => 0.15, 
        2 => 0.25,
        3 => 0.35,
        4 => 0.8,
        5 => 2,
        6 => 2.1,
        7 => 2.35, 
    ),
    "5,000-9,999" => array( 
        1 => 0.1, 
        2 => 0.22,
        3 => 0.32,
        4 => 0.65,
        5 => 1.65,
        6 => 1.70,
        7 => 2.15, 
    ), 
);    
//end printing array ******
 
 
//display cost and totals.
 
$unitprice= number_format($labelPricing[$key][$label] + $packagePricing[$key][$package] + $printingPricing[$key][$printing] + $cdEach,2);
$total= number_format($quantity * $unitprice,2);
 
print "You have selected $quantity CDs at a unit price of \$$unitprice 
<br/>
<br/>
Your total is \$$total";
?> 
 

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.