2014-03-05 90 views
0

这里有以下代码:笨MySQL查询输出数据显示

Controller.php这样

function getchart() { 
$prac = $this->input->post('prac_name'); 
$datee = $this->input->post('datee'); 
$this->load->model('appoint'); 
$results['appoint'] = $this->appoint->getappoint($prac , $datee); 
$this->load->view('ajax/getappchart' , $results); 
} 

model.php

function getappoint($prac , $datee) { 
     $this->db->select('rdv.id as rdvid, startTime, endTime, day, firstname, lastname'); 
     $this->db->from('rdv'); 
     $this->db->join('contact', 'contact.id = rdv.contact_id'); 
     $this->db->where('people_id',$practicien); 
     $this->db->where('DATE(day)', $datee); 
     $this->db->order_by('TIME(startTime)', 'ASC'); 
     $query = $this->db->get(); 
     //print_r($this->db->last_query()); 
     return $query; 
    } 

view.php

if ($appoint->num_rows() > 0) { 
     foreach($appoint->result() as $sub_row) 
     { 
// display output. 
} 
    } else { 
     echo 'No Appointments on Above Date.'; 
    } 
    ?> 

我需要的是t这里是同一天和同一天的约会(大部分是2次)。 如果有2个以上,我需要为约会设置两种不同的课程风格。 我怎么能做到这一点?

谢谢。

回答

1

最终的答案:与@ minhaz艾哈迈德

if ($appoint->num_rows() > 0) { 
    $appoint_counter = array(); 
    foreach ($appoint->result() as $sub_row) { 
     //i am assuming your startTime is H:M:S, and day Y-M-D format 
     $key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']); 
     if (!isset($appoint_counter[$key])) { 
      $appoint_counter[$key] = 0; 
     } 
     $appoint_counter[$key] ++; 
     $style_class = 'YOUR_1ST_CLASS'; 
     if ($appoint_counter[$key] > 2) { 
      $style_class = 'YOUR_2ND_CLASS'; 
     } 

     //REST VIEW CODE 
    } 
} 
else { 
    echo 'No Appointments on Above Date.'; 
} 
+1

我得到了你想要的东西,我以为你想要不同的课程,如果超过两个,并显示在相同的风格 – Minhaz

+0

感谢您的代码。 – Sparkz

0

你可以这样做

if ($appoint->num_rows() > 0) { 
     $appoint_counter = array(); 
     foreach ($appoint->result() as $sub_row) { 
      //i am assuming your startTime is H:M:S, and day Y-M-D format 
      $key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']); 
      if (!isset($appoint_counter[$key])) { 
       $appoint_counter[$key] = 0; 
      } 
      $appoint_counter[$key] ++; 
     } 
     foreach ($appoint->result() as $sub_row) { 
      //i am assuming your startTime is H:M:S, and day Y-M-D format 
      $key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']); 

      $style_class = 'YOUR_1ST_CLASS'; 
      if ($appoint_counter[$key] > 2) { 
       $style_class = 'YOUR_2ND_CLASS'; 
      } 

      //YOUR REST VIEW 
     } 
    } else { 
     echo 'No Appointments on Above Date.'; 
    } 
+0

的帮助下完成它这让我1个或2两者的约会? – Sparkz

0

你的类样式是什么意思?无论如何,下面的代码假定你会把结果放在一个数据网格中。

$prev_date = ""; 
$prev_time = ""; 

$results = $query->result(); 
foreach($results as $appointment) { 

    if ($prev_date != $appointment->day) // Assuming "day" is tables date 
     # Display output here 

    if ($prev_time != $appointment->startTime) // Logically, display should sort appointments by their starting time 
     # Display output here 

    # Display appointment rows here 

    $prev_date = $appointment->day; 
    $prev_time = $appointment->startTime; 
} 

if (empty($results)) { 
    # Display "no appointments found" output here 
}