Welcome Guest, Not a member yet? Register   Sign In
Is there a way to combine these two functions? Category System
#1

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:
<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 
Reply
#2

Combining is easy:

PHP Code:
if( $subject_list $this->Posts_categories_model->get_list() )
{
 
   foreach$subject_list as $sub )
 
   {
 
       $data['subject_options'][$sub->id        $sub->title;
 
       $data['subject_name_options'][$sub->title] = $sub->title
 
   }

Reply
#3

(02-15-2018, 05:33 PM)skunkbad Wrote: Combining is easy:

PHP Code:
if( $subject_list $this->Posts_categories_model->get_list() )
{
 
   foreach$subject_list as $sub )
 
   {
 
       $data['subject_options'][$sub->id        $sub->title;
 
       $data['subject_name_options'][$sub->title] = $sub->title
 
   }


If I do that I just get them repated, the difference between them is the value would "value=1" or value="category-one".
Not just that I' trying to add them here(the first two lines):

PHP Code:
           $data = array(
                
'subject_id'    => $this->input->post('subject_id'),
// Obviously the input below will not work but I had to try it out.
                
'subject_name'    => $this->input->post($sub->title),
                
'slug'            => $slug,
                
'title'            => $this->input->post('title'),
                
'post_image'    => $post_image,
                
'body'            => $this->input->post('body'),
                
'is_published'    => $this->input->post('is_published'),
 
           ); 


Doing it as you say it would just fill the subject_id input, and I want one input to fill the two values.
How can I fill in the second value(subject_name) according to the subject_id?
I do Front-End development most of the time 
Reply
#4

(This post was last modified: 02-16-2018, 04:21 AM by InsiteFX.)

CodeIgniter documentation see below the form_dropdown.

SEE: form_multiselect
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

I was merely combining the code you had in the first section. I was not trying to solve any problems. It is my perception that you may need to learn a little more PHP, and that perhaps you jumped on the CodeIgniter bandwagon a little early. Whatever the case, your learning experience is now, when you figure it out.
Reply
#6

(02-16-2018, 09:03 AM)skunkbad Wrote: I was merely combining the code you had in the first section. I was not trying to solve any problems. It is my perception that you may need to learn a little more PHP, and that perhaps you jumped on the CodeIgniter bandwagon a little early. Whatever the case, your learning experience is now, when you figure it out.

Yes, I know but the combination was not the problem, I could do that by myself with my own example just like:

PHP Code:
           $subject_options = array();
            
$subject_name_options = array();
 
           $subject_options[0] = 'Select Post Category ID';
            
$subject_name_options[0] = 'Select Post Category Name';

 
           $subject_list $this->Posts_categories_model->get_list();
            
            foreach(
$subject_list as $subject){
 
               $subject_options[$subject->id] = $subject->title;
                
$subject_name_options[$subject->title] = $subject->title;                                
            }

 
           $data['subject_options'] = $subject_options;
            
$data['subject_name_options'] = $subject_name_options

which is what I had before asking but I decided to divided them into two fuctions to make more clear what I was asking for. The problem of what I really want is to create a tag system like Wordpress in which the only necessary action is to select the cat/tag and it shows the link and its corresponding name in the front-end(per post)... Thats what I'm really having troubles with. I think WordPress call it "get term by id" or something like that.
I do Front-End development most of the time 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB