OK, I could use some help please.
I have a mysql table that I use for scheduling employees with the structure:
schedule_id employee_id request_date approved slot_id flags phase_added
This structure is great for almost all of my needs, except creating a easily readable weekly schedule. The data gets displayed on the screen very well, but printing the display is ‘messy’. I need to create a file than can be opened in excel and easily printed.
I have code to create the schedule items for a particular day:
[php]
//Get the data for putting into the new table
$query = “SELECT employee_id, slot_id, flags FROM schedule WHERE request_date = ‘$day_two’ ORDER BY FIELD (slot_id, 1, 2, 3, 4, 5, 6, 7, 8, 57 ,58 ,10 ,9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 59, 61, 41, 42, 43, 44, 47, 49, 45, 46)”;
$result = mysql_query($query);
$result = mysql_query($query) or die(mysql_error());
for($i = 0; $array[$i] = mysql_fetch_assoc($result); $i++) ;
array_pop($array);
//Let’s find out what $i equaled
$array_rows = array_values($array);
$array_rows_number = count($array_rows);
echo $array_rows_number."
";
[/php]
Which give me an assoc array that looks like this (but with many more entries):
[0] => Array
(
[employee_id] => Carla Carter
[slot_id] => 1
[flags] =>
)
Here’s the problem, slot_id are not unique. For example [slot_id] => 45 denotes someone on vacation. There can 0 to 25 employees on vacation for given day. Which mean that as I loop through my selection: WHERE request_date = 'each day in a week’; my value for $array_rows_number changes.
I think what I want to do is place space holders in each array with no values for [employee_id], or [flags] based on the max number of [slot_id] entries I find. That way I can just insert the array into a table and down load a .csv file. And I know that only 3 [slot_id]s will have multiple (10, 45, and 46).
Question1: Am I correct about my plan of action? If so, how do I proceed?
Additionally in my $query I use ORDER BY FIELD. But I already have this order stored in a separate table with the structure:
slot_id slot_name
I put these values into an array so I can change number to names in another part of my code:
[php]
$shift_order = array();
// $shift_order2 = array();
$query = “SELECT slot_id, slot_name FROM shift_order”;
// $query = “SELECT slot_id FROM shift_order”;
$result = mysql_query($query);
while(list($k, $v) = mysql_fetch_array($result)) $shift_order[$k] = $v;
$num_results = mysql_num_rows($result);
// for($i=0; $i<$num_results; $i++)
// while($shift_order2 = mysql_fetch_row($result))
echo ‘
’;’;
print_r($shift_order);
echo ‘
// echo $shift_order2."
";
// echo ‘
’;’;
// print_r($shift_order2);
// echo ‘
[/php]
I had intend to use an array (note the commented out lines) to sort the ORDER BY FIELD, instead of typing out everything manually; but I had a bunch of trouble. I was trying variations on:
$query = "SELECT employee_id, slot_id, flags FROM schedule WHERE request_date = ‘$day_two’ ORDER BY FIELD (slot_id, $shift_order2)”
Question 2: Can somebody suggest a reason why I was unable to sort by the array ?