{ foreach ($hash as $key => $value) { echo format_key_value_pair($key, $value) . '<br>'; } } /** print out line of text with a label, to be used between PRE tags @param string $label a descriptive label or title @param string $line the text to display @return NULL */ function print_line($label, $line) { echo format_key_value_pair($label, $line) . "\n"; } /** extract an element from an array without generating e_notice 'errors' when the key is not set @param array $array the array to access @param string $key the array element to retrieve @return string the value for the key or an empty string when the key is not set */ function value_for_key($array, $key) { $value = ''; if (isset($array[$key])) { $value = $array[$key]; } return $value; } ?>
public function find($options = array()) { $db = DB::Instance(); //is $options a number if (is_numeric($options)) { //return object for ID $id = $options; $results = $db->where($this->get(true) . '.' . $this->primary_key(), $id)->limit(1)->get($this->get()); if ($results->num_rows) { $results = $this->process_results($results); return $results[0]; } return null; } else { $db->where($this->base_filter()); if (count($this->base_filter_joins())) { foreach ($this->base_filter_joins() as $join) { $db->join($join['table'], $join['on']); } } if ($where = value_for_key('where', $options)) { $db->where($where); } if ($having = value_for_key('having', $options)) { $db->having($having); } if ($limit = value_for_key('limit', $options)) { $offset = value_for_key('offset', $options, 0); $db->limit($limit, $offset); } if ($order = value_for_key('order', $options)) { $order = explode(' ', $order); if (count($order) == 2) { $db->order($order[0], $order[1]); } else { $db->order($order[0]); } } if ($select = value_for_key('select', $options)) { echo $select; $db->select($select); } $results = $this->process_results($db->get($this->get())); if ($include = value_for_key('include', $options)) { foreach ($include as $key) { if ($association = $this->association($key)) { if ($association['type'] == 'belongs_to') { $class_name = !empty($association['options']['class_name']) ? ucfirst($association['options']['class_name']) : ucfirst($association['name']); $class = new $class_name(); if (!empty($association['options']['foreign_key'])) { $foreign_key = $association['options']['foreign_key']; } else { $foreign_key = $association['name'] . '_id'; } $primary_key = $class->primary_key(); $ids = array(); foreach ($results as $row) { if ($row->{$foreign_key}) { $ids[] = $row->{$foreign_key}; } } if (count($ids)) { $include_results = $class->process_results($db->where('( ' . $primary_key . ' IN (' . implode(',', $ids) . ') )')->get($class->table_name())); foreach ($include_results as $include_row) { foreach ($results as $row) { if ($row->{$foreign_key} == $include_row->{$primary_key}) { $row->add_data($association['name'], $include_row); } } } } } } } } return $results; } }