2014-10-06 121 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