1

I have created an array which I need to show a list of projects for the current user. Each project is made up of stages, each stage can have multiple KPIs assigned to it.

As you can see the stages are appended to the array fine and for the most part so are the KPIs.

The exception to this is with KPIs that are not stage specific. When I have this type of KPI is should be assigned to the project section, as it spans multiple stages.

KPIs are assigned to stages by the PSID, if the id is null then it is a project wide KPI.

The issue I'm having is with the project wide KPIs. They are repeated, I need each KPI to appear once in the array.

I've tried to check the previously pushed array element with the currently pushed one in order to check.

$i=0;
    foreach($projects as $project){
        $projectStages = $this->ProjectStages->find('all',
                array(
                        'conditions' => array(
                            'pid' => $project['Projects']['pid']
                        )
                )
        );
        array_push($projects[$i], $projectStages);


        $n=0;
        foreach($projectStages as $stage){
            $projectKPIs = $this->ProjectKPIs->find('all',
                    array(
                            'conditions' => array(
                                    'nPid' => $project['Projects']['pid']
                            )
                    )
            );  

            foreach($projectKPIs as $projectKPI){
                if(empty($projectKPI['ProjectKPIs']['nPsid'])){

                    $N = $n-1;
                    $curpkid = $projectKPI['ProjectKPIs']['pkid'];

                    if(isset($projects[$i]['Projects'][$N]['ProjectKPIs']['pkid'])){
                        $prevpkid = $projects[$i]['Projects'][$N]['ProjectKPIs']['pkid'];
                    }else{
                        $prevpkid = null;
                    }

                    if($curpkid != $prevpkid){
                        echo 'cur: '.$curpkid.' - prev: '.$prevpkid;
                        array_push($projects[$i]['Projects'], $projectKPI);
                    }

                }else if($projectKPI['ProjectKPIs']['nPsid'] == $stage['ProjectStages']['psid']){
                    array_push($projects[$i][$i][$n]['ProjectStages'], $projectKPI);
                }

            }



            $n++;
        }


        $i++;
    }

Below is the code I'm using and the array I'm currently getting back:

Array
(
[0] => Array
    (
        [Projects] => Array
            (
                [pid] => 811
                [name] => Kpi Project
                [description] => This Project Has Kpis
                [scheduledStartDate] => 2015-05-01
                [scheduledEndDate] => 2015-06-01
                [actualStartDate] => 
                [actualEndDate] => 
                [pmid] => 1137
                [0] => Array
                    (
                        [ProjectKPIs] => Array
                            (
                                [pkid] => 37
                                [nPsid] => 
                                [type] => 1
                                [start] => 2015-05-03
                                [end] => 2015-05-09
                                [nPid] => 811
                                [nKid] => 7
                            )

                    )

                [1] => Array
                    (
                        [ProjectKPIs] => Array
                            (
                                [pkid] => 37
                                [nPsid] => 
                                [type] => 1
                                [start] => 2015-05-03
                                [end] => 2015-05-09
                                [nPid] => 811
                                [nKid] => 7
                            )

                    )

                [2] => Array
                    (
                        [ProjectKPIs] => Array
                            (
                                [pkid] => 37
                                [nPsid] => 
                                [type] => 1
                                [start] => 2015-05-03
                                [end] => 2015-05-09
                                [nPid] => 811
                                [nKid] => 7
                            )

                    )

                [3] => Array
                    (
                        [ProjectKPIs] => Array
                            (
                                [pkid] => 37
                                [nPsid] => 
                                [type] => 1
                                [start] => 2015-05-03
                                [end] => 2015-05-09
                                [nPid] => 811
                                [nKid] => 7
                            )

                    )

                [4] => Array
                    (
                        [ProjectKPIs] => Array
                            (
                                [pkid] => 37
                                [nPsid] => 
                                [type] => 1
                                [start] => 2015-05-03
                                [end] => 2015-05-09
                                [nPid] => 811
                                [nKid] => 7
                            )

                    )

            )

        [pm] => Array
            (
                [first_name] => Mr
                [last_name] => A
            )

        [0] => Array
            (
                [0] => Array
                    (
                        [ProjectStages] => Array
                            (
                                [psid] => 99
                                [pid] => 811
                                [stageID] => 1
                                [name] => 1
                                [description] => 1
                                [label] => 1
                                [actualStartDate] => 0000-00-00
                                [scheduledStartDate] => 2015-05-01
                                [actualEndDate] => 0000-00-00
                                [scheduledEndDate] => 2015-05-08
                                [prevStage] => n
                                [keyStage] => n
                                [0] => Array
                                    (
                                        [ProjectKPIs] => Array
                                            (
                                                [pkid] => 32
                                                [nPsid] => 99
                                                [type] => 0
                                                [start] => 
                                                [end] => 
                                                [nPid] => 811
                                                [nKid] => 2
                                            )

                                    )

                            )

                    )

                [1] => Array
                    (
                        [ProjectStages] => Array
                            (
                                [psid] => 100
                                [pid] => 811
                                [stageID] => 2
                                [name] => 2
                                [description] => 2
                                [label] => 2
                                [actualStartDate] => 0000-00-00
                                [scheduledStartDate] => 2015-05-08
                                [actualEndDate] => 0000-00-00
                                [scheduledEndDate] => 2015-05-15
                                [prevStage] => n
                                [keyStage] => n
                                [0] => Array
                                    (
                                        [ProjectKPIs] => Array
                                            (
                                                [pkid] => 33
                                                [nPsid] => 100
                                                [type] => 0
                                                [start] => 
                                                [end] => 
                                                [nPid] => 811
                                                [nKid] => 4
                                            )

                                    )

                            )

                    )

                [2] => Array
                    (
                        [ProjectStages] => Array
                            (
                                [psid] => 101
                                [pid] => 811
                                [stageID] => 3
                                [name] => 3
                                [description] => 3
                                [label] => 3
                                [actualStartDate] => 0000-00-00
                                [scheduledStartDate] => 2015-05-15
                                [actualEndDate] => 0000-00-00
                                [scheduledEndDate] => 2015-05-22
                                [prevStage] => n
                                [keyStage] => n
                                [0] => Array
                                    (
                                        [ProjectKPIs] => Array
                                            (
                                                [pkid] => 34
                                                [nPsid] => 101
                                                [type] => 0
                                                [start] => 
                                                [end] => 
                                                [nPid] => 811
                                                [nKid] => 5
                                            )

                                    )

                            )

                    )

                [3] => Array
                    (
                        [ProjectStages] => Array
                            (
                                [psid] => 102
                                [pid] => 811
                                [stageID] => 4
                                [name] => 4
                                [description] => 4
                                [label] => 4
                                [actualStartDate] => 0000-00-00
                                [scheduledStartDate] => 2015-05-22
                                [actualEndDate] => 0000-00-00
                                [scheduledEndDate] => 2015-05-29
                                [prevStage] => n
                                [keyStage] => n
                                [0] => Array
                                    (
                                        [ProjectKPIs] => Array
                                            (
                                                [pkid] => 35
                                                [nPsid] => 102
                                                [type] => 0
                                                [start] => 
                                                [end] => 
                                                [nPid] => 811
                                                [nKid] => 6
                                            )

                                    )

                            )

                    )

                [4] => Array
                    (
                        [ProjectStages] => Array
                            (
                                [psid] => 103
                                [pid] => 811
                                [stageID] => 5
                                [name] => 5
                                [description] => 5
                                [label] => 5
                                [actualStartDate] => 0000-00-00
                                [scheduledStartDate] => 2015-05-29
                                [actualEndDate] => 0000-00-00
                                [scheduledEndDate] => 2015-06-01
                                [prevStage] => n
                                [keyStage] => n
                                [0] => Array
                                    (
                                        [ProjectKPIs] => Array
                                            (
                                                [pkid] => 36
                                                [nPsid] => 103
                                                [type] => 0
                                                [start] => 
                                                [end] => 
                                                [nPid] => 811
                                                [nKid] => 8
                                            )

                                    )

                            )

                    )

            )

    )

[1] => Array
    (
        [Projects] => Array
            (
                [pid] => 572
                [name] => Sgh
                [description] => Dfgh
                [scheduledStartDate] => 2015-04-01
                [scheduledEndDate] => 2015-05-01
                [actualStartDate] => 
                [actualEndDate] => 
                [pmid] => 3304
            )

        [pm] => Array
            (
                [first_name] => Mr
                [last_name] => Brown
            )

        [0] => Array
            (
            )

    )

)

1 Answer 1

1

It seems that you are retrieving the same exact database results multiple times. I'm looking at the find in this code block:

    foreach($projectStages as $stage){
            $projectKPIs = $this->ProjectKPIs->find('all',
                    array(
                            'conditions' => array(
                                    'nPid' => $project['Projects']['pid']
                            )
                    )
            ); 

            // ...
    }

It looks to me that you are going to get the same result set for $projectKPIs on each iteration of the foreach $projectStages because the find for ProjectKPIs is using the project id and nothing to do with whatever the current $stage is.

So effectively for every stage you then loop over all the same KPIs again, causing your logic to get convoluted trying to avoid duplicates.

Perhaps this can be simplified by restructuring your foreach loops. I would ditch the outer foreach($projectStages as $stage) and then iterate over all the KPIs only once. Then inside that loop iterate over each stage only when you need to test against the $stage['ProjectStages']['psid'], which may actually be unnecessary at that point.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This led me right to the answer. I simply matched the project KPIs query with the psid and then for the project wide KPIs I ran a separate query to match psid where null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.