I have two table,i want fetch two table values,this code should working fine but count=2 means loop running 2 times,return the output of 2 times,like count=3 means loop running in 3 times return output 3 times,what i did mistake....
OUTPUT
{
"status":"success",
"count":2,
"data":[
{
"id":"1",
"t_id":"STV1",
"t_title":"Horoscope Uploading",
"t_project":"1",
"t_sub_project":"Sub-project",
"t_desc":"cfdgdgdcf",
"t_priority":"Urgent",
"t_assign_to":"AE098",
"t_assign_on":"2016-04-13 12:03:49",
"t_started_on":"2016-04-14 05:30 PM",
"t_due_on":"2016-04-22 05:30 PM",
"t_complete_percentage":"100",
"t_est_hours":"35",
"t_worked":"10 Hours",
"t_comment":"dfhfghfgfsfhfgh",
"t_created_on":"2016-04-13 12:03:49",
"t_edited_on":"2016-04-14 07:01:06",
"t_status":"3",
"t_delete_on":"0"
}
]
}{
"status":"success",
"count":2,
"data":[
{
"id":"1",
"t_id":"STV1",
"t_title":"Horoscope Uploading",
"t_project":"1",
"t_sub_project":"Sub-project",
"t_desc":"cfdgdgdcf",
"t_priority":"Urgent",
"t_assign_to":"AE098",
"t_assign_on":"2016-04-13 12:03:49",
"t_started_on":"2016-04-14 05:30 PM",
"t_due_on":"2016-04-22 05:30 PM",
"t_complete_percentage":"100",
"t_est_hours":"35",
"t_worked":"10 Hours",
"t_comment":"dfhfghfgfsfhfgh",
"t_created_on":"2016-04-13 12:03:49",
"t_edited_on":"2016-04-14 07:01:06",
"t_status":"3",
"t_delete_on":"0"
}
]
}
Same ans but i will come two times,how to fix this problem
<?php
session_start();
include('dbconfig.php');
if(empty($_SESSION['email'])){
header('Location:login.php');
}
$dapartment = $_POST['department'];
$sql = mysql_query("SELECT * FROM task_employee WHERE emp_designation='$dapartment'");
$count=mysql_num_rows($sql);
$return = array();
while($r=mysql_fetch_assoc($sql)){
$emp_id=$r['emp_id'];
if($count > 0){
$mysql = mysql_query("SELECT * FROM task WHERE t_assign_to='$emp_id'");
while($row= mysql_fetch_assoc($mysql)){
$data[] = $row;
}
$return=array('status'=>'success','count'=>$count,'data'=>$data);
echo json_encode($return);
}else{
$return=array('status'=>'not found','count'=>$count,'data'=>$data);
echo json_encode($return);
}
}
?>
</div>
1) You've to collect emp_ids
using first query
2) Then get all tasks by doing WHERE IN ()
3) Collect all tasks to array
4) You may output json_encode
once, otherwise You json is invalid
<?php
session_start();
include('dbconfig.php');
if(empty($_SESSION['email'])){
header('Location:login.php');
}
header('Content-Type: text/javascript; charset=utf8');
$dapartment = $_POST['department'];
$q = "SELECT * FROM task_employee WHERE emp_designation='".mysql_real_escape_string($dapartment)."'";
$q = mysql_query($q);
$taskEmployees = array();
while($taskEmployee = mysql_fetch_assoc($q)) {
$taskEmployees[$taskEmployee['emp_id']] = $taskEmployee;
}
$empIds = array_keys($taskEmployees);
$tasks = array();
if(!empty($empIds)) {
// this query tells mysql to return tasks of assigned users
$q = "SELECT * FROM task WHERE t_assign_to IN (".implode(',', $empIds).")";
$q = mysql_query($q) or die(mysql_error());
// generating array of tasks associated by emp_id (t_assign_to field)
while($task = mysql_fetch_assoc($q)) {
$tasks[] = array(
'task' => $task,
'task_employee' => $taskEmployees[$task['t_assign_to']]
);
}
}
echo json_encode(array(
'status' => (empty($empIds))? 'not found' : 'success',
'count' => sizeof($tasks),
'data' => $tasks
));
In JS You'll use them like (it's for not to merge field names):
for(var r in records) {
var record = records[r];
console.log(record.task, record.taks_employee);
}
As @Wobbles mentioned. You can use joins.
session_start();
include('dbconfig.php');
if(empty($_SESSION['email'])){
header('Location:login.php');
}
$dapartment = $_POST['department'];
$q = mysql_query("SELECT * FROM task_employee te, task t WHERE te.emp_designation='$dapartment' AND te.emp_id = t.t_assign_to");
$data = array();
while($r = mysql_fetch_assoc($q)){
$data[] = $r;
}
$return = array(
'status' => ((empty($data))?'not found':'success'),
'count' => sizeof($data),
'data' => $data
);
header('Content-Type: text/javascript; charset=utf8');
echo json_encode($return);
For more info, please have a look on mysql-using-joins
Just use an inner join so you dont have to iterate employee id's
SELECT *
FROM task_employee
INNER JOIN task
ON task_employee.emp_id=task.t_assign_to
ORDER BY task_employee.emp_id;
You do not have to use a join. You can select from two tables in one query.
<?php
session_start();
include('dbconfig.php');
if(empty($_SESSION['email'])){
header('Location:login.php');
}
$dapartment = mysql_real_escape_string($_POST['department']);
$sql = mysql_query("SELECT * FROM task_employee, task WHERE task_employee.emp_designation='$dapartment' AND task_employee.t_assign_to = task.emp_id");
if(false == $sql){
die('Invalid query: ' . mysql_error());
}
$count = mysql_num_rows($sql);
$data = [];
while($row=mysql_fetch_assoc($sql)){
$data[] = $row;
}
if (count($data) <= 0){
$return = array('status'=>'not found','count'=>$count,'data'=>$data);
}
else{
$return = array('status'=>'success','count'=>$count,'data'=>$data);
}
echo json_encode($return);
?>