Remove duplication and show max count

54 Views Asked by At

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

1

There are 1 best solutions below

0
Shlomtzion On BEST ANSWER

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.

    <?php 

    $projects['Project'][0]['id'] = 1;
    $projects['Project'][0]['project_name'] = "musem something";
    $projects['Project'][0]['project_price'] = 10;
    $projects['Project'][0]['project_deduction'] = 30;
    $projects['Project'][0]['country_id'] = 1;

    $projects['Project'][1]['id'] = 2;
    $projects['Project'][1]['project_name'] = "musem nid";
    $projects['Project'][1]['project_price'] = 5;
    $projects['Project'][1]['project_deduction'] = 25;
    $projects['Project'][1]['country_id'] = 1;

    $projects['Project'][2]['id'] = 3;
    $projects['Project'][2]['project_name'] = "khalefa tower";
    $projects['Project'][2]['project_price'] = 20;
    $projects['Project'][2]['project_deduction'] = 50;
    $projects['Project'][2]['country_id'] = 2;

    $projects['Project'][3]['id'] = 4;
    $projects['Project'][3]['project_name'] = "gold market";
    $projects['Project'][3]['project_price'] = 15;
    $projects['Project'][3]['project_deduction'] = 40;
    $projects['Project'][3]['country_id'] = 2;

    /*Country names*/
    $cNames[1] = "Holland"; 
    $cNames[2] = "Dubai";

    echo '<pre>';
    print_r($projects); /* print the array */

    $country_counts=[];
    $count_country =0;
    $country_count_each=0;
    $ids=array();// Store unique country_id values here
?><table>
<tr>
    <th>Country ID</th>
    <th>Country Name</th>
    <th>Number of Place</th>
</tr><?php
    /* Loop to create table data */
    foreach( $projects['Project'] as $p ) {
        /* create new table data array with the country code as its key */
        if(!isset($tableArray[$p['country_id']])){
            $tableArray[$p['country_id']]['count'] = 1;
        } else {
            ++$tableArray[$p['country_id']]['count'];
        }
    }
    print_r($tableArray); /* print table data */
    /* Loop to create table */
    foreach($tableArray as $k => $line){
        ?>
        <tr>
            <td style="width: 30%"><?php echo $k; ?></td>
            <td style="width: 30%"><?php echo $cNames[$k] ?></td>
            <td style="width: 30%"><?php echo $line['count']; ?></td>
        </tr>
    <?php       
    }

    ?>

</table>

<?php 
    $proects_num = count($projects['Project']); 
    echo '<br>' .'Total projects numbers are: ' . $proects_num .'<br>'; 
    echo '<br>' .'Total countries are: ' . count($tableArray) .'<br>'; 
    echo $html->link('Home ', $this->webroot.'countries/index/');
?>

Will output:

Array
(
    [Project] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [project_name] => musem something
                    [project_price] => 10
                    [project_deduction] => 30
                    [country_id] => 1
                )

            [1] => Array
                (
                    [id] => 2
                    [project_name] => musem nid
                    [project_price] => 5
                    [project_deduction] => 25
                    [country_id] => 1
                )

            [2] => Array
                (
                    [id] => 3
                    [project_name] => khalefa tower
                    [project_price] => 20
                    [project_deduction] => 50
                    [country_id] => 2
                )

            [3] => Array
                (
                    [id] => 4
                    [project_name] => gold market
                    [project_price] => 15
                    [project_deduction] => 40
                    [country_id] => 2
                )

        )

)
Array
(
    [1] => Array
        (
            [count] => 2
        )

    [2] => Array
        (
            [count] => 2
        )

)
        
Country ID  Country Name    Number of Place
1           Holland         2
2           Dubai           2



Total projects numbers are: 4

Total countries are: 2