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

Multi-Dimensional Array Help

Reply
  #1 (permalink)  
Old 04-20-09, 09: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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-20-09, 10:21 PM
forgueam forgueam is offline
iNET Code Serf
 
Join Date: Aug 2007
Location: West Chester, OH, USA
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
I think one of your problems is probably the "global $newDate;" in the getDateforMonth() method. Globals are nasty things and should be avoided as much as possible (IMHO).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 04-20-09, 10:36 PM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 307
Thanks: 0
Thanked 1 Time in 1 Post
So how should I approach this?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 04-20-09, 10:53 PM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 307
Thanks: 0
Thanked 1 Time in 1 Post
Hey, I went to do a research and found out that maybe I can use & to get reference. But I'm not sure how it applies to my case. Also, the moment I removed the global. The result is what I wanted but with a small error.

PHP Code:
NoticeUndefined variablenewDate in C:xampphtdocsdmsdms_adminclassesfunction.inc.php on line 148

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


NoticeUndefined variablenewDate in C:xampphtdocsdmsdms_adminclassesfunction.inc.php on line 148

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


NoticeUndefined variablenewDate in C:xampphtdocsdmsdms_adminclassesfunction.inc.php on line 148

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


NoticeUndefined variablenewDate in C:xampphtdocsdmsdms_adminclassesfunction.inc.php on line 148

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


NoticeUndefined variablenewDate in C:xampphtdocsdmsdms_adminclassesfunction.inc.php on line 148

Array
(
    [
0] => 2009-04-10
    
[1] => 2009-04-11
    
[2] => 2009-04-12

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 04-20-09, 11:00 PM
forgueam forgueam is offline
iNET Code Serf
 
Join Date: Aug 2007
Location: West Chester, OH, USA
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
This might work, though I haven't actually tested it....

PHP Code:

    
function availableDates(&$DB$personnelId$month) {

        
// Base on the ID, check with the individual schedule
        
$queryID "SELECT individual_schedule_date FROM dms_individual_schedule WHERE personnel_id = $personnelId";
        
$rsID $DB->Execute($queryID);
        
$arrID $rsID->FetchRow();

        
// Remove the last 2 character (, ) from the string using substr
        // Use explode to convert string into array
        
$dates explode(', 'substr($arrID['individual_schedule_date'], 0, -2));

        
// Get the available dates for the specified month instead of the whole schedule
        
$availableDates = array()
        foreach (
$dates as $dateValue) {
            if (
substr($dateValue52) == $month) {
                
$availableDates[] = $dateValue;
            }
        }

        return 
$availableDates;
    }


    
$DB NewADOConnection('mysql');
    
$DB->Connect(@$server, @$user, @$pwd, @$db);

    
$query "SELECT id, personnel_name FROM dms_personnel";

    
$rs $DB->Execute($query);
    
$arr $rs->GetRows();
    
$count count($arr) - 1;

    
$personnelDates = array();
    for (
$i 0$i <= $count$i++) {
        
$personnelDates[$arr[$i]['personnel_name']] = availableDates($DB$arr[$i]['id'], '04');
    } 

    echo 
'<pre>'.print_r($personnelDates1).'</pre>'
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 04-20-09, 11:41 PM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 307
Thanks: 0
Thanked 1 Time in 1 Post
Yeah. It works fine. However, what's the 1 for in this line.

PHP Code:
print_r($personnelDates1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 04-21-09, 12:48 AM
forgueam forgueam is offline
iNET Code Serf
 
Join Date: Aug 2007
Location: West Chester, OH, USA
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
That just tells print_r() to return its output as a string, instead of simply outputting it. http://php.net/print_r
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 04-21-09, 12:52 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 307
Thanks: 0
Thanked 1 Time in 1 Post
Alright. Thanks for the help!

I might meet into some more problem with the array in the near future, thus, if I do I will post it here since it falls under the same topic.

Last edited by Nikas; 04-21-09 at 12:54 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 04-21-09, 03:17 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 307
Thanks: 0
Thanked 1 Time in 1 Post
I like need to get the opposite dates. Currently, those date are unavailable dates. I need to get the available dates.

This is what I wrote, it works but there's a part I don't know how to get the value of the user.

PHP Code:
// Function to give a list of personnel who are able to serve the duty for the particular month
// Some might not be able to make it due to medical reason, off, leave, roving and/or other reason.
function availableList(&$DB$unavailableDate$month) {
    
// Determine the number of days in the particular month and year
    
$year getdate();
    
$num cal_days_in_month(CAL_GREGORIAN$month$year['year']);

    
$temp_availableDate = array();
    for (
$i=1$i<=$num$i++) {
        if (!
in_array($year['year'].'-'.$month.'-'.$i$unavailableDate)) {
            
$temp_availableDate[] = $year['year'].'-'.$month.'-'.$i;
        }
    }
    return 
$temp_availableDate;
}

// This I type it out myself. If I wanted to use the name from the previous array? // How do I call it? It's $personnelDates[$arr[$i]['personnel_name']] but that's 
// the date.
availableList($DB$personnelDates['Tan Ah Lian'], '04'); 
PS: After some thought, would it be better not to do this function? When I'm doing the script, I will just add in the codes for it.

Last edited by Nikas; 04-21-09 at 03:34 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 04-21-09, 07:23 AM
forgueam forgueam is offline
iNET Code Serf
 
Join Date: Aug 2007
Location: West Chester, OH, USA
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
You could do something like this to get the array indicies (personnel names):

PHP Code:

foreach ($personnelDates as $name => dates) {
    
$availableList availableList($DB$name'04');  

Make sense?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

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
Trackbacks are On
Pingbacks are On
Refbacks are On

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 04:17 AM
[SOLVED] Sorting multi array on key patter PHP 8 03-27-08 03:23 PM
Two dimensional array Volkun C/C++ 2 08-29-07 06:35 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 11:44 PM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.