Multi-Dimensional Array Help

04-20-09, 09:38 PM
|
|
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($name, 0, -2));
$list = "";
foreach ($sub_name as $names) {
$list = availableDate($names, 04);
}
And this is the function.
PHP Code:
function getDateforMonth($date, $month) {
global $newDate;
foreach ($date as $dateValue) {
if (substr($dateValue, 5, 2) == $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?
|

04-20-09, 10:21 PM
|
|
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).
|

04-20-09, 10:36 PM
|
|
Coding Addict
|
|
Join Date: Jun 2005
Location: Singapore
Posts: 307
Thanks: 0
Thanked 1 Time in 1 Post
|
|
So how should I approach this?
|

04-20-09, 10:53 PM
|
|
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:
Notice: Undefined variable: newDate 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 )
Notice: Undefined variable: newDate in C:xampphtdocsdmsdms_adminclassesfunction.inc.php on line 148
Array ( [0] => 2009-04-04 [1] => 2009-04-05 [2] => 2009-04-06 )
Notice: Undefined variable: newDate in C:xampphtdocsdmsdms_adminclassesfunction.inc.php on line 148
Array ( [0] => 2009-04-01 [1] => 2009-04-02 [2] => 2009-04-03 )
Notice: Undefined variable: newDate in C:xampphtdocsdmsdms_adminclassesfunction.inc.php on line 148
Array ( [0] => 2009-04-09 [1] => 2009-04-07 [2] => 2009-04-08 )
Notice: Undefined variable: newDate in C:xampphtdocsdmsdms_adminclassesfunction.inc.php on line 148
Array ( [0] => 2009-04-10 [1] => 2009-04-11 [2] => 2009-04-12 )
|

04-20-09, 11:00 PM
|
|
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($dateValue, 5, 2) == $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($personnelDates, 1).'</pre>';
|

04-20-09, 11:41 PM
|
|
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($personnelDates, 1)
|

04-21-09, 12:48 AM
|
|
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
|

04-21-09, 12:52 AM
|
|
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.
|

04-21-09, 03:17 AM
|
|
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.
|

04-21-09, 07:23 AM
|
|
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?
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|