Current location: Hot Scripts Forums » Programming Languages » PHP » Multi-Dimensional Array Help

Multi-Dimensional Array Help

 
Prev Previous Post   Next Post Next
  #1  
Old 04-20-09, 10:38 PM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 307
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
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PHP Storing multi Dimensional array data in MySQL scott2500uk PHP 3 04-11-08 05:17 AM
[SOLVED] Sorting multi array on key patter PHP 8 03-27-08 04:23 PM
Two dimensional array Volkun C/C++ 2 08-29-07 07:35 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 09:59 AM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2 (Unregistered)