dongzhi4239 2016-04-14 13:53
浏览 31

在获取两个表值时,whilelopp不正确

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>
  • 写回答

4条回答 默认 最新

  • donglunzai4288 2016-04-14 14:06
    关注

    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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式