tahira rehman
tahira rehman

Reputation: 3

How To perform calculations on array and show it in tabular form

I have an assignment regarding multiplication of array and show it in tabular form, my teacher just give me the data that is in the array form and said to display it in tabular form and also multiply the items **Quantity and price ** and show it in table ....i already have display the array in tabular form but i'm unable to product the QTY and price? anybody help ?


<!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
$ProductsTest = array("1001" => array("Name" => "LCD","Desc" => "Sony","Qty" =>10,"Price" => 2000),
"1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300),
"1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300),
"1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700),
"1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000));
$no_of_ele = count($ProductsTest);
echo '<br><br>';
echo ' <table border="2", bgcolor="white",width="100%"> 
<tr>
 <th>Name</th> 
    <th>Brand</th>
     <th>QTY</th> 
    <th>price</th>
    <th>totalamount</th>
  </tr>';
foreach( $ProductsTest as $values)
{
    echo '<tr>';
    foreach( $values as $key )
    {
        echo '<td>'.$key.'</td>';
  }
    echo '</tr>';
}

echo '</table>';
?>

</body>
</html>

this is my output till now

Upvotes: 0

Views: 40

Answers (3)

Fermin Perdomo
Fermin Perdomo

Reputation: 424

This is a good soluction for this problem using functional programming you can map the array to return other array with the totalamount

    <!doctype html>
        <html>
        <head>
        <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    $lists = array("1001" => array("Name" => "LCD","Desc" => "Sony","Qty" =>10,"Price" => 2000),
    "1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300),
    "1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300),
    "1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700),
    "1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000));

    $ProductsTest  = array_map(function($item){
        $item['totalamount'] = $item['Qty'] * $item['Price'];
        return $item;
    },$lists);
$no_of_ele = count($ProductsTest);
    echo '<br><br>';
    echo ' <table border="2", bgcolor="white",width="100%"> 
    <tr>
     <th>Name</th> 
        <th>Brand</th>
         <th>QTY</th> 
        <th>price</th>
        <th>totalamount</th>
      </tr>';
    foreach( $ProductsTest as $values)
    {
        echo '<tr>';
        foreach( $values as $key )
        {
            echo '<td>'.$key.'</td>';
        }
        echo '</tr>';
    }

    echo '</table>';
    ?>

    </body>
    </html>

Upvotes: 0

user1544541
user1544541

Reputation: 193

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    $ProductsTest = array(
    "1001" => array("Name" => "LCD", "Desc" => "Sony", "Qty" =>10, "Price" => 2000),
    "1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300),
    "1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300),
    "1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700),
    "1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000)
    );

    $no_of_ele = count($ProductsTest);

    echo '<br><br>';

    if( $no_of_ele > 0 )
    {
    echo '<table border="2", bgcolor="white",width="100%"> 
    <tr>
    <th>Name</th> 
    <th>Brand</th>
    <th>QTY</th> 
    <th>price</th>
    <th>totalamount</th>
    </tr>';

    foreach( $ProductsTest as $key_ProductsTest => $value_ProductsTest)
    {
    echo '<tr>';
    echo '<td>'.$value_ProductsTest["Name"].'</td>';
    echo '<td>'.$value_ProductsTest["Desc"].'</td>';
    echo '<td>'.$value_ProductsTest["Qty"].'</td>';
    echo '<td>'.$value_ProductsTest["Price"].'</td>';
    echo '<td>'. ($value_ProductsTest["Qty"] * $value_ProductsTest["Price"]) .'</td>';
    echo '</tr>';
    }

    echo '</table>';
    }
    else
    {
    echo "Sorry, no data found.";
    }


    ?>

    </body>
    </html>

Upvotes: 0

Naincy
Naincy

Reputation: 2943

<?php
$ProductsTest = array("1001" => array("Name" => "LCD","Desc" => "Sony","Qty" =>10,"Price" => 2000),
"1002" => array("Name" => "Mouse","Desc" => "Dell","Qty" =>3,"Price" => 300),
"1003" => array("Name" =>"Mouse","Desc" =>"a4Tech","Qty" =>3,"Price" =>300),
"1004" => array("Name" =>"USB","Desc" =>"Kingstton","Qty" =>15,"Price" =>700),
"1005" => array("Name" =>"HeadPhone","Desc" =>"a4Tech","Qty" =>7,"Price" =>3000));
$no_of_ele = count($ProductsTest); 
?>
<br><br>
<table border="2", bgcolor="white",width="100%"> 
<tr>
 <th>Name</th> 
    <th>Brand</th>
     <th>QTY</th> 
    <th>price</th>
    <th>totalamount</th>
  </tr>
<?php
foreach( $ProductsTest as $key => $value)
{
    echo '<tr>';
    echo '<td>'.$value['Name'].'</td>';
    echo '<td>'.$value['Desc'].'</td>';
    echo '<td>'.$value['Qty'].'</td>';
    echo '<td>'.$value['Price'].'</td>';
    echo '<td>'.$value['Qty']*$value['Price'].'</td>';
    echo '</tr>';
}
?>
</table>

Upvotes: 1

Related Questions