Welcome Guest, Not a member yet? Register   Sign In
Returning an Array of rows within a Result Array.
#1

[eluser]defectivereject[/eluser]
Tried all night and day, and with Google.
So turning to you guys for sanity

each time the user has a lesson/course it is added to the DB via a course booking form, which puts data in several tables.

1st it adds the course to a DB table, with all the details and who's attending e.t.c.
2nd a separate query then runs to email the joining instructions to all those members attending(identified by course_id against a name).

3rd (Where i am stuck)
What I'm looking for now is;
the initial query has already added the following to a table called 'officecal'
containing
ID(PK),
course_id,
date_t
and a user column

so table would be
id 1, course_id 1, user john, date 2010/01/01
id 2, course_id 1, user dave, date 2010/01/01
id 3, course_id 2, user john, date 2010/01/03
id 4, course_id 2, user dave, date 2010/01/03
id 5, course_id 2, user sue, date 2010/01/03
id 6, course_id 3, user sam, date 2010/01/04

what i'm trying to do is get the query to return an array of users per date (which i identify by course_id)
and throw that into CI calendar

My query is a simple one i know, but i can't get it to give me what i want, especially with what i have done with others up to this point!
Code:
function get_staff_training_dates($year, $month, $id) {
$this->db->select('date_t, user');
$this->db->where('course_id', $id);
$Q2 = $this->db->get('officecal');
if ($Q2->num_rows() > 0){
foreach ($Q2->result_array() as $row){
$data[(int)substr($row['date_t'], 8, 2)] = $row['user'];
}
}
$Q2->free_result();    
return $data;
    }

What I'm trying to get this query to do is this(The 1 and the 3 below are stripped out of the date by the query above to identify purely the day of the month)
Code:
Array (
[1] => array(john, dave)
[3] => array(john, dave, sue)
[4] => sam
)
So i can throw it in the CI Calendar (Yeah i fixed the library/calendar.php i think(!) to eventually receive an array per cell.

But all i can get back from the result is
Code:
Array (
[1] => dave
[3] => sue
[4] => sam

It's seemingly returning only the last record per date in the db rather than all, per date.
I'm obviously screwing up somewhere, any help would be great!

Cheers in advance!
#2

[eluser]Watermark Studios[/eluser]
Right now you're just replacing your key (the day) value with the most recent user value. You need to just add a nested array.
I'm just taking a quick stab at this, but I would think you would want to do something like this:

Code:
$data[(int)substr($row['date_t'], 8, 2)][] = $row['user'];

This would produce:

Code:
Array(
[1] => Array(
                        [0] => 'john',
                        [1] => 'dave'
             ),
[3] => Array(
                        [0] => 'john',
                        [1] => 'dave',
                        [2] => 'sue'
             ),
[4] => Array(
                        [0] => 'sam'
             )
)
#3

[eluser]defectivereject[/eluser]
Cheers
Ignoring the funky data below as i did it to test the array faster i have now gotten the array i need
Code:
[26] => Array ( [0] => asd [1] => asd [2] =>
Tinker
 [3] => sadasd [4] => dsaff [5] => dfgdfgdfg )
Problem is the fix i thought i had for the calendar, doesn't work and inserts the above data on the said date of each month rather than the said date of that month
#4

[eluser]defectivereject[/eluser]
Tis fine.
I used Jagar's My_Calendar Library, and then changed the key part of my query to the following
Code:
$data[(int)substr($row['date_t'], 0, 4)][(int)substr($row['date_t'], 5, 2)][(int)substr($row['date_t'], 8, 2)][] =  $row['data_t'];

Seems to have done the trick, as its created an array as follows
Code:
Array
(
[2010] => Array ( //Year
    [10] => Array ( //Month
        [6] => Array ( [0] => xzc )  //Day & User
        [4] => Array ( [0] => xzczxc [1] => all [2] => all )  //Day & Users
        [26] => Array ( [0] => asd [1] => asd [2] =>
Tinker
 [3] => sadasd [4] => dsaff )  [5] => Array ( [0] => xc ) //Day & Users
    )  

    [9] => Array ( //Month
        [13] => Array ( [0] => fgdfg )  //e.t.c.
        [26] => Array ( [0] => News Day )  
        [1] => Array ( [0] => hfghfg )  
        [30] => Array ( [0] => fdghdfghdfgh )  )  
    [11] => Array (
        [26] => Array ( [0] => dfgdfgdfg )  )  

    [8] => Array (
        [3] => Array ( [0] => gfhfghfgh )  [8] => Array ( [0] => fgh )  )  )  

[2009] => Array (  //Year
    [10] => Array ( //Month
        [2] => Array ( [0] => dsf )  )  //e.t.c.
        [12] => Array ( [3] => Array ( [0] => dfdgdf )  )  //e.t.c.
        [9] => Array ( [9] => Array ( [0] => sdfgsdfg )  )  //e.t.c.
        [8] => Array ( [12] => Array ( [0] => sdfg )  )  )  //e.t.c.
        )
        1
        
        ?>

Pic




Theme © iAndrew 2016 - Forum software by © MyBB