2014-10-06 187 views
0

我正在嘗試獲取最小的開始值和最大值結束下面的對象。該對象是一個sql腳本的結果。選擇一個對象的最小值和最大值

object(CI_DB_mysql_result)[18] 
    public 'conn_id' => resource(54, mysql link persistent) 
    public 'result_id' => resource(61, mysql result) 
    public 'result_array' => 
    array (size=0) 
     empty 
    public 'result_object' => 
    array (size=3) 
     0 => 
     object(stdClass)[16] 
      public 'id' => string '601' (length=3) 
      public 'scheduled' => string '0' (length=1) 
      public 'start' => string '2014-10-17 06:00:00' (length=19) 
      public 'end' => string '2014-10-17 11:00:00' (length=19) 
     1 => 
     object(stdClass)[19] 
      public 'id' => string '602' (length=3) 
      public 'scheduled' => string '0' (length=1) 
      public 'start' => string '2014-10-17 18:00:00' (length=19) 
      public 'end' => string '2014-10-17 19:30:00' (length=19) 
     2 => 
     object(stdClass)[20] 
      public 'id' => string '603' (length=3) 
      public 'scheduled' => string '1' (length=1) 
      public 'start' => string '2014-10-17 11:00:00' (length=19) 
      public 'end' => string '2014-10-17 18:00:00' (length=19) 
    public 'custom_result_object' => 
    array (size=0) 
     empty 
    public 'current_row' => int 0 
    public 'num_rows' => int 3 
    public 'row_data' => null 

在這種情況下啓動的最小值=「2014年10月17日6點00分00秒」和端部的最大值=「2014年10月17日19:30:00」。所以我正在尋找的結果如下所示:

$smallest_start_value = '2014-10-17 06:00:00'; 
$biggest_end_value = '2014-10-17 19:30:00'; 

應該如何計算這個結果?

功能:

function unschedule_resource() { 
    $event_id = 4;//$_POST['event_id']; 
    $event_start = '2014-10-17 11:00:00';//$_POST['event_start']; 
    $event_end = '2014-10-17 18:00:00';//$_POST['event_end']; 
    // Get resource id 
    $this->db->select(' 
     event.resource_id' 
    ); 
    $this->db->from('promo_manager.event'); 
    $this->db->where('event.id =', $event_id); 
    $data = $this->db->get(); 
    foreach ($data->result() as $row) { 
     echo $row->resource_id . "<br>"; 
     $resource_id = $row->resource_id; 
    } 
    // Get resource events to unschedule 
    $this->db->select(' 
     resource_calendar.id, 
     resource_calendar.scheduled, 
     resource_calendar.start, 
     resource_calendar.end' 
    ); 
    $this->db->from('promo_manager.resource_calendar'); 
    $this->db->where('resource_calendar.resource_id =', $resource_id); 
    $this->db->where('resource_calendar.start =', $event_start); 
    $this->db->where('resource_calendar.end =', $event_end); 
    $this->db->or_where('resource_calendar.end =', $event_start); 
    $this->db->or_where('resource_calendar.start =', $event_end); 
    $data = $this->db->get(); 

    $lowest_value = null; 
    $highest_value = null; 
    foreach ($data as $the_key => $the_value) { 
     if ($the_key == 'start') { 
      if ($lowest_value === null) { 
       $lowest_value = $the_value; 
      } 

      if ($the_value < $lowest_value) { 
       $lowest_value = $the_value; 
      } 
     } 
    } 
    //echo $lowest_value; 
    var_dump ($lowest_value) ; 
} 

回答

-1

我不知道是否有一種方法,你不能在SELECT語句或沒有做到這一點,我會認爲沒有。將你的結果對象放入一個數組中。這是超級基礎,但應該完成工作。

$lowest_value = null; 
$highest_value = null; 
foreach(result_object as $the_key => $the_value) { 
    if($the_key == 'start') { 
     if($lowest_value === null) { 
      $lowest_value = $the_value; 
     } 

     if(the_value < $lowest_value) { 
      $lowest_value = $the_value; 
     } 
    } 
} 

做到底值的第二檢查只是改變了運營商

+0

嗨,山姆,thx的解決方案。不幸的是,當我執行var_dump時,我得到NULL值。當我回應價值時,我沒有看到任何結果。我做錯了什麼?請參閱上面的完成函數... – MikeTSF 2014-10-06 17:59:30

+0

您只是遍歷一個對象的屬性。如果'result_object'應該指向包含這些對象的數組,'$ the_key'將始終爲數字,因此'start'的比較失敗。 – dognose 2014-10-06 18:02:35

+0

那麼我應該如何解決這個問題,dognose? – MikeTSF 2014-10-06 18:05:29

0

山姆的代碼應該是這樣的:

$lowest_value = null; 
$highest_value = null; 
foreach($data as $obj) { //$data should be the array containing the result objects. 
    $start = $obj->start; 
    $end = $obj->end; 

    if ($lowest_value === null) 
     $lowest_value = $start; 
    else 
     if ($lowest_value > $start) 
     $lowest_value = $start; 

    if ($highest_value === null) 
     $highest_value = $end; 
    else 
     if ($highest_value < $end) 
     $highest_value = $end; 
} 

可以縮短爲:

if ($lowest_value === null || $lowest_value > $start) 
     $lowest_value = $start; 

    if ($highest_value === null || $highest_value < $end) 
     $highest_value = $end; 

這兩個值都可以在一次迭代中生成,而無需 問題。請記住,如果您按照給定的格式保留日期,則<>適用。另外,您需要使用日期功能來比較日期。

你也可以使用MySQL來直接查詢的最小值/最大值使用聚合方法最小或最大:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

(如果您不需要每個對象被加載,這將是更快)

+0

嗨Dognose,代碼仍然給我沒有價值,但我確實得到正確的值與最小值和最大值,所以thx的技巧.... – MikeTSF 2014-10-06 18:27:49

+0

@ user3739279 np。代碼的邏輯看起來很像這樣(它未經測試的)。因此,唯一可能與此代碼不匹配的是輸入「$ data」 - 或者它是空的,這將是ofc。保持所有值爲空。 – dognose 2014-10-06 18:37:42