Codeigniter form with dropdown select with categoryname insert to categoryid in blog table and category table with catgoryname

StackOverflow https://stackoverflow.com/questions/23316093

  •  10-07-2023
  •  | 
  •  

Question

I have news id, news slug, news details, news_img, category id in News table and category id category name, categoryslug in category table. I have here 2 problems:

  1. dropdown displaying category name
  2. add blog in selecting the category

The commented part is the own I tried to do manually but I want dynamic.

I want to know whether the category dropdown should have category_id or categoryname. Here after form_dropdown, I want to list category name. And when blog is submitted I want to categoryid in news table and categoryname and id of categorytable get the submitted values.

I would also like if you can show how to integrate an image upload for blog that automatically creates a folder with blog title as foldername and stores images of that particular blog post only. I would be happy if you can show it using Plupload in CI.

I am using CodeIgniter with HMVC

<?php // Change the css classes to suit your needs    
$attributes = array('class' => '', 'id' => '');
echo form_open_multipart('blogs/add', $attributes); ?>
<h1>Add a blog </h1>
<div class="form-group">
    <label for="news_title">News Title <span class="required">*</span></label>
    <?php echo form_error('news_title'); ?>
    <?php echo form_input( array( 'name' => 'news_title', 'class' => 'form-control', 'id' =>'news_title', 'required' => 'required','placeholder' => 'Enter a title','rows' => '5', 'cols' => '80', 'value' => set_value('news_title') ) );?>
</div>
<div class="form-group">
    <label for="news_slug">News Slug <span class="required">*</span></label>
    <?php echo form_error('news_slug'); ?>
    <?php echo form_input( array( 'name' => 'news_slug', 'class' => 'form-control', 'id' =>'news_slug', 'required' => 'required','placeholder' => 'Separate each word by underscore','rows' => '5', 'cols' => '80', 'value' => set_value('news_slug') ) );?>
</div>
<div class="form-group">
    <label for="news_body">News <span class="required">*</span></label>
    <?php echo form_error('news_body'); ?>
    <?php echo form_textarea( array( 'name' => 'news_body', 'class' => 'form-control', 'id' =>'newsbody','rows' => '5', 'cols' => '80','placeholder' => 'Write here an article for blog', 'value' => set_value('news_body') ) )?>
</div>
<div class="form-group">
    <label for="category_id">News category <span class="required">*</span></label>
    <?php echo form_error('category_id'); ?>
    <?php // Change the values in this array to populate your dropdown as required ?>

    <?php /** $options = array(
        ''          => 'Select category',
        'value1'    => 'Web Development',
        'value2'    => 'Domain',
        'value3'    => 'Web Hosting',
        'value4'    => 'Events',
        'value5'    => 'SEO',
        'value6'    => 'Social Media Marketing',
        'value7'    => 'Vacancy'
    ); ?>
    <?php $htmlelements = 'class = "form-control" id="subject" required="required"';
    echo form_dropdown('news_id', $options, set_value('category_id'), $htmlelements);
    */

    form_dropdown('category_id', $drop_category_id,$category_id);
    ?>
</div>
<?php echo "<br/>" . 
form_submit(array('name' => 'submit', 'class' => 'btn btn-primary', 'id' => 'btnSubmit'), 'Submit'); ?>
<?php echo form_close(); ?>
Was it helpful?

Solution

You need to have field in db table for category category name. Option 1: You can either add it to blog's table

Option 2 (Harder method but better): You can create another table for category and then have its primary key as a foreign key in your blog table.

Learn about joining table because you need to join these table to make it work.

http://www.w3schools.com/sql/sql_join.asp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top