Hi, I’ve got the following array, which comes from a database query
Array
(
[0] => Array
(
[proprietario] => Filippo Rossi
[interno] => A3
[Tabella Generale] => 6.000,0.228000000
[Tabella A] => 0.000
[Tabella B] => 0.000
[Tabella C] => 0.000
[total] => 0.228000000
)
[1] => Array
(
[proprietario] => Giuseppe Rossi
[interno] => A4
[Tabella Generale] => 2.000,0.076000000
[Tabella A] => 8.000,0.040000000
[Tabella B] => 0.000
[Tabella C] => 0.000
[total] => 0.040000000,0.076000000
)
)
I need to convert it to a html table possibily with twig and the table should look like this
The problem is also that the headers (tabella generale, tabella a, tabella b, tabella c) are non always the same.
I don’t really know where to start. Many thanks for your support
vincekaribusana:
The problem is also that the headers (tabella generale, tabella a, tabella b, tabella c) are non always the same.
Not always the same for each “row” of the array?
Hi @droopsnoot thanks for your reply, yes the array can be different for example it can look like:
Array
(
[0] => Array
(
[proprietario] => Filippo Rossi
[interno] => A3
[Tabella F] => 6.000,0.228000000
[Tabella G] => 3.000, 0.2340000
[total] => 0.228000000, 0.234000
)
[1] => Array
(
[proprietario] => Giuseppe Rossi
[interno] => A4
[Tabella F] => 2.000,0.076000000
[Tabella G] => 8.000,0.040000000
[total] => 0.040000000,0.076000000
)
)
What am I missing? I meant can the the column headers be different on each row of the array - in this picture and the first, row zero and row one have the same columns in them.
I imagine that if they are a return from a query, it was a pointless question as that will have the same number of columns for each row. I was just clarifying what you meant by “different headers”.
The only complexity I see is where you have more than one value in some of the fields, and need to split that into separate table columns. I think you’d have to run a pre-pass down every row to count how many values there are in each column, so as to know how to handle your “colspan” value. (I may also have shown that I’m years out of touch with html, and maybe there’s a different way to get a table column header to span more than one column now.)
What have you tried so far?
droopsnoot:
The only complexity I see is where you have more than one value in some of the fields, and need to split that into separate table columns. I think you’d have to run a pre-pass down every row to count how many values there are in each column, so as to know how to handle your “colspan” value. (I may also have shown that I’m years out of touch with html, and maybe there’s a different way to get a table column header to span more than one column now.)
Hi, yes but also for the key “total” i need to sum the values separted by comma
I’m not sure how to do it, at the moment I’ve only executed a foreach and processed using twig, this is the code:
<table class="table" id="millesimalTablesReport">
<thead>
<tr>
{% for titolo in flatExpensesReport|first|keys %}
<th class="text-uppercase text-11 hide-column">{{titolo}} </th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for sub_array in flatExpensesReport %}
<tr>
{% for value in sub_array %}
<td class="font-weight-bold text-10">{{ value|raw }}</th>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Ah, OK. I can’t help you with that, I’ve never heard of Twig.
Do you think if I convert the array to look like the following it could work?
Array
(
[0] => Array
(
[proprietario] => Filippo Rossi
[interno] => A3
[Tabella Generale] => array(
[millesimi] => 6.000,
[spesa] =>0.228000000
)
[Tabella A] => array(
[millesimi] => 0.000,
[spesa] => 0.000
)
[Tabella B] => array(
[millesimi] => 0.000,
[spesa] => 0.000
)
[Tabella C] => array(
[millesimi] => 0.000,
[spesa] => 0.000
)
[total] => 0.228000000
)
[1] => Array
(
[proprietario] => Giuseppe Rossi
[interno] => A4
[Tabella Generale] => array(
[millesimi] => 2.000,
[spesa] => 0.76000000
)
[Tabella A] => array(
[millesimi] => 8.000,
[spesa] => 0.0400000
)
[Tabella B] => array(
[millesimi] => 0.000,
[spesa] => 0.000
)
[Tabella C] => array(
[millesimi] => 0.000,
[spesa] => 0.000
)
[total] => 0.116
)
)
So far I’ve only managed to do the following:
$myarray = array();
$i = 0;
foreach ($flatsRipartoExpensesReport as $value) {
foreach ($value as $pippo) {
$searchForValue = ',';
if(strpos($pippo, $searchForValue) !== false ) {
$myArray1 = explode(',', $pippo);
}
}
$res = explode(',',$value['totaleSpese']);
$result = array_sum($res);
$myarray[$i]["total"]= $result;
$myarray[$i]["interno"]= $value['interno'];
$myarray[$i]["proprietario"]= $value['proprietario'];
$i++;
}
Which gives me this new array:
Array
(
[0] => Array
(
[total] => 0.228
[interno] => A3
[proprietario] => Filippo Rossi
)
[1] => Array
(
[total] => 0.116
[interno] => A4
[proprietario] => Giuseppe Rossi
)
)
system
Closed
December 10, 2020, 12:24am
8
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.