Correctly echo out multiple values in an implode array_column [duplicate] - php

I have an array in below format
Array ( [0] => Array ( [product_id] => 33 [amount] => 1 ) [1] => Array ( [product_id] => 34 [amount] => 3 ) [2] => Array ( [product_id] => 10 [amount] => 1 ) )
I want to get output from that array as below format
Product ID Amount
33 1
34 3
10 1
Can anyone please help me regarding this problem. var_dump of the variable is.
array
0 =>
array
'product_id' => string '33' (length=2)
'amount' => string '1' (length=1)
1 =>
array
'product_id' => string '34' (length=2)
'amount' => string '3' (length=1)
2 =>
array
'product_id' => string '10' (length=2)
'amount' => string '1' (length=1)

I believe this is your array
$array = Array (
0 => Array ( "product_id" => 33 , "amount" => 1 ) ,
1 => Array ( "product_id" => 34 , "amount" => 3 ) ,
2 => Array ( "product_id" => 10 , "amount" => 1 ) );
Using foreach
echo "<pre>";
echo "Product ID\tAmount";
foreach ( $array as $var ) {
echo "\n", $var['product_id'], "\t\t", $var['amount'];
}
Using array_map
echo "<pre>" ;
echo "Product ID\tAmount";
array_map(function ($var) {
echo "\n", $var['product_id'], "\t\t", $var['amount'];
}, $array);
Output
Product ID Amount
33 1
34 3
10 1

<table>
<tr>
<th>Product Id</th>
<th>Ammount</th>
</tr>
<?php
foreach ($yourArray as $subAray)
{
?>
<tr>
<td><?php echo $subAray['product_id']; ?></td>
<td><?php echo $subAray['amount']; ?></td>
</tr>
<?php
}
?>
</table>

Try this..
foreach($arr as $a)
{
echo $a['product_id'];
echo $a['amount'];
}
Format as per your requirment.

Related

How to get all the data from an array and display them in a php site? [duplicate]

I have an array in below format
Array ( [0] => Array ( [product_id] => 33 [amount] => 1 ) [1] => Array ( [product_id] => 34 [amount] => 3 ) [2] => Array ( [product_id] => 10 [amount] => 1 ) )
I want to get output from that array as below format
Product ID Amount
33 1
34 3
10 1
Can anyone please help me regarding this problem. var_dump of the variable is.
array
0 =>
array
'product_id' => string '33' (length=2)
'amount' => string '1' (length=1)
1 =>
array
'product_id' => string '34' (length=2)
'amount' => string '3' (length=1)
2 =>
array
'product_id' => string '10' (length=2)
'amount' => string '1' (length=1)
I believe this is your array
$array = Array (
0 => Array ( "product_id" => 33 , "amount" => 1 ) ,
1 => Array ( "product_id" => 34 , "amount" => 3 ) ,
2 => Array ( "product_id" => 10 , "amount" => 1 ) );
Using foreach
echo "<pre>";
echo "Product ID\tAmount";
foreach ( $array as $var ) {
echo "\n", $var['product_id'], "\t\t", $var['amount'];
}
Using array_map
echo "<pre>" ;
echo "Product ID\tAmount";
array_map(function ($var) {
echo "\n", $var['product_id'], "\t\t", $var['amount'];
}, $array);
Output
Product ID Amount
33 1
34 3
10 1
<table>
<tr>
<th>Product Id</th>
<th>Ammount</th>
</tr>
<?php
foreach ($yourArray as $subAray)
{
?>
<tr>
<td><?php echo $subAray['product_id']; ?></td>
<td><?php echo $subAray['amount']; ?></td>
</tr>
<?php
}
?>
</table>
Try this..
foreach($arr as $a)
{
echo $a['product_id'];
echo $a['amount'];
}
Format as per your requirment.

get values from database and sort them based on other fields

I'm using php and mysql -[codeigniter framework]- and this is my table structure:
Please visit this image to find out table structure
Every single item is separated to three or four parts that these parts made this item's price.
And these items belong to an invoice that will point using refid. refid is invoice_id in fact.
now I need to get a specific invoice items and show them in a table.
this is what I need:
Please visit this image to find out my need
I do not have any problem to get values from database and I will use codeigniter active records.
As you may know, codeigniter database result_array function will give us an associative array. Now I want to separate every item from other and display them in a html table and I have to use rowspan for every item dependence sub items.
Would you please help me to find out how should I use result array to simply show the result in a html table?
so lets assume that you are getting a result array like this,
$result = array(
array(
'id' => 26,
'price' => 100,
'parent' => 0
),
array(
'id' => 27,
'price' => 100,
'parent' => 26
),
array(
'id' => 28,
'price' => 150,
'parent' => 26
),
array(
'id' => 29,
'price' => 100,
'parent' => 26
),
array(
'id' => 30,
'price' => 100,
'parent' => 0
),
array(
'id' => 31,
'price' => 99,
'parent' => 30
),
array(
'id' => 32,
'price' => 75,
'parent' => 30
),
array(
'id' => 33,
'price' => 600,
'parent' => 0
),
array(
'id' => 34,
'price' => 100,
'parent' => 33
),
array(
'id' => 35,
'price' => 50,
'parent' => 33
),
array(
'id' => 36,
'price' => 100,
'parent' => 33
),
);
i cant insert all the data manually in the array from your image, so i have inserted only the needed once,
so now u can do something like this to group the data of same item,
$new_array = array();
$invoice_price = 0;
foreach ($result as $key => $value) {
$invoice_price += $value['price'];
if($value['parent'] == 0){
$new_array[$value['id']]['other_data'][] = $value;
$new_array[$value['id']]['final_price'] = $value['price'];
}else{
$new_array[$value['parent']]['other_data'][] = $value;
$new_array[$value['parent']]['final_price'] += $value['price'];
}
}
now if u print this $new_array, u will get the following output,
Array
(
[26] => Array
(
[other_data] => Array
(
[0] => Array
(
[id] => 26
[price] => 100
[parent] => 0
)
[1] => Array
(
[id] => 27
[price] => 100
[parent] => 26
)
[2] => Array
(
[id] => 28
[price] => 150
[parent] => 26
)
[3] => Array
(
[id] => 29
[price] => 100
[parent] => 26
)
)
[final_price] => 450
)
[30] => Array
(
[other_data] => Array
(
[0] => Array
(
[id] => 30
[price] => 100
[parent] => 0
)
[1] => Array
(
[id] => 31
[price] => 99
[parent] => 30
)
[2] => Array
(
[id] => 32
[price] => 75
[parent] => 30
)
)
[final_price] => 274
)
[33] => Array
(
[other_data] => Array
(
[0] => Array
(
[id] => 33
[price] => 600
[parent] => 0
)
[1] => Array
(
[id] => 34
[price] => 100
[parent] => 33
)
[2] => Array
(
[id] => 35
[price] => 50
[parent] => 33
)
[3] => Array
(
[id] => 36
[price] => 100
[parent] => 33
)
)
[final_price] => 850
)
)
hope u can see that the data is already groups by items.
now just send the data in your view file,
$this->load->view('yourviewfilename',array('data'=>$new_array,'invoice_price'=>$invoice_price));
and to print the table like your screenshot use the following code,
<table border="1">
<tr>
<th>ID</th>
<th>parent</th>
<th>price</th>
<th>final price</th>
</tr>
<?php $count = 0; ?>
<?php foreach ($data as $key => $value) { ?>
<?php $count++; ?>
<?php foreach ($value['other_data'] as $k => $v) { ?>
<tr>
<?php if ($k == 0) { ?>
<td rowspan="<?php echo count($value['other_data']); ?>"><?php echo $count ?></td>
<?php } ?>
<td> <?php echo $v['parent']; ?></td>
<td> <?php echo $v['price']; ?></td>
<?php if ($k == 0) { ?>
<td rowspan="<?php echo count($value['other_data']); ?>"><?php echo $value['final_price']; ?></td>
<?php } ?>
</tr>
<?php } ?>
<?php } ?>
</table>
u will get output like this,
hope u got the point and can print the other data you needed.

PHP Two Arrays Into One Table. First Array Vertical, Second Horizontal

i want result like this in view.
This is (part of) the array:
Array ( [0] => Array ( [car_id] => ferarri [total] => 15 ) [1] => Array ( [car_id] => lamborgini [total] => 10 ) [2] => Array ( [car_id] => ford [total] => 32 ) [3] => Array ( [car_id] => toyota [total] => 45 ) [4] => Array ( [car_id] => viar [total] => 1 ) ) 1
how do I make it look. horizontally displays the total. and vertical displays car brand.
You can use this
<?php
$car_tot = array(
'0' => array (
'car_id' => 'ferarri',
'total' => 15
),
'1' => array (
'car_id' => 'lamborgini',
'total' => 10
),
'2' => array (
'car_id' => 'ford',
'total' => 32
),
'3' => array (
'puskesmas_id' => 'toyota',
'total' => 45
),
'4' => array (
'car_id' => 'viar',
'total' => 1
)
);
echo '<pre>';
print_r( $car_tot );
echo '</pre>';
?>
<table>
<tr>
<th>Type</th>
<th>Total</th>
</tr>
<?php
foreach( $car_tot as $key=>$ctRow )
{
?>
<tr>
<td>
<?= !empty( $ctRow['car_id'] ) ? $ctRow['car_id'] : '';?>
</td>
<td>
<?= !empty( $ctRow['total'] ) ? $ctRow['total'] : '';?>
</td>
</tr>
<?php
}
?>
</table>

Displaying a nested array in an HTML table

This is a simple problem that I've been trying to solve for several hours. I have an array containing information of several students and marks they scored in tests. There are 8 subjects in total, with each subject having 5 parameters.
The array looks like below:
Array
(
[Ron] => Array
(
[subject1] => Array
(
[test1] => 47
[test2] => 86
[total] => 133
[percentage] => 88.67
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 963
[gross_percentage] => 80.25
[...]
[subject8] => Array
(
[test1] => 48
[test2] => 86
[total] => 134
[percentage] => 89.33
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 900
[gross_percentage] => 75.50
)
[John] => Array
(
[subject1] => Array
(
[test1] => 39
[test2] => 72
[total] => 111
[percentage] => 74
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 963
[gross_percentage] => 80.25
[...]
[subject8] => Array
(
[test1] => 39
[test2] => 75
[total] => 114
[percentage] => 76
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 846
[gross_percentage] => 70.5
)
)
I need to display the following in a nicely formatted HTML table (I'm planning to use Bootstrap), but I can't for the life of me figure out how to properly nest the table using PHP.
This is the HTML markup that I currently have: http://jsfiddle.net/9y910uvp/
This gives the following result:
Which is okay.
The Actual Problem
I'm using PHP to display the contents from the array. I'm confused how to display the values for the nested <tr> and <td> sections.
How do I loop through the array and display the contents correctly? (I'd post my attempts so far, but they're all stupid and not working so I'm not posting).
An example would be greatly appreciated because I've spent countless hours trying to get this working, but failed terribly.
Not quite the same output, but here's a recursive approach that should handle any depth of nested arrays...
<?php
$data = Array (
"Ron" => Array (
"subject1" => Array (
"tests" => Array (
"test1" => 47,
"test2" => 86,
"total" => 133,
"percentage" => 88.67,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 963,
"gross_percentage" => 80.25,
),
"subject8" => Array (
"tests" => Array (
"test1" => 48,
"test2" => 86,
"total" => 134,
"percentage" => 89.33,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 900,
"gross_percentage" => 75.50,
),
),
"John" => Array (
"subject1" => Array (
"tests" => Array (
"test1" => 39,
"test2" => 72,
"total" => 111,
"percentage" => 74,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 963,
"gross_percentage" => 80.25,
),
"subject8" => Array (
"tests" => Array (
"test1" => 39,
"test2" => 75,
"total" => 114,
"percentage" => 76,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 846,
"gross_percentage" => 70.5,
),
),
);
print_r($data);
$table = table_cell($data);
echo $table;
function table_cell($data) {
$return = "<table border='1'>";
foreach ($data as $key => $value) {
$return .= "<tr><td>$key</td><td>";
if (is_array($value)) {
$return .= table_cell($value);
} else {
$return .= $value;
}
$return .= "</td><tr>";
}
$return .= "</tr></table>";
return($return);
}
and the table looks nothing like the requested but... it was an interesting excersize...
Try this out
<table border="1">
<thead>
<thead>
<tr>
<th>Name</th>
<th>Subject</th>
<th>Test1 Marks</th>
<th>Test2 Marks</th>
<th>Total Marks</th>
<th>Status</th>
<th>Percentage</th>
<th>Pass Count</th>
<th>Total Percentage</th>
</tr>
</thead>
<tbody>
</tr>
<?php
$numberOfSubjects = 3; //I used 3 subjects. You should change this to 8 for your data.
foreach ($data as $student => $info) {
echo "<tr><td rowspan=$numberOfSubjects />$student</td>";
//flag to let the inner loop the tr has been drawn for the first row
$firstRow = true;
foreach ($info as $key => $value) {
//we only want subject info
if (strpos($key, "subject") === 0) {
if (!$firstRow) {
echo "<tr>";
} //else we already drew it
//its a subject so
echo "<td>$key</td>";
echo "<td>{$value['test1']}</td>";
echo "<td>{$value['test2']}</td>";
echo "<td>{$value['total']}</td>";
echo "<td>{$value['status']}</td>";
echo "<td>{$value['percentage']}</td>";
//only draw total for the first row
if ($firstRow) {
echo "<td rowspan=$numberOfSubjects>{$info['pass_count']}</td>";
echo "<td rowspan=$numberOfSubjects>{$info['gross_percentage']}</td>";
}
//close the row
echo "</tr>";
$firstRow = false;
}
}
}
?>
</tbody>
</table>
Here is the output:
Its based on a sample dataset I constructed from your description, included below for completeness:
<?php
$data = array(
"Ron" => Array
(
"subject1" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 963
, "gross_percentage" => 80.25,
"subject2" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "subject3" => Array
(
"test1" => 48
, "test2" => 86
, "total" => 134
, "percentage" => 89.33
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 900
, "gross_percentage" => 75.50
),
"John" => Array
(
"subject1" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 963
, "gross_percentage" => 80.25,
"subject2" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "subject3" => Array
(
"test1" => 48
, "test2" => 86
, "total" => 134
, "percentage" => 89.33
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 963
, "gross_percentage" => 80.25
)
);
Judging from your array, this might be something like what you're looking for:
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Subject</th>
<th>Test1 Marks</th>
<th>Test2 Marks</th>
<th>Total Marks</th>
<th>Status</th>
<th>Percentage</th>
<th>Pass Count</th>
<th>Total Percentage</th>
</tr>
</thead>
<tbody>
<?php foreach($arr as $name => $subjects): ?>
<?php $i = 0; ?>
<?php foreach($subjects as $subjectName => $subject): ?>
<?php if (is_array($subject)): ?>
<tr>
<?php if ($i === 0): ?>
<td rowspan="8"><?php echo $name; ?></td>
<?php endif; ?>
<td><?php echo $subjectName; ?></td>
<td><?php echo $subject['test1']; ?></td>
<td><?php echo $subject['test2']; ?></td>
<td><?php echo $subject['total']; ?></td>
<td><?php echo $subject['status']; ?></td>
<td><?php echo $subject['percentage']; ?></td>
<?php if ($i === 0): ?>
<td rowspan="8"><?php echo $subjects['pass_count']; ?></td>
<td rowspan="8"><?php echo $subjects['gross_percentage']; ?></td>
<?php endif; ?>
</tr>
<?php endif; ?>
<?php $i++; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</tbody>
</table>

How to print multidimensional arrays in php

I have an array in below format
Array ( [0] => Array ( [product_id] => 33 [amount] => 1 ) [1] => Array ( [product_id] => 34 [amount] => 3 ) [2] => Array ( [product_id] => 10 [amount] => 1 ) )
I want to get output from that array as below format
Product ID Amount
33 1
34 3
10 1
Can anyone please help me regarding this problem. var_dump of the variable is.
array
0 =>
array
'product_id' => string '33' (length=2)
'amount' => string '1' (length=1)
1 =>
array
'product_id' => string '34' (length=2)
'amount' => string '3' (length=1)
2 =>
array
'product_id' => string '10' (length=2)
'amount' => string '1' (length=1)
I believe this is your array
$array = Array (
0 => Array ( "product_id" => 33 , "amount" => 1 ) ,
1 => Array ( "product_id" => 34 , "amount" => 3 ) ,
2 => Array ( "product_id" => 10 , "amount" => 1 ) );
Using foreach
echo "<pre>";
echo "Product ID\tAmount";
foreach ( $array as $var ) {
echo "\n", $var['product_id'], "\t\t", $var['amount'];
}
Using array_map
echo "<pre>" ;
echo "Product ID\tAmount";
array_map(function ($var) {
echo "\n", $var['product_id'], "\t\t", $var['amount'];
}, $array);
Output
Product ID Amount
33 1
34 3
10 1
<table>
<tr>
<th>Product Id</th>
<th>Ammount</th>
</tr>
<?php
foreach ($yourArray as $subAray)
{
?>
<tr>
<td><?php echo $subAray['product_id']; ?></td>
<td><?php echo $subAray['amount']; ?></td>
</tr>
<?php
}
?>
</table>
Try this..
foreach($arr as $a)
{
echo $a['product_id'];
echo $a['amount'];
}
Format as per your requirment.

Categories