1

I created db query that joins four tables and returns the following results in an array in codeigniter.

Array
(
[results] => Array
    (
        [0] => stdClass Object
            (
                [slip_id] => 257
                [slip_num] => 32
                [project_no] => SWS153
                [client_id] => 1
                [dwg_id] => 1
                [user_id] => 1
                [issue_date] => 2015-10-25 15:00:32.000000
                [sws_dwg_no] => SWS153-100
                [client_dwg_no] => 
                [dwg_title] => Pipework Suction and  Discharge: General Arrangement
                [dwg_by] => 
                [dwg_rev] => D
                [dwg_date] => 2015-10-25 15:00:32
                [rev_id] => 143
                [clientName] => R
                [clientSurname] => C
                [clientCom] => Pumps and Valves (Pty.) Ltd.
                [id] => 1
                [email] => [email protected]
                [password] => 5f4dcc3b5aa765d61d8327deb882cf99
                [name] => Admin
                [lastname] => Admin
                [project_name] => Metalong Raw Water Pump Station
            )

            [1] => stdClass Object
            (
                [slip_id] => 258
                [slip_num] => 32
                [project_no] => SWS153
                [client_id] => 1
                [dwg_id] => 2
                [user_id] => 1
                [issue_date] => 2015-10-25 15:00:32.000000
                [sws_dwg_no] => SWS153-107
                [client_dwg_no] => 
                [dwg_title] => Pipework isolation valve chamber & surg vessel
                [dwg_by] => 
                [dwg_rev] => D
                [dwg_date] => 2015-10-25 15:00:32
                [rev_id] => 144
                [clientName] => R
                [clientSurname] => C
                [id] => 1
                [email] => [email protected]
                [password] => 5f4dcc3b5aa765d61d8327deb882cf99
                [name] => Admin
                [lastname] => Admin
                [project_name] => Metalong Raw Water Pump Station
            )

        [2] => stdClass Object
            (
                [slip_id] => 259
                [slip_num] => 32
                [project_no] => SWS153
                [client_id] => 1
                [dwg_id] => 3
                [user_id] => 1
                [issue_date] => 2015-10-25 15:00:32.000000
                [sws_dwg_no] => SWS153-112
                [client_dwg_no] => 
                [dwg_title] => Pipework discharge pipe pieces plan and details
                [dwg_by] => 
                [dwg_rev] => D
                [dwg_date] => 2015-10-25 15:00:32
                [rev_id] => 145
                [clientName] => R
                [clientSurname] => C
                [id] => 1
                [email] => [email protected]
                [password] => 5f4dcc3b5aa765d61d8327deb882cf99
                [name] => Admin
                [project_name] => Metalong Raw Water Pump Station
            )

        [3] => stdClass Object
            (
                [slip_id] => 260
                [slip_num] => 32
                [project_no] => SWS153
                [client_id] => 1
                [dwg_id] => 4
                [user_id] => 1
                [issue_date] => 2015-10-25 15:00:32.000000
                [sws_dwg_no] => SWS153-113
                [client_dwg_no] => 
                [dwg_title] => Pipework suction and discharge setting out layout
                [dwg_by] => 
                [dwg_rev] => D
                [dwg_date] => 2015-10-25 15:00:32
                [rev_id] => 146
                [clientName] => R
                [clientSurname] => C
                [id] => 1
                [email] => [email protected]
                [password] => 5f4dcc3b5aa765d61d8327deb882cf99
                [name] => Admin
                [project_name] => Metalong Raw Water Pump Station
            )

        [4] => stdClass Object
            (
                [slip_id] => 261
                [slip_num] => 32
                [project_no] => SWS153
                [client_id] => 3
                [dwg_id] => 1
                [user_id] => 1
                [issue_date] => 2015-10-25 15:00:32.000000
                [sws_dwg_no] => SWS153-100
                [client_dwg_no] => 
                [dwg_title] => Pipework Suction and  Discharge: General Arrangement
                [dwg_by] => 
                [dwg_rev] => D
                [dwg_date] => 2015-10-25 15:00:32
                [rev_id] => 143
                [clientName] => F
                [clientSurname] => N
                [id] => 1
                [email] => [email protected]
                [password] => 5f4dcc3b5aa765d61d8327deb882cf99
                [name] => Admin 
                [project_name] => Metalong Raw Water Pump Station
            )

        [5] => stdClass Object
            (
                [slip_id] => 262
                [slip_num] => 32
                [project_no] => SWS153
                [client_id] => 3
                [dwg_id] => 2
                [user_id] => 1
                [issue_date] => 2015-10-25 15:00:32.000000
                [sws_dwg_no] => SWS153-107
                [client_dwg_no] => 
                [dwg_title] => Pipework isolation valve chamber & surge vessel
                [dwg_by] => 
                [dwg_rev] => D
                [dwg_date] => 2015-10-25 15:00:32
                [rev_id] => 144
                [clientName] => F
                [clientSurname] => N
                [id] => 1
                [email] => [email protected]
                [password] => 5f4dcc3b5aa765d61d8327deb882cf99
                [name] => Admin
                [project_name] => Metalong Raw Water Pump Station
            )

        [6] => stdClass Object
            (
                [slip_id] => 263
                [slip_num] => 32
                [project_no] => SWS153
                [client_id] => 3
                [dwg_id] => 3
                [user_id] => 1
                [issue_date] => 2015-10-25 15:00:32.000000
                [sws_dwg_no] => SWS153-112
                [client_dwg_no] => 
                [dwg_title] => Pipework discharge pipe pieces plan and details
                [dwg_by] => 
                [dwg_rev] => D
                [dwg_date] => 2015-10-25 15:00:32
                [rev_id] => 145
                [clientName] => F
                [clientSurname] => N
                [id] => 1
                [email] => [email protected]
                [password] => 5f4dcc3b5aa765d61d8327deb882cf99
                [name] => Admin
                [project_name] => Metalong Raw Water Pump Station
            )

        [7] => stdClass Object
            (
                [slip_id] => 264
                [slip_num] => 32
                [project_no] => SWS153
                [client_id] => 3
                [dwg_id] => 4
                [user_id] => 1
                [issue_date] => 2015-10-25 15:00:32.000000
                [sws_dwg_no] => SWS153-113
                [client_dwg_no] => 
                [dwg_title] => Pipework suction and discharge setting out layout
                [dwg_by] => 
                [dwg_rev] => D
                [dwg_date] => 2015-10-25 15:00:32
                [rev_id] => 146
                [clientName] => F
                [clientSurname] => N
                [id] => 1
                [email] => [email protected]
                [password] => 5f4dcc3b5aa765d61d8327deb882cf99
                [name] => Admin
                [project_name] => Metalong Raw Water Pump Station
            )

    )

 )

I want to create an associative array using the client_id so I can group al the information for a client together and display it together. This is to produce a receipt. The same documents can be sent to different clients, so the receipt must be displayed per client.

I have tried to create a foreach loop to reorganize the array.

1
  • so what's your problem, are u reorganize your array with foreach Commented Oct 25, 2015 at 18:40

1 Answer 1

1

This will group the results into an array, the original array is called $arr:

$result = array();
foreach( $arr['results'] as $v ) {
    $result[$v->client_id][] = $v;
}

To get a single clients ($client_id) all result display them like this

foreach( $result[$client_id] as $res ) {
    // Use the data
    echo 'Slip: ' .  $res->slip_id . "<br />\n"
        . 'Title: ' . $res->dwg_title  . "<br />\n";
}

To display everything, ordered by client, do somethoing like:

foreach( $result as $client_id => $temp ) {
    echo 'Client_id: ' . $client_id . "<br />\n";
    foreach( $temp as $res ) {
        // Use the data
        echo 'Slip: ' .  $res->slip_id . "<br />\n"
            . 'Title: ' . $res->dwg_title  . "<br />\n";
    }
}

Is this an answer to your question?

Edit2 $res is an object...

A way to display a single client and a way to display all clients!

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

6 Comments

OP asked for an associative array but this should provide the same result
@Hasse Thanks for your help. The first part of the code that you provided me with worked and gave me a rearranged array according to $client_id. I'm struggling with the next bit to pass the array to my view.
Thank you for pointing out the last part was in error. Of course it is an associative array, not an object. I have updated the script to show this!
@HasseBjörk So I've tried the updated code, but I keep running into problems to display it. I manage to get the post array of the $client_id from the previous view with which I create a $client array and pass to foreach($result[$client] as $res){...}. But I keep getting a Illegal offset type error. Unmodified Post array (client array) - Array ( [1] => Array ( [client_id] => 1 ) [3] => Array ( [client_id] => 3 ) )
What are you trying to do with the data? Display a single client or display all clients?
|

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.