Reputation: 8385
Currently I am calling a title from the database using the below code it works fine but I am wanting to implement a view all area (pulling other rows from the options
table) in my admin area for the main options for my website.
What would be the best way to do this? Create another function for just the title? as I do not want to have a run a foreach loop just for the title.
function systemOptions()
{
$query = $this->db->get('options');
if($query->num_rows() > 0)
{
$row = $query->row_array();
$row['cms_name'];
}
return $row;
}
Upvotes: 0
Views: 163
Reputation: 972
I would do something like this;
<?php
/*
CREATE TABLE `options` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(64) DEFAULT NULL,
`value` text,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
);
INSERT INTO `options` (`id`, `name`, `value`)
VALUES
(1, 'generic_keywords', 'This,is,my,generic.keywords'),
(2, 'generic_description', 'This is a brief site summary'),
(3, 'page_status_active', '1'),
(4, 'page_status_draft', '2'),
(5, 'page_status_invisible', '2'),
(6, 'page_status_archived', '4'),
(7, 'listing_length', '20'),
(8, 'page_status_deleted', '9');
*/
function systemOptions() {
$options = FALSE;
$query = $this->db->get('options');
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data = new StdClass;
$data->{$row->name} = $value;
$options[] = $data;
}
}
return $options;
}
function systemOption($name) {
$query = $this->db->get_where('options', array('name' => $name), 1);
return ($query->num_rows() == 1) ? $query->row() : FALSE;
}
Upvotes: 1
Reputation: 2437
I think you should better return all the results in your model and extract it wherever you need an object as Colin said:
function systemOptions()
{
$query = $this->db->get('options');
if($query->num_rows() > 0)
{
$row = $query->result();
}
return $row;
}
and if you want to use it in index method in controller:
public function index()
{
$data[row] = $this->example_model->systemOptions();
$this->load->view('example_view', $data);
}
and use the code below wherever you need the object in your view file:
echo $row->cms_name;
echo $row->title;
Upvotes: 0