I have the code below, but my problem is i received PHP Notice: Undefined offset: (from 1 to 7) error message and what is very important for me to get country ID only once with the max number. its counting the number of projects and increase everytime one which is correct and what i need, but need to display it only once how does it display the correct result show be similar to: the correct result
here is my code:-
<table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$country_counts=[];
$count_country =0;
$country_count_each=0;
$ids=array();// Store unique country_id values here
foreach( $projects as $project ) {
$country_id = $project['Project']['country_id'];
# Check if the country_id is NOT in the array and display if OK.
if( isset( $country_counts[ $country_id ] ) || !in_array( $country_id, $ids ) ) {
$country_counts[$country_id]++;
$country_count_each = $project['Project']['country_id'];
if($project['Project']['country_id']==$country_count_each){
$count_country+=$country_count_each;
$ids[]=$country_id;
//echo $country_counts[$country_id];
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
}
}else {
$country_counts[$country_id] =$country_id;
}
}
$proects_num = count($projects);
?>
</table>
<?php
echo '<br>' .'Total projects numbers are: ' . $proects_num .'<br>';
echo $html->link('Home ', $this->webroot.'countries/index/');
?>
the table in database Table of projects
I need a help in that please
Your problem lies where you are trying to loop and count and echo the table in the same loop... you cannot :)
You need to prepare the data before you output it.
You need to know the amount of occurrences each country code appears in beforehand (before you start the table output loop....).
Adding another loop to prepare the data is an easy option.
I created a code paraphrasing your code somewhat (created some data in the beginning) : In this example I added one more loop ... now we have two.
Will output: