Purushotam Sharma Purushotam Sharma - 1 month ago 7
PHP Question

Arrange the array in table format

This is my array and I want to set this array in table format and I want to set first row as show below:

$_tierPrices = Array
(
Array
(
"price_qty" => "4",
"price" => "143.00",
"savePercent" => "8"
),

Array
(
"price_qty" => "12",
"price" => "133.0000",
"savePercent" => "15"
),

Array
(
"price_qty" => "20",
"savePercent" => "18",
"price" => "128.0000"
),

Array
(
"price_qty" => "40",
"savePercent" => "21",
"price" => "123.0000"
)
);


I want to set this array value in table format as shown below

This is my table row:

4 | 12 | 20 | 40 |


I need this:

4 - 11 | 12 - 19 | 20 - 39 | 40+ |


Please advise.

Answer

Try this:

Tier Price Array

<?php
    $_tierPrices = array (
        array
        (
            "price_qty" => "4",
            "price" => "143.00",
            "savePercent" => "8"      
        ),

        array
        (   
            "price_qty" => "12",
            "price" => "133.0000",
            "savePercent" => "15"
        ),

        array
        (   
            "price_qty" => "20",
            "savePercent" => "18",
            "price" => "128.0000"
        ),

        array
        (
            "price_qty" => "40",
            "savePercent" => "21",
            "price" => "123.0000"
        )
    );
?>

Tier Price Table

<table>
    <tr><td>Header</td></tr>
    <?php $_qtyRow = '<tr>'; ?>
    <?php $_priceRow = '<tr>'; ?>
    <?php $_saveRow = '<tr>'; ?>
    <?php
        $size = count($_tierPrices);
        foreach($_tierPrices as $index => $_tierPrice) {
            if($index < $size-1) {
                $_qty = $_tierPrice['price_qty'] . ' - ' . ($_tierPrices[$index+1]['price_qty'] - 1);
            } else {
                $_qty = $_tierPrice['price_qty'] . '+';             
            }
            $_qtyRow .= '<td>'.$_qty.'</td>';
            $_priceRow .= '<td>'.$_tierPrice['price'].'</td>';
            $_saveRow .= '<td>'.$_tierPrice['savePercent'].'</td>';
        }

        $_qtyRow .= '</tr>';
        $_priceRow .= '</tr>';
        $_saveRow .= '</tr>';

        echo $_qtyRow;
        echo $_priceRow;
        echo $_saveRow;
    ?>
    <tr><td>Footer</td></tr>
</table>

I hope this will help.

Comments