$prev_record = null; $next_record = null; if ($res->num_rows() > 0) { foreach ($res->result() as $key=>$row) { list($y,$m,$d) = explode('/',date('Y/m/d',$row->date)); if ($found && is_null($next_record)) { $next_record = array( 'y'=>$y, 'm'=>$m, 'd'=>$d ); } else if ((int)$y == (int)$c_y && (int)$m == (int)$c_m && (int)$d == (int)$c_d) { $found = true; } else { if (!$found) { $prev_record = array( 'y'=>$y, 'm'=>$m, 'd'=>$d ); } } $girls[(int)$y][(int)$m][(int)$d] = site_url('girl/index/'.$y.'/'.$m.'/'.$d); } } $data['girls'] = $girls; $data['prev_girl'] = $prev_record; $data['next_girl'] = $next_record;