Hi, I'm working on a blog in which I need to display the categories it has, the problem is not that I can not get them, the problem is that I have to select the two values(category.id and category.title) in my
add view , basically I have two inputs and I'm trying to get them together in just one single select input.
Here is my functions, one for selecting the ID of the category, and a second one for the category's name.
PHP Code:
// Select Category ID
$subject_options = array();
$subject_options[0] = 'Select Post Category ID';
$subject_list = $this->Posts_categories_model->get_list();
foreach($subject_list as $subject){
$subject_options[$subject->id] = $subject->title;
}
$data['subject_options'] = $subject_options;
// Select Category Name
$subject_name_options = array();
$subject_name_options[0] = 'Select Post Category Name';
$subject_name_list = $this->Posts_categories_model->get_list();
foreach ($subject_name_list as $subject_name) {
$subject_name_options[$subject_name->title] = $subject_name->title;
}
$data['subject_name_options'] = $subject_name_options;
Here is my method get_list located in the Post_categories_model:
PHP Code:
public function get_list()
{
$query = $this->db->get($this->table);
return $query->result();
}
This is what I have in my add view:
PHP Code:
<!-- Post Subject ID -->
<div class="form-group">
<?= form_label('Post Category', 'subject_id'); ?>
<?= form_dropdown('subject_id',$subject_options,0, array('class' => 'form-control')); ?>
</div>
<!-- Post Subject Name -->
<div class="form-group">
<?= form_label('Post Category Name', 'subject_name'); ?>
<?= form_dropdown('subject_name',$subject_name_options,0, array('class' => 'form-control')); ?>
</div>
and there's another problem, when displaying the category name in my post view(index.php; see below), I have this:
PHP Code:
<i class="fa fa-folder-open"></i> <a href="<?= base_url(); ?>posts/category/<?= $page->subject_id; ?>"><?= $page->subject_name; ?></a>
it seems pretty simple but what can I do if there's more than one category per post?
I hope you guys can help me.
By the way this is what I have in my controller to display the posts:
PHP Code:
public function index()
{
// Get Posts
$data['posts'] = $this->Post_model->get_featured();
// Get Post Categories
$data['categories'] = $this->Post_model->get_categories();
// Load template
$this->template->load('public', 'default', 'posts/index', $data);
}
I do Front-End development most of the time