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.
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();
// 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>";
}
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?
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).
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:\xampp\htdocs\dms\dms_admin\classes\function.inc.php on line 148
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;
}
}
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.