Nov 20, 2022

# Solution 1 :

Before you get into the printing loop you should find how many rows for this district you have.

``````<?php
foreach (\$data as \$key => \$value) {
if (\$value->district == 'Arghakhachi') {
\$district[\$value->district]=\$value;
}
}
``````

And keep in mind that you should omit the first td for the next rows.

``````foreach (\$district['Arghakhachi'] as \$key=> \$value) {
if(\$key==0){
?>
<tr>
<td rowspan="<?=count(\$district['Arghakhachi'])?>"><?= \$value->district ?></td>
<td><?= \$value->fiscal_Year ?></td>
<td><?= \$value->animal_distributed_total ?></td>
<td><?= \$value->total_animal_ai_ni_total ?></td>
<td><?= \$value->ni_conceived_animal_total ?></td>
<td><?= \$value->ni_produced_animal_total_female_male ?></td>
</tr>
<?php }else{ ?>
<tr>
<td><?= \$value->fiscal_Year ?></td>
<td><?= \$value->animal_distributed_total ?></td>
<td><?= \$value->total_animal_ai_ni_total ?></td>
<td><?= \$value->ni_conceived_animal_total ?></td>
<td><?= \$value->ni_produced_animal_total_female_male ?></td>
</tr>
<?php } ?>
``````

I did not test this. but I am sure that will work maybe with some small modifications.

# Problem :

``````<?php
foreach (\$data as \$key => \$value) {
if (\$value->district == 'Arghakhachi') {
\$count += count(\$value->district);
?>
<tr>
<!-- <td><?= \$key + 1 ?></td> -->
<td rowspan="<?php (\$key == 0) ? echo \$count : '' ?>"><?= \$value->district ?></td>
<td><?= \$value->fiscal_Year ?></td>
<td><?= \$value->animal_distributed_total ?></td>
<td><?= \$value->total_animal_ai_ni_total ?></td>
<td><?= \$value->ni_conceived_animal_total ?></td>
<td><?= \$value->ni_produced_animal_total_female_male ?></td>
</tr>
<?php }} ?>
``````

I want the first td to merge the rows based on the total count value only if the district name is Argakhanchi. I am getting all the output as desired leaving the rowspan.
Since the td is inside the loop, the code gives me increment of count on every loop but I want that as a total count at once.

Can anybody help me with this please?

The value stored in \$data.

``````Array
(
[0] => stdClass Object
(
[fiscal_Year] => 2071/72
[district] => Arghakhachi
[animal_distributed_total] => 0
[ai_animal_total] => 232
[ai_convicted_animal_total_female_male] => 59
[ni_animal_total] => 61
[total_animal_ai_ni_female_male] => 0
[total_animal_ai_ni_total] => 0
[ni_conceived_animal_total] => 0
[ni_produced_animal_total_female_male] => 0
)

[1] => stdClass Object
(
[fiscal_Year] => 2072/73
[district] => Arghakhachi
[animal_distributed_total] => 12
[ai_animal_total] => 785
[ai_convicted_animal_total_female_male] => 198
[ni_animal_total] => 217
[total_animal_ai_ni_female_male] => 0
[total_animal_ai_ni_total] => 0
[ni_conceived_animal_total] => 0
[ni_produced_animal_total_female_male] => 0
)

[2] => stdClass Object
(
[fiscal_Year] => 2072/73
[district] => Gulmi
[animal_distributed_total] => 2
[ai_animal_total] => 831
[ai_convicted_animal_total_female_male] => 129
[ni_animal_total] => 285
[total_animal_ai_ni_female_male] => 0
[total_animal_ai_ni_total] => 0
[ni_conceived_animal_total] => 0
[ni_produced_animal_total_female_male] => 0
)

[3] => stdClass Object
(
[fiscal_Year] => 2072/73
[district] => Pyuthan
[animal_distributed_total] => 0
[ai_animal_total] => 123
[ai_convicted_animal_total_female_male] => 0
[ni_animal_total] => 0
[total_animal_ai_ni_female_male] => 0
[total_animal_ai_ni_total] => 0
[ni_conceived_animal_total] => 0
[ni_produced_animal_total_female_male] => 0
)

[4] => stdClass Object
(
[fiscal_Year] => 2072/73
[district] => Rolpa
[animal_distributed_total] => 2
[ai_animal_total] => 119
[ai_convicted_animal_total_female_male] => 21
[ni_animal_total] => 29
[total_animal_ai_ni_female_male] => 0
[total_animal_ai_ni_total] => 0
[ni_conceived_animal_total] => 0
[ni_produced_animal_total_female_male] => 0
)

[5] => stdClass Object
(
[fiscal_Year] => 2073/74
[district] => Arghakhachi
[animal_distributed_total] => 8
[ai_animal_total] => 869
[ai_convicted_animal_total_female_male] => 251
[ni_animal_total] => 278
[total_animal_ai_ni_female_male] => 0
[total_animal_ai_ni_total] => 0
[ni_conceived_animal_total] => 0
[ni_produced_animal_total_female_male] => 0
)

[6] => stdClass Object
(
[fiscal_Year] => 2073/74
[district] => Gulmi
[animal_distributed_total] => 8
[ai_animal_total] => 803
[ai_convicted_animal_total_female_male] => 148
[ni_animal_total] => 213
[total_animal_ai_ni_female_male] => 0
[total_animal_ai_ni_total] => 0
[ni_conceived_animal_total] => 0
[ni_produced_animal_total_female_male] => 0
)
``````

SQL Code :

``````select
bin.fiscal_Year,bin.district
,sum(bin.animal_distributed_total)  animal_distributed_total
,sum(bin.ai_animal_total)  ai_animal_total
,(sum(bin.ai_convicted_animal_total_male) + sum(ai_convicted_animal_total_female) )  ai_convicted_animal_total_female_male
,sum(bin.ni_animal_total) ni_animal_total
,(sum(bin.total_animal_ai_ni_male) + sum(bin.total_animal_ai_ni_female) ) total_animal_ai_ni_female_male
,sum(bin.total_animal_ai_ni_total)  total_animal_ai_ni_total
,sum(bin.ni_conceived_animal_total) ni_conceived_animal_total
,(sum(bin.ni_produced_animal_total_male)+sum(bin.ni_produced_animal_total_female) ) ni_produced_animal_total_female_male
from breed_improvement_new bin
group by bin.fiscal_Year,bin.district
``````

### Comment posted by Devsi Odedra

can you show the data stored in

if

### Comment posted by Meeraj Adhikari

I have updated the question with the value of \$data being fetched.

### Comment posted by misorude

“only if the district name is Argakhanchi”

Use