0

There is a pricing table array that holds a set of prices associated with each damage area, dent size and number of dents. I'm trying to get the specific price depending on which was selected.

To explain: A user will select have the option to select a Damage Area > Dent Size > Number of Dents, then there is a specific cost associated. (For example, Hood > Nickel > 6-15 will cost $225)

$pricing_table => Array (
                    [Hood] => Array ( 
                        [Dime] => Array ( 
                            [1-5] => 100 
                            [6-15] => 200 
                            [16-30] => 250 
                            [31-50] => 375 
                            [51-75] => 425 
                            [76-100] => 475 
                            [101-150] => 475 
                            [151-200] => 600 ) 
                        [Nickel] => Array ( 
                            [1-5] => 125 
                            [6-15] => 225 
                            [16-30] => 300 
                            [31-50] => 400 
                            [51-75] => 475 
                            [76-100] => 525 
                            [101-150] => 625 
                            [151-200] => 700 )
                        [Quarter] => Array ( 
                            [1-5] => 150 
                            [6-15] => 250 
                            [16-30] => 400 
                            [31-50] => 500 
                            [51-75] => 575 
                            [76-100] => 625 
                            [101-150] => 725 
                            [151-200] => 800 )
                        [Halfdollar] => Array ( 
                            [1-5] => 200 
                            [6-15] => 300 
                            [16-30] => 375 
                            [31-50] => 500 
                            [51-75] => 675 
                            [76-100] => 825 
                            [101-150] => 925 
                            [151-200] => 975 ) ) 
                    [Roof] => Array...
                    [Trunk] => Array...
                    Etc. 
                    REPEAT x 12

The array above ($pricing_table) is created with the following code below in the backend, the user sees none of this:

    $pricing_table = get_option( 'hailbids_pricing_table' );
    $is_submit = 0;
    foreach($pricing_table as $damage_area_title => &$damage_area) {
        foreach($damage_area as $dent_size_title => &$dent_size) {
            foreach($dent_size as $number_dents_title => &$number_dents) {
                if($_POST[sanitize_title('submit-tab-'.$damage_area_title)]) {
                    $is_submit = 1;
                    $number_dents = $_POST[sanitize_title($damage_area_title.'-'.$dent_size_title.'-'.$number_dents_title)];
                }
            }
        }
    }

The code below is what I have so far:

<?php 
$dent_size = get_post_meta(get_the_ID(), 'dent_size', true);
$number_dents = get_post_meta(get_the_ID(), 'number_dents', true);
            
if ($price > 0) {
?>

<?php
    $pricing_table = get_option( 'hailbids_pricing_table' );
    $damage_area_no = 1;
    foreach ( $pricing_table as $damage_area_title => $damage_area ) :
        $i = $damage_area_no - 1;
        if(!empty($dent_size[$i]) and !empty($number_dents[$i])) {              
            echo $number_dents[$i][0];  
            echo $number_dents[$i];
        } else 
            echo 0;
?>
<?php
        $damage_area_no++;
    endforeach;
?>
<?PHP } else {  ?>
<?PHP }   ?>

The script will loop through each $damage_area and check to see if the *$*dent_size[$i] and $number_dents[$i] is not empty. If they are not empty, then I need to echo the correct price associated with $number_dents[$i] from the pricing table array $pricing_table.

This was all passed to me and I'm still figuring it out, so let me know if I didn't explain something well enough.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
EonYY
  • 1
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Nov 10 '22 at 17:02
  • Although it rarely see it with Wordpress code – RiggsFolly Nov 10 '22 at 17:03
  • As it happens I posted a question years ago that might help you? Not sure if it's the same issue you're having: https://stackoverflow.com/questions/51835248/php-use-array-to-get-value-in-another-multidimensional-array – Shoelaced Nov 10 '22 at 18:46
  • `$pricing_table => Array (` oh dear. USE A DATABASE. – symcbean Nov 10 '22 at 21:28
  • @RiggsFolly You're definitely right. This isn't my code, but I did dump it here and should have formatted it a little before posting. I did remove all the HTML though. I'll make sure to do that next time! – EonYY Nov 11 '22 at 14:22
  • @Shoelaced Thanks for this! I'm going to try and see if I can apply this to my situation. I'll let you know if it works out. – EonYY Nov 11 '22 at 14:23
  • @symcbean A good idea, but I was just tasked with getting this one solution. Hopefully, I don't get a task to rebuild the thing in the future. Fingers crossed. – EonYY Nov 11 '22 at 14:25

0 Answers0