I am trying to display the array data in a specific way in a table, so I am needing to format my array like this:
Leafy Life - Aspley - All Green
Callistemon1 - 33 - -
Callistemon2 - - - 3.59
Acronychia - - 22.5 -
I have tried many times, but my knowledge is limited and would really appreciate it if anyone can point me in the right direction, or even if you have time to adjust my code that would be very much appreciated.
<?php
$options = array(
array("plant" => "Callistemon Kings Park Special 300 mm","nursery" => "A Leafy Life","price" => "33"),
array("plant" => "Callistemon Kings Park Special 300 mm","nursery" => "Alpine Nurseries Alstonville","price" => "23"),
array("plant" => "Callistemon Kings Park Special 140 mm","nursery" => "All Green Nursery","price" => "3.59"),
array("plant" => "Acronychia imperforata 300 mm","nursery" => "Aspley Nursery","price" => "22.5"),
array("plant" => "Acronychia imperforata 300 mm","nursery" => "All Green Nursery","price" => "23"),
array("plant" => "Metrosideros collina Little Dugald 140 mm","nursery" => "Accent Plants","price" => "5.25"),
);
$newOptions = array();
foreach ($options as $option) {
$plant = $option['plant'];
$nursery = $option['nursery'];
$price = $option['price'];
$newOptions[$plant][$nursery] = $price;
}
print_r($newOptions);
echo "<table cellpadding='0' cellspacing='0' border='2'>";
echo "<thead>";
echo "<th></th>";
foreach($newOptions as $key => $row)
{
foreach($row as $k => $v)
{
print_r($k);
echo "<th>";
echo $k;
echo "</th>";
}
}
echo "</thead>";
echo "<tbody>";
$i=0;
foreach($newOptions as $keys => $rows)
{
echo "<tr>";
echo "<td>";
echo $keys;
echo "</td>";
foreach($rows as $v)
{
echo "<td>";
echo " ". $i;
echo "</td>";
$i++;
echo "<td>";
echo $v;
echo "</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
We can change structure of the array like this -
$Headers = array();
$headers[1] = "Leafy Life";
$headers[2] = "Aspley";
$headers[3] = "All Green";
$firstCloumnEntries = array();
$firstCloumnEntries[1] = "Callistemon1";
$firstCloumnEntries[2] = "Callistemon2";
$firstCloumnEntries[3] = "Acronychia";
$values = array();
$values[1] = array(); // this will have all values for first row;
$values[1][1] = "33";
$values[2] = array(); // this will have all values for second row;
$values[2][3] = "3.59";
$values[1] = array(); // this will have all values for third row;
$values[3][2] = "22.5";
Now while displaying - first add everything from all column "header" array.
Then for each row - use "firstCloumnEntries" array to get first value using index of the loop.
Write an inner loop to print corresponding row values. Before printing that value make use of isset().
Index "0" is purposely eliminated as we do not have any value in 0th column 0th row.
Index in the header array or in firstCloumnEntries array will help us to understand ID of the element inside it, e.g 1 is ID for "Leafy Life", 2 is for "Aspley" and so on.. using this we can add more headers with new IDs and more firstCloumnEntries with more IDs.
This will help in adding dynamic content and makes looping easy.
Related
I was doing a simple PHP array table, but the result of my code is not what I've expected,
<?php
$names = array (
'First Name' =>
array('Alfre', 'Beka', 'Charlie'),
'Middle Name' =>
array('Brom', 'Tiv', 'Clore'),
'Last Name' =>
array('Adani', 'Filial', 'Chrome'),
);
echo '<table border="1">';
foreach($names as $name => $indv_name) {
echo '<tr>';
echo "<th>$name</th>";
foreach($indv_name as $per_name) {
echo '<td>',$song, '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
The result of the array of this code is echoed horizontally, can anyone help me to echo it vertically?
Here's my output example:
firstname -> value, value, value
middlename -> value, value, value
lastname -> value, value, value
Output that I want to expect:
firstname - middlename - lastname
value - value - value
value - value - value
value - value - value
- value - value
- value
And whenever I add value in the array, it won't break the line.
Sorry for the late edit
A basic way
$names = array (
'First Name' =>
array('Alfre', 'Beka', 'Charlie'),
'Middle Name' =>
array('Brom', 'Tiv', 'Clore'),
'Last Name' =>
array('Adani', 'Filial', 'Chrome'),
);
echo "<table>";
echo "<thead>";
echo "<tr>";
foreach ($names as $key => $value) {
echo "<th>$key</th>";
}
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($names as $key => $value) {
echo "<tr>";
foreach ($value as $key => $value) {
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
My question is referring to my previous question in this link How to echo specific multiple same records only once using foreach loops in PHP? .
This is my current code
Code :
$sqlTXT = "SELECT * FROM TABLE";
$arr_old = DB::getInstance()->FetchArray($sqlTXT);
if(count($arr_old) > 0)
{
$arr = array();
foreach($arr_old as $key => $item)
{
if(!array_key_exists($item['ACCOUNT_NUMBER'], $arr))
{
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['ACCOUNT'] = $item['ACCOUNT'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['CATEGORY'] = $item['CATEGORY'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['VALUE'] = $item['VALUE'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['FUND'] = $item['FUND'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['AMOUNT'] = $item['AMOUNT'];
}
else
{
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['CATEGORY'] .= ",".$item['CATEGORY'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['VALUE'] .= ",".$item['VALUE'];
}
}
ksort($arr, SORT_NUMERIC);
echo "<table>";
echo "<tr>";
echo "<td>ACCOUNT</td>";
echo "<td>CATEGORY</td>";
echo "<td>VALUE</td>";
echo "<td>FUND</td>";
echo "<td>AMOUNT</td>";
echo "</tr>";
foreach($arr as $key => $item)
{
// Display Category
$xpl = explode(",",$item[$key]['CATEGORY']);
$n_category = "";
foreach($xpl as $b => $a){
$n_category .= ($b!=0) ? "<br>".$a : $a ;
}
// Display Value
$trl = explode(",",$item[$key]['VALUE']);
$n_value = "";
foreach($trl as $c => $d){
$n_value .= ($c!=0) ? "<br>".$d : $d ;
// $new = number_format($n_value, 2, '.', ',');
}
echo "<tr>";
echo "<td>".$item[$key]['ACCOUNT']."</td>";
echo "<td>".$n_category."</td>";
echo "<td>".$new."</td>";
echo "<td>".$item[$key]['FUND']."</td>";
echo "<td>".$item[$key]['AMOUNT']."</td>";
echo "</tr>";
}
echo "</table>";
}
And the output as shown as below.
Output :
ACCOUNT CATEGORY VALUE FUND AMOUNT
0001 Category1 10000 BIN 300,000.00
Category2 0
Category3 500
Category4 15000
0002 Category1 8500 BIN 70,000.00
Category2 7000
Category3 100
Category4 0
But my current issue is I can't convert the VALUE column into 2 decimals format. When I added code
$new = number_format($n_value, 2, '.', ',');
The output only shows the first value of array.
I hope that someone can help me to solve this issue.
Thanks in advance.
You need to convert each value in the column independently. Try changing your foreach loop to this:
foreach($trl as $c => $d){
if (is_numeric($d)) $d = number_format($d, 2, '.', ',');
$n_value .= ($c!=0) ? "<br>".$d : $d ;
}
I tried to create a table that calculating the data which stored in mutli-dimensional arrays' value and show the result by table format.
I don't know how to count the values which stored in the array's array(third level array's value.
Can I using the function to count values in the mutli-dimensional arrays' value? Hope anyone can teach me, thanks.
<?php
$itemList = array
("book"=>array("ID"=>"14567",
"name"=>array("History"=>array("American"=>12,"Europe"=>2), "SF"=>array("Space"=>32), "Chinese"=>array("kungFu"=>10))),
"stationary"=>array("ID"=>"24547", "name"=>array("HB"=>array("ABC"=>123, "MC"=>161,"GCD"=>26)))
);
$item = "<table border=1>";
$item .= "<td>item count(s)</td>";
foreach($itemList as $key => $value){
$item .= "<tr>";
$item .= "<td align=center>" .
/*count the categories of the items(e.g. American,Europe,Space,Kungfu show
the result: "4")*/
. "</td>";
$item .= "</tr>";
}
$item .= "</table>";
echo $item;
?>
foreach($itemList['book']['name'] as $key => $value)
{
foreach ($itemList['book']['name'][$key] as $key1 => $value1) {
$category[] = $key1;
$value2[] = $value1;
}
}
echo count($category);
here you got the total numebr of count of your category
I have a multidimensional array where each key holds another array as value. I printed each key value pairs in separate tables. But I get unwanted space between tables (spacing between any two tables are not the same). How can I eliminate spacing as a whole?
foreach ($stockist as $key => $value)
{
echo "<table align='center' border='1'>";
echo "<tr><td align = 'center'> <font color = 'blue'> $key</td></tr>";
foreach($value as $key1 => $value1)
{
echo "<tr><td align ='center'>$value1</td></tr><br>";
}
echo "</table>";
}
This should get it all into a single table.
PHP
echo '<table>';
foreach ($stockist as $key => $value)
{
echo '<tr><td>'.$key.'</td></tr>';
foreach($value as $key1 => $value1)
{
echo '<tr><td>'.$value1.'</td></tr>';
}
}
echo '</table>;
I fetched record from mysql and resultent is in array:
Array ( [Logodesign] => 130 [Webdesign] => 80 [Printdesign] => 72 [Illustrationdesign] => 49 [iPhoneportfolio] => 23 )
and I want to show all categories with their total rows in tag like
to do so i coded:
$q = mysql_query("SELECT
SUM(IF(cat = 'logodesignportfolio',1,0)) AS Logodesign,
SUM(IF(cat = 'websitedesignportfolio',1,0)) AS Webdesign,
SUM(IF(cat = 'printdesignportfolio',1,0)) AS Printdesign,
SUM(IF(cat = 'illustrationdesignportfolio',1,0)) AS Illustrationdesign,
SUM(IF(cat = 'iphoneportfolio',1,0)) AS iPhoneportfolio
FROM portfolio");
$f = mysql_fetch_assoc($q);
print_r($f);
echo "<br />";
echo "<select>";
foreach($f as $k){
echo "<option>".$f.' - '.$k."</option>";
}
echo "</select>";
but it shows:
Use this:
foreach($f as $k => $a){
echo "<option>".$k.' - '.$a."</option>";
}