Если вы хотите отобразить проповедника с проповедью, вы должны добавить JOIN к вашему запросу, таким образом, если проповедник не проповедует, он не появится в результирующем наборе строк.
т.е.:
function viewAllpreachers($offset=0, $limit=null) {
$preacher_ids = array();
$this->db->distinct('preacher_id');
$this->db->select('preacher_id');
$this->db->from('sermons');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $key => $res1) {
$preacher = array();
$this->db->select('*');
$this->db->from('preacher');
$this->db->join('sermons', 'preacher.preacher_id = sermons.preacher_id');
$this->db->where('preacher_id', $res1->preacher_id);
$this->db->order_by('first_name');
$query = $this->db->get('', $limit, $offset);
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
$preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
$preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
$preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;
$this->db->select('*');
$this->db->from('sermons');
$this->db->where('preacher_id', $row->preacher_id);
$this->db->order_by('sort_order ');
$sermon_array = array();
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row1) {
$sermon_array[$row1->sermon_id] ['sermon_image'] = $row1->sermon_image;
$sermon_array[$row1->sermon_id] ['sermon_title'] = $row1->sermon_title;
$sermon_array[$row1->sermon_id] ['audio_file'] = $row1->audio_file;
$sermon_array[$row1->sermon_id] ['sermon_description'] = $row1->sermon_description;
}
}
$preacher[$row->preacher_id]['sermon'] = $sermon_array;
}
return $preacher;
}
} }
return false;
}
Примечание. Существует ли конкретная причина, по которой вы используете два запроса, а не только один, вы должны быть в состоянии получить список проповедников с проповедью в одномSQL-запрос.
function viewAllpreachers($offset=0, $limit=null) {
$preacher = array();
$this->db->select('*');
$this->db->from('preacher');
$this->db->join('sermons', 'preacher.preacher_id = sermons.preacher_id');
$this->db->where('preacher_id', $res1->preacher_id);
$this->db->order_by('first_name');
$query = $this->db->get('', $limit, $offset);
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
$preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
$preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
$preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;
$this->db->select('*');
$this->db->from('sermons');
$this->db->where('preacher_id', $row->preacher_id);
$this->db->order_by('sort_order');
$sermon_array = array();
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row1) {
$sermon_array[$row1->sermon_id] ['sermon_image'] = $row1->sermon_image;
$sermon_array[$row1->sermon_id] ['sermon_title'] = $row1->sermon_title;
$sermon_array[$row1->sermon_id] ['audio_file'] = $row1->audio_file;
$sermon_array[$row1->sermon_id] ['sermon_description'] = $row1->sermon_description;
}
}
$preacher[$row->preacher_id]['sermon'] = $sermon_array;
}
return $preacher;
}
return false;
}