View Single Post
  #1 (permalink)  
Old 04-20-09, 10:38 PM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Multi-Dimensional Array Help

I need to do create a multi-dimensional array with name and date. Both are executed in different query then joined together. The end product should be something like this.

PHP Code:

Array

(
    [
Matthew] => Array
        (
            [
0] => 2009-04-05
            
[1] => 2009-04-05
            
[2] => 2009-04-05
            
[3] => 2009-04-05
            
[4] => 2009-04-05
        
)

)

Array
(
    [
Ong Kian Ming] => Array
        (
            [
0] => 2009-04-05
            
[1] => 2009-04-05
            
[2] => 2009-04-05
            
[3] => 2009-04-05
            
[4] => 2009-04-05
        
)


Now I'm almost there however, there's just something that I couldn't get to work.

This is the part where I process the query.

PHP Code:

$DB NewADOConnection('mysql');

    
$DB->Connect(@$server, @$user, @$pwd, @$db);
    
    
$query "SELECT personnel_name FROM dms_personnel";

    
$rs $DB->Execute($query);
    
$arr $rs->GetRows();
    
$count count($arr) - 1;
    
$name '';
    for (
$i=0$i<=$count$i++) {
        
$name .= $arr[$i]['personnel_name'] . ", ";
    }
    
$sub_name explode(", "substr($name0, -2));
    
$list "";
    foreach (
$sub_name as $names) {
    
$list availableDate($names04);
    } 
And this is the function.

PHP Code:

function getDateforMonth($date$month) {

    global 
$newDate;
    
    foreach (
$date as $dateValue) {
        if (
substr($dateValue52) == $month) {
            
// Store the correct month a new array
            
$newDate .= $dateValue ", ";
        }
    }
    return 
$newDate;
    
}

// Get a list of available date of each personnel and sort them accordingly
// $personnel :: Official name
// $month     :: Month of Duty
function availableDate($personnel$month) {
    
$DB NewADOConnection('mysql');
    
$DB->Connect(@$server, @$user, @$pwd, @$db);
    
    
$query "SELECT * FROM dms_personnel WHERE personnel_name =  '$personnel'";
    
$rs $DB->Execute($query);
    
$arr $rs->FetchRow();
    
    
$id $arr['id'];
    
$coy $arr['personnel_company'];
    
    
// Base on the ID, check with the individual schedule
    
$queryID "SELECT individual_schedule_date FROM dms_individual_schedule WHERE personnel_id = $id";
    
$rsID $DB->Execute($queryID);
    
$arrID $rsID->FetchRow();
    
    
// Remove the last 2 character (, ) from the string using substr
    // Use explode to convert string into array
    
$date explode(', 'substr($arrID['individual_schedule_date'], 0, -2));
    
// Get the available date for the date instead of the whole schedule
    
$new "";
    
$new getDateforMonth($date$month);
        echo 
"<pre>";
    
print_r(explode(", "substr($new,0,-2)));
    echo 
"</pre>";

But the result that I got was..

PHP Code:

Array

(
    [
0] => 2009-04-20
    
[1] => 2009-04-21
    
[2] => 2009-04-22
    
[3] => 2009-04-24
    
[4] => 2009-04-25
)

Array
(
    [
0] => 2009-04-20
    
[1] => 2009-04-21
    
[2] => 2009-04-22
    
[3] => 2009-04-24
    
[4] => 2009-04-25
    
[5] => 2009-04-04
    
[6] => 2009-04-05
    
[7] => 2009-04-06
)

Array
(
    [
0] => 2009-04-20
    
[1] => 2009-04-21
    
[2] => 2009-04-22
    
[3] => 2009-04-24
    
[4] => 2009-04-25
    
[5] => 2009-04-04
    
[6] => 2009-04-05
    
[7] => 2009-04-06
    
[8] => 2009-04-01
    
[9] => 2009-04-02
    
[10] => 2009-04-03
)

Array
(
    [
0] => 2009-04-20
    
[1] => 2009-04-21
    
[2] => 2009-04-22
    
[3] => 2009-04-24
    
[4] => 2009-04-25
    
[5] => 2009-04-04
    
[6] => 2009-04-05
    
[7] => 2009-04-06
    
[8] => 2009-04-01
    
[9] => 2009-04-02
    
[10] => 2009-04-03
    
[11] => 2009-04-09
    
[12] => 2009-04-07
    
[13] => 2009-04-08
)

Array
(
    [
0] => 2009-04-20
    
[1] => 2009-04-21
    
[2] => 2009-04-22
    
[3] => 2009-04-24
    
[4] => 2009-04-25
    
[5] => 2009-04-04
    
[6] => 2009-04-05
    
[7] => 2009-04-06
    
[8] => 2009-04-01
    
[9] => 2009-04-02
    
[10] => 2009-04-03
    
[11] => 2009-04-09
    
[12] => 2009-04-07
    
[13] => 2009-04-08
    
[14] => 2009-04-10
    
[15] => 2009-04-11
    
[16] => 2009-04-12
)

Array
(
    [
0] => 2009-04-20
    
[1] => 2009-04-21
    
[2] => 2009-04-22
    
[3] => 2009-04-24
    
[4] => 2009-04-25
    
[5] => 2009-04-04
    
[6] => 2009-04-05
    
[7] => 2009-04-06
    
[8] => 2009-04-01
    
[9] => 2009-04-02
    
[10] => 2009-04-03
    
[11] => 2009-04-09
    
[12] => 2009-04-07
    
[13] => 2009-04-08
    
[14] => 2009-04-10
    
[15] => 2009-04-11
    
[16] => 2009-04-12
)

Array
(
    [
0] => 2009-04-20
    
[1] => 2009-04-21
    
[2] => 2009-04-22
    
[3] => 2009-04-24
    
[4] => 2009-04-25
    
[5] => 2009-04-04
    
[6] => 2009-04-05
    
[7] => 2009-04-06
    
[8] => 2009-04-01
    
[9] => 2009-04-02
    
[10] => 2009-04-03
    
[11] => 2009-04-09
    
[12] => 2009-04-07
    
[13] => 2009-04-08
    
[14] => 2009-04-10
    
[15] => 2009-04-11
    
[16] => 2009-04-12

By right, the date for the respective ID should be.

PHP Code:

Array

(
    [
0] => 2009-04-20
    
[1] => 2009-04-21
    
[2] => 2009-04-22
    
[3] => 2009-04-24
    
[4] => 2009-04-25

)
Array
(
    [
0] => 2009-04-01
    
[1] => 2009-04-02
    
[2] => 2009-04-03

)
Array
(
    [
0] => 2009-04-04
    
[1] => 2009-04-05
    
[2] => 2009-04-06

)
Array
(
    [
0] => 2009-04-07
    
[1] => 2009-04-08
    
[2] => 2009-04-09


I'm yet to do into the multi-dimension array and I have problem like the data keep adding on the previous ones. I know somewhere I have to clear the list first before going into the loop. But I just can't get it. Can someone help me with it?
Reply With Quote