I am trying to fetch all employee leave data to an array. But first leave data of my employee is replacing by last leave data. I do not want that.
my data set:
id | emp_id | leave_from | leave_to
2894 886 2019-11-26 2019-11-27
2896 1970 2019-11-20 2019-11-24
2897 1016 2019-11-23 2019-11-23
2898 1870 2019-11-10 2019-11-10
2899 1895 2019-11-09 2019-11-11
2900 1895 2019-11-23 2019-11-23
2901 287 2019-11-27 2019-11-30
my sql query:
$leave_sql = "SELECT l.* FROM 0_employee e right join (SELECT * FROM 0_leave_ledger WHERE (leave_approved_from && leave_approved_to BETWEEN '".$fisrt_date."' AND '".$last_date."' )) l ON l.emp_id = e.emp_id where e.inactive=0";
$leave = get_tb($leave_sql);
$leave_date = array();
foreach ($leave as $leave_d) {
$leave_date[$leave_d["emp_id"]]['type'] = $leave_d["leave_type"];
$leave_date[$leave_d["emp_id"]]['lto'] = $leave_d["leave_approved_to"];
$leave_date[$leave_d["emp_id"]]['lfrom'] = $leave_d["leave_approved_from"];
}
let's look emp_id 1895.It has two rows in my data set. So I am expecting to insert those two rows in my array. But I am getting only last rows of data in my array.
my expecting output from $leave_date is:
[1895] => Array ( [type] => 4 [lto] => 2019-11-09 [lfrom] => 2019-11-11 )
[1895] => Array ( [type] => 4 [lto] => 2019-11-23 [lfrom] => 2019-11-23 )
but I am getting only
[1895] => Array ( [type] => 4 [lto] => 2019-11-23 [lfrom] => 2019-11-23 )
Your problem is that you have an employee who has taken multiple leaves, and the entries for each leave overwrite any previous ones in your array because they have the same array index. You need to change your data structure to allow for that. Try something like this:
$leave_date = array();
foreach ($leave as $leave_d) {
$leave_date[$leave_d["emp_id"]][] = array(
'type' => $leave_d["leave_type"],
'lto' => $leave_d["leave_approved_to"],
'lfrom' => $leave_d["leave_approved_from"]
);
}
For your sample data, this will give you something like
[1895] => Array (
[0] => Array ( [type] => 4 [lto] => 2019-11-09 [lfrom] => 2019-11-11 )
[1] => Array ( [type] => 4 [lto] => 2019-11-23 [lfrom] => 2019-11-23 )
)
The structure of your array $leave_date means it can only store one set of leave data per emp_id. When you iterate, it first fills up with the first leave record, and then the second iteration it gets replaced. If you were to put a print_r() at the end of your foreach you would see this happen for each iteration.
What you may want to do is try something like this:
foreach ($leave as $leave_d) {
$record = [
'type' => $leave_d["leave_type"],
'lto' => $leave_d["leave_approved_to"],
'lfrom' => $leave_d["leave_approved_from"],
]
if(array_key_exists($leave_d['emp_id'], $leave_date) {
$leave_date[$leave_d['emp_d']][] = $record;
} else {
$leave_date[$leave_d['emp_d'] = [$record];
}
This way, $leave_date[1234] will always be an array of leave records.
Related
I have tried many ways to do this but no luck. I'm trying to take value from First array and generate second array and put the second array in the first array.
The code that I have now, generate the first array, in this array I have column called prereq_id I need to take this value and match it with courses table to bring its information in the courses table course_id=prereq_id. Any help would be highly appreciated
The array output is like this right now
[course_id] => 2
[curriculum_id] => 1
[set_number] => 0
[course_code] => GCIS 516
[course_name] => Data-Centric Concepts and Methods
[credits] => 3
[semester_ava] => 2
[prereq_id] => 10
[set] => 3
php
$result2 = $mysqli->query("SELECT *
FROM curriculumcourses
INNER JOIN courses ON curriculumcourses.course_id = courses.course_id
LEFT JOIN prerequisites ON courses.course_id=prerequisites.course_id
WHERE curriculum_id='$ids' ");
?>
<?php
for ($x = 1; $x <= $mysqli->affected_rows; $x++) {
$rows[] = $result2->fetch_assoc();
}
echo "<pre>";
print_r($rows);
echo "</pre>";
The result I need look like this
[course_id] => 2
[curriculum_id] => 1
[set_number] => 0
[course_code] => GCIS 516
[course_name] => Data-Centric Concepts and Methods
[credits] => 3
[semester_ava] => 2
[prereq_id] => 10
[set] => 3
[course_code] => GCIS 508
[course_name] =>Database Management Systems
[credits] => 3
[semester_ava] => 1
My Problem
Trying to create similar arrays so I can compare them.
I am creating a distribution list for files, the adding of the distribution list work fine, I list user surname, user forename and user department, these are selected and posted, foreach user I retrieve the users.user_id and store this in a distribution table (under distro_lis)t like so.
distro_id (AI, PK) | distro_list | policy_id
---------------------------------------------
1 | 1 23 21 34 | 13
2 | 10 22 21 34 | 15
3 | 1 27 26 40 | 34
Now I working on the editing of the distribution list, so for a row, lets say row 1, I fetch the distro_list and parse it to get each user ID (from distro_list) using.
$policyID is received in the method argument
$db = $this->dbh->prepare('SELECT distro_list FROM distro WHERE policy_id = :policy_id');
$db->bindParam(':policy_id', $policyID);
$db->execute();
$row = $db->fetch(PDO::FETCH_ASSOC);
$ids = explode(' ',$row);
foreach ($ids as $user) {
$db = $this->dbh->prepare('SELECT user_forename, user_surname, user_dept FROM users WHERE user_id = :user_id ORDER BY user_surname ASC');
$db->bindParam(':user_id', $user);
$db->execute();
$row = $db->fetch(PDO::FETCH_ASSOC);
var_dump($row);
}
the var_dump($row) returns an array of arrays (objects.. not sure of the terminology here) which looks like so (print_r).
Array
(
[user_forename] => fname1
[user_surname] => sname1
[user_dept] => dept1
)
Array
(
[user_forename] => fname2
[user_surname] => sname2
[user_dept] => dept2
)
Array
(
[user_forename] => fname3
[user_surname] => sname3
[user_dept] => dept3
)
the array that I want to compare it to looks like so (print_r).
array
(
[0] => Array
(
[user_forename] => fname1
[user_surname] => sname1
[user_dept] => dept1
)
[1] => Array
(
[user_forename] => fname2
[user_surname] => sname2
[user_dept] => dept2
)
[2] => Array
(
[user_forename] => fname3
[user_surname] => sname3
[user_dept] => dept3
)
)
I understand why the first array looks like that, because im var_dump'ing' after each iteration. How can I get the first array (array of arrays or objects) into a single array so I can compare it to the second array?
Try this
$list_users = array()
foreach ($ids as $user) {
//...
$list_users[] = $row;
}
var_dump($list_users);
p1 p2 p3 p4 p5 p6 p7 p8
---------------------------------------------------------------------------------------
1414 1414 1417 1417 1422,1421 1422,1421 1422,1421 1422,1421
The above table has 8 columns in which the adjacent columns having same values. How to count the number of columns based on the column values in php or in mysql..
For Eg :
for 1414---values are p1,p2 and count is 2
for 1422,1421--- values are p5,p6,p7,p8 and count is 4.
Can any one help me in this..
Array
(
[0] => Array
(
[period] => p1
[subject] => 1434
[nop] => 1
)
<b>
**[1] => Array
(
[period] => p2
[subject] => 1440,1439
[nop] => 1
)
[2] => Array
(
[period] => p2,p3
[subject] => 1440,1439
[nop] => 2
)**
</b>
[3] => Array
(
[period] => p2,p3,p4
[subject] => 1440,1439
[nop] => 3
)
[4] => Array
(
[period] => p5
[subject] => 1442
[nop] => 1
)
[5] => Array
(
[period] => p6
[subject] => 1442
[nop] => 1
)
[6] => Array
(
[period] => p7
[subject] => 1442
[nop] => 1
)
)
I have got the above array.. how to remove the above highlighted values in the above array from it.
suppose your $row has reesult of table
$your_val = '1414'; // for eg 1414
$result_col = ""; // will contain you columns
$result_count = 0; // will contain your count
// $row has the each column value when you fetch from database eg $row['p1'] = 1414
foreach($row as $key=>$val)
{
if($val==$your_val)
{
$result_count += 1;
if($result_text=="")
{
$result_col = $key;
}
else
{
$result_col = $result_col.",".$key;
}
}
}
echo "values are ".$result_col." and count is ".$result_count; // output
You can do it with MySQL:
SELECT
IF(p1='1414',1,0) + IF(p2='1414',1,0)+ IF(p3='1414',1,0) + IF(p4='1414',1,0) + IF(p5='1414',1,0) + IF(p6='1414',1,0) + IF(p7='1414',1,0) + IF(p8='1414',1,0) AS cnt
FROM your_table;
If you are using PHP to access the databasem, you can replace the '1414' with a variable to count the occurences of the variable's value.
I have the following output of an array using PHP. I need to do two things... First, I need to sort the array so it prints by the most recent date. I can't use a simple sort, because the date is outputted in the format mm/dd/yyyy (and not a regular time stamp) ...
Then I need to count how many rows exist for each year.
So, in the example below, I would need to know that there are ...
2 entries from 2010
2 entries from 2011
1 entry from 2012
Stop counting when there are no more rows
Since the year is not separate from the rest of the date digits, this also complicates things...
Array
(
[0] => Array
(
[racer_date] => 11/15/2010
[racer_race] => Test Row 4
[racer_event] => 321
[racer_time] => 16
[racer_place] => 12
[racer_medal] => 1
)
[1] => Array
(
[racer_date] => 7/15/2010
[racer_race] => Test Row 3
[racer_event] => 123
[racer_time] => 14
[racer_place] => 6
[racer_medal] => 0
)
[2] => Array
(
[racer_date] => 7/28/2011
[racer_race] => Test Row
[racer_event] => 123
[racer_time] => 10
[racer_place] => 2
[racer_medal] => 2
)
[3] => Array
(
[racer_date] => 10/9/2011
[racer_race] => Test Row 2
[racer_event] => 321
[racer_time] => 12
[racer_place] => 3
[racer_medal] => 3
)
[4] => Array
(
[racer_date] => 10/3/2012
[racer_race] => World Indoor Championships (final)
[racer_event] => 400m
[racer_time] => 50.79
[racer_place] => 1
[racer_medal] => 1
)
)
function cmp($a, $b)
{
if (strtotime($a["racer_date"]) == strtotime($b["racer_date"])) {
return 0;
}
return (strtotime($a["racer_date"]) < strtotime($b["racer_date"])) ? -1 : 1;
}
usort($array, "cmp");
call your array $array, and above code will sort it..
And to count entities you'll need to run foreach and check date('Y',strtotime($a["racer_date"])) in that foreach which will give you year in 4 digit..
ok so i have this query
select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id from system_step ss
join system as s on s.system_id=ss.system_id
join category_description as cd on cd.category_id=ss.sub_category_id
join system_step_product as ssp on ss.system_step_id=ssp.system_step_id
join product as p on p.product_id=ssp.product_id where s.system_id = 41
order by ss.step_number, ssp.class_id;
which yields this result
7 1 Screens 808 115.0000 1
7 1 Screens 809 381.9000 2
7 1 Screens 810 441.9000 3
8 2 Printers 811 112.3200 1
8 2 Printers 812 201.0400 2
8 2 Printers 813 202.8700 3
9 3 Cash Drawers 814 135.7000 1
9 3 Cash Drawers 815 86.5400 2
9 3 Cash Drawers 816 135.7000 3
Is there a way to turn this into a php array of three elements like this
Array
(
[0] => Array
(
[name] => "Screens"
[standard_product] => Array ([id] => 808, [price] => '115.0000')
[business_product] => Array ([id] => 809, [price] => '381.9000')
[premium_product] => Array ([id] => 810, [price] => '441.9000')
)
[1] => Array
(
[name] => "Printers"
[standard_product] => Array ([id] => 811, [price] => '112.3200')
[business_product] => Array ([id] => 812, [price] => '201.0400')
[premium_product] => Array ([id] => 813, [price] => '202.8700')
)
[2] => Array
(
[name] => "Cash Drawers"
[standard_product] => Array ([id] => 814, [price] => '135.7000')
[business_product] => Array ([id] => 815, [price] => '86.5400')
[premium_product] => Array ([id] => 816, [price] => '135.7000')
)
)
$sql = "select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id, pd.name as product_name, pd.description from system_step ss join system as s on s.system_id=ss.system_id join category_description as cd on cd.category_id=ss.sub_category_id join system_step_product as ssp on ss.system_step_id=ssp.system_step_id join product as p on p.product_id=ssp.product_id join product_description as pd on pd.product_id=p.product_id where s.system_id = {$system['system_id']} order by ss.step_number, ssp.class_id;";
$result = mysql_query($sql);
$steps = array();
while($row_r = mysql_fetch_assoc($result)){
$steps[] = $row_r;
}
so steps is the the full array with 9 elements
As you can see the only thing of note is the class_id 1 is standard_product class_id 2 is business_product and class_id 3 is premium_product
You can fetch fill a multi-dimensional array while fetching the SQL result or split into several SQL commands and therefore build up your array (1st fetching the names, than the products with values of every name).
I do not know which you prefer but as you have a large join splitting the SQL might not be the worst in terms of readability of your code. Performance impact may vary.
Methinks this will do it
<?php
$finalArray = array();
$nameArray = array('Screens', 'Printers', 'Cash Drawers');
foreach ($nameArray as $name) {
while ($row = mysql_fetch_assoc($result)) {
if ($row['name'] == $name) {
if ($row['class_id'] == 1) {
$standard = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
}
else if ($row['class_id'] == 2) {
$business = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
}
else if ($row['class_id'] == 3) {
$premium = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
}
}
}
array_push($finalArray, array('name' => $name, 'standard_product' => $standard, 'business_product' => $business, 'premium_product' => $premium,));
}
?>
Hopefully I got all the column names right.
This is what I tested it off of and I get the correct output http://codepad.org/yjU3gxbB
The only thing you may what to change is creating the name array dynamically
while ($row = mysql_fetch_assoc($result)) {
array_push($nameArray, $row['name']);
}
$nameArray = array_unique($nameArray);
However, PHP doesn't like it when you do a mysql_fetch_assoc twice on the same query. You might want to do another query where you only select the cd.name