Welcome Guest, Not a member yet? Register   Sign In
Form Help
#1

(This post was last modified: 11-20-2019, 10:23 PM by Trainingday.)

Hello,

I am trying to learn CI and so far so good but not so for learning forms.  The script I found I am trying to edit to input data in db but it does not allow the new field I created in view to input the data.  The field i created extra is called "Organizations" with a simple select. The code does not have the traditional inputs that referenced in CI documentation.  

Here is my view and when I do a var dump all is being passed:

PHP Code:
            <?php
            $this
->lang->load('ps''english');
            
?>
            <ul class="breadcrumb">
                <li><a href="<?php echo site_url(). "/dashboard";?>"><?php echo $this->lang->line('dashboard_label')?></a> <span class="divider"></span></li>
                <li><a href="<?php echo site_url('feeds');?>"><?php echo $this->lang->line('feed_list_label')?></a> <span class="divider"></span></li>
                <li><?php echo $this->lang->line('add_new_feed_button')?></li>
            </ul>
            <div class="wrapper wrapper-content animated fadeInRight">
            <?php
            $attributes 
= array('id' => 'feed-form','enctype' => 'multipart/form-data');
            echo 
form_open(site_url('feeds/add'), $attributes);
            
?>
                <legend><?php echo $this->lang->line('feed_info_lable')?></legend>

                <?php $this->load->view'flash_message' ); ?>
                 
                
                
                
                <div class="row">
                    <div class="col-sm-8">
                            <div class="form-group">
                                <label><?php echo $this->lang->line('feed_title_label')?>
                                    <a href="#" class="tooltip-ps" data-toggle="tooltip" title="<?php echo $this->lang->line('feed_title_tooltips')?>">
                                        <span class='glyphicon glyphicon-info-sign menu-icon'>
                                    </a>
                                </label>
                                <?php 
                                    
echo form_input( array(
                                        
'type' => 'text',
                                        
'name' => 'title',
                                        
'id' => 'title',
                                        
'class' => 'form-control',
                                        
'placeholder' => 'Title',
                                        
'value' => ''
                                    
));
                                
?>
                            </div>
                            
                            <div class="form-group">
                                <label><?php echo $this->lang->line('description_label')?>
                                    <a href="#" class="tooltip-ps" data-toggle="tooltip" title="<?php echo $this->lang->line('feed_description_tooltips')?>">
                                        <span class='glyphicon glyphicon-info-sign menu-icon'>
                                    </a>
                                </label>
                                <textarea class="form-control" name="description" placeholder="Description" rows="9"></textarea>
                            </div>
                            
                        <div class="form-group">

                                    <label><?php echo $this->lang->line('organization_name_selection')?></label> <br>
                                    <select class="form-control" name='organization' id='organization'>
                                        <?php
                                            
foreach($this->item->get_all()->result() as $item)
                                                echo 
"<option value='".$item->id."'>".$item->name."</option>";
                                        
?>
                                    </select>
                                    
                                </div>
                            
                    </div>
                </div>
                
                <hr/>
                
                <input type="submit" name="save" value="<?php echo $this->lang->line('save_button')?>" class="btn btn-primary"/>
                
                <input type="submit" name="gallery" value="<?php echo $this->lang->line('save_go_button')?>" class="btn btn-primary"/>
                <a href="<?php echo site_url('feeds');?>" class="btn btn-primary"><?php echo $this->lang->line('cancel_button')?></a>
            </form>
            </div>
            <script>
            $(document).ready(function(){
                $('#feed-form').validate({
                    rules:{
                        title:{
                            required: true,
                            minlength: 4
                        }
                    },
                    messages:{
                        title:{
                            required: "Please fill title.",
                            minlength: "The length of title must be greater than 4"
                        }
                    }
                });
            });
            
            $(function () { $("[data-toggle='tooltip']").tooltip(); });
            
            </script> 

Here is my controller:

PHP Code:
<?php
require_once('Main.php');
class 
Feeds extends Main
{
    function 
__construct()
    {
        
parent::__construct('feeds');
        
$this->load->library('uploader');
    }
    
    function 
index()
    {
        
$this->session->unset_userdata('searchterm');
        
        
$pag $this->config->item('pagination');
        
$pag['base_url'] = site_url('feeds/index');
        
$pag['total_rows'] = $this->feed->count_all($this->get_current_city()->id);
        
        
$data['feeds'] = $this->feed->get_all($this->get_current_city()->id$pag['per_page'], $this->uri->segment(3));
        
$data['pag'] = $pag;
        
        
$content['content'] = $this->load->view('feeds/list',$data,true);        
        
        
$this->load_template($content);
    }
    
    function 
add()
    {
        if(!
$this->session->userdata('is_city_admin')) {
        
      $this->check_access('add');
        }
        
        
$action "save";
        unset(
$_POST['save']);
        if (
htmlentities($this->input->post('gallery'))) {
            
$action "gallery";
            unset(
$_POST['gallery']);
        }
        
        if (
$this->input->server('REQUEST_METHOD')=='POST') {

            
// server side validation
            
if ( ! $this->is_valid_input()) {
                
redirectsite_url'feeds/add' ));
            }

            
$feed_data $this->input->post();

            
$temp = array();
            foreach ( 
$feed_data as $key=>$value ) {
                
$temp[$key] = $value;
            }
            
$feed_data $temp;

            
$feed_data['city_id'] = $this->get_current_city()->id;
            
$feed_data['is_published'] = 1;
            if (
$this->feed->save($feed_data)) {            
                
$this->session->set_flashdata('success','Feed is successfully added.');
            } else {
                
$this->session->set_flashdata('error','Database error occured.Please contact your system administrator.');
            }
            
            if (
$action == "gallery") {
                
redirect(site_url('feeds/gallery/'.$feed_data['id']));
            } else {
                
redirect(site_url('feeds'));
            }
        }
        
        
$content['content'] = $this->load->view('feeds/add',array(),true);
        
        
$this->load_template($content);
    }
        
    function 
search()
    {
        
$search_term $this->searchterm_handler(array(
            
"searchterm"=>htmlentities($this->input->post('searchterm'))
        ));
        
$data $search_term;
        
        
$pag $this->config->item('pagination');
        
        
$pag['base_url'] = site_url('feeds/search');
        
$pag['total_rows'] = $this->feed->count_all_by($this->get_current_city()->id$search_term);
        
        
$data['feeds'] = $this->feed->get_all_by($this->get_current_city()->id$search_term,$pag['per_page'],$this->uri->segment(3));
        
$data['pag'] = $pag;
        
        
$content['content'] = $this->load->view('feeds/search',$data,true);        
        
        
$this->load_template($content);
    }
    
    function 
searchterm_handler($searchterms = array())
    {
        
$data = array();
        
        if (
$this->input->server('REQUEST_METHOD')=='POST') {
            foreach (
$searchterms as $name=>$term) {
                if (
$term && trim($term) != " ") {
                    
$this->session->set_userdata($name,$term);
                    
$data[$name] = $term;
                } else {
                    
$this->session->unset_userdata($term);
                    
$data[$name] = "";
                }
            }
        } else {
            foreach (
$searchterms as $name=>$term) {
                if (
$this->session->userdata($name)) {
                    
$data[$name] = $this->session->userdata($name);
                } else { 
                    
$data[$name] = "";
                }
            }
        }
        return 
$data;
    }
    
    function 
edit($feed_id=0)
    {
        if(!
$this->session->userdata('is_city_admin')) {
        
    $this->check_access('edit');
        }
        
        if (
$this->input->server('REQUEST_METHOD')=='POST') {

            
// server side validation
            
if ( ! $this->is_valid_input$feed_id )) {
                
redirectsite_url'feeds/edit/'$feed_id ));
            }

            
$feed_data $this->input->post();

            
$temp = array();
            foreach ( 
$feed_data as $key=>$value ) {
                
$temp[$key] = $value;
            }
            
$feed_data $temp;

            if (
$this->feed->save($feed_data$feed_id)) {
                
$this->session->set_flashdata('success','Feed is successfully updated.');
            } else {
                
$this->session->set_flashdata('error','Database error occured.Please contact your system administrator.');
            }
            
redirect(site_url('feeds'));
        }
        
        
$data['feed'] = $this->feed->get_info($feed_id);
        
        
$content['content'] = $this->load->view('feeds/edit',$data,true);        
        
        
$this->load_template($content);
    }
    
    function 
gallery($id)
    {
        
// session_start();
        
$_SESSION['parent_id'] = $id;
        
$_SESSION['type'] = 'feed';
        $content['content'] = $this->load->view('feeds/gallery', array('id' => $id), true);
        
        $this
->load_template($content);
    }
    
    function 
upload($feed_id=0)
    {
        if(!
$this->session->userdata('is_city_admin')) {
        
    $this->check_access('edit');
        }
        
        
$upload_data $this->uploader->upload($_FILES);
        
        if (!isset(
$upload_data['error'])) {
            foreach (
$upload_data as $upload) {
                
$image = array(
                                
'item_id'=> $feed_id,
                                
'type' => 'feed',
                                
'path' => $upload['file_name'],
                                
'width'=>$upload['image_width'],
                                
'height'=>$upload['image_height']
                            );
                
$this->image->save($image);
            }
        } else {
            
$data['error'] = $upload_data['error'];
        }
        
        
$data['feed'] = $this->feed->get_info($feed_id);
        
        
$content['content'] = $this->load->view('feeds/edit',$data,true);        
        
        
$this->load_template($content);
    }
    
    function 
publish($id 0)
    {
        if(!
$this->session->userdata('is_city_admin')) {
            
$this->check_access('publish');
        }
        
        
$feed_data = array(
            
'is_published'=> 1
        
);
            
        if (
$this->feed->save($feed_data,$id)) {
            echo 
'true';
        } else {
            echo 
'false';
        }
    }
    
    function 
unpublish($id 0)
    {
        if(!
$this->session->userdata('is_city_admin')) {
            
$this->check_access('publish');
        }
        
        
$feed_data = array(
            
'is_published'=> 0
        
);
            
        if (
$this->feed->save($feed_data,$id)) {
            echo 
'true';
        } else {
            echo 
'false';
        }
    }

    function 
delete($feed_id=0)
    {
        if(!
$this->session->userdata('is_city_admin')) {
        
     $this->check_access('delete');
        }
        
        
$images $this->image->get_all_by_type($feed_id'feed');
        foreach (
$images->result() as $image) {
            
$this->image->delete($image->id);
            
unlink('./uploads/'.$image->path);
        }
        
        if (
$this->feed->delete($feed_id)) {
            
$this->session->set_flashdata('success','Feed is successfully deleted.');
        } else {
            
$this->session->set_flashdata('error','Database error occured.Please contact your system administrator.');
        }
        
redirect(site_url('feeds'));
    }
    
    function 
delete_image($feed_id,$image_id,$image_name)
    {
        if(!
$this->session->userdata('is_city_admin')) {
        
    $this->check_access('edit');
        }
        
        if (
$this->image->delete($image_id)) {
            
unlink('./uploads/'.$image_name);
            
$this->session->set_flashdata('success','Image is successfully deleted.');
        } else {
            
$this->session->set_flashdata('error','Database error occured.Please contact your system administrator.');
        }
        
redirect(site_url('feeds/edit/'.$feed_id));
    }

    
/**
     * Determines if valid input.
     *
     * @return     boolean  True if valid input, False otherwise.
     */
    
function is_valid_input()
    {
        
$rule 'required|min_length[3]';

        
$this->form_validation->set_rules('title''Title'$rule );
        
$this->form_validation->set_rules('description''Description'$rule );

        if ( 
$this->form_validation->run() == FALSE ) {
            
$this->session->set_flashdata('error'validation_errors());
            return 
false;
        }

        return 
true;
    }
}
?>
 
My Model:

PHP Code:
<?php
class Feed extends Base_Model
{
    protected 
$table_name;
    
    function 
__construct()
    {
        
parent::__construct();
        
$this->table_name 'cd_feeds';
    }

    function 
exists($data)
    {
        
$this->db->from($this->table_name);
        
$this->db->where('city_id'$data['city_id']);
        
        if (isset(
$data['id'])) {
            
$this->db->where('id'$data['id']);
        }
        
        if (isset(
$data['city_id'])) {
            
$this->db->where('city_id'$data['city_id']);
        }
        
        
$query $this->db->get();
        return (
$query->num_rows() >= 1);
    }

    function 
save(&$data$id=false)
    {
        if (!
$id && !$this->exists(array('id' => $id'city_id' => $data['city_id']))) {
            if (
$this->db->insert($this->table_name$data)) {
                
$data['id'] = $this->db->insert_id();
                return 
true;
            }
        } else {
            
$this->db->where('id'$id);
            return 
$this->db->update($this->table_name$data);
        }    
        return 
false;
    }

    function 
get_all($city_id$limit=false$offset=false$order_field 'added'$order_type 'desc')
    {
        
$this->db->from($this->table_name);
        
$this->db->where('city_id'$city_id);
        
        if (
$limit) {
            
$this->db->limit($limit);
        }
        
        if (
$offset) {
            
$this->db->offset($offset);
        }
        
        
$this->db->order_by($order_field,$order_type);
        return 
$this->db->get();
    }
    
    function 
get_all_published($city_id$limit=false$offset=false$order_field 'added'$order_type 'desc')
    {
        
$this->db->from($this->table_name);
        
$this->db->where('city_id'$city_id);
        
$this->db->where('is_published'1);
        
        if (
$limit) {
            
$this->db->limit($limit);
        }
        
        if (
$offset) {
            
$this->db->offset($offset);
        }
        
        
$this->db->order_by($order_field,$order_type);
        return 
$this->db->get();
    }

    function 
count_all_published($city_id$limit=false$offset=false$order_field 'added'$order_type 'desc')
    {
        
$this->db->from($this->table_name);
        
$this->db->where('city_id'$city_id);
        
$this->db->where('is_published'1);
        
        if (
$limit) {
            
$this->db->limit($limit);
        }
        
        if (
$offset) {
            
$this->db->offset($offset);
        }
        
        
$this->db->order_by($order_field,$order_type);
        return 
$this->db->count_all_results();
    }

    function 
get_info($id)
    {
        
$query $this->db->get_where($this->table_name, array('id' => $id));
        
        if (
$query->num_rows()==1) {
            return 
$query->row();
        } else {
            return 
$this->get_empty_object($this->table_name);
        }
    }

    function 
get_multiple_info($ids)
    {
        
$this->db->from($this->table_name);
        
$this->db->where_in($ids);
        return 
$this->db->get();
    }

    function 
count_all($city_id)
    {
        
$this->db->from($this->table_name);
        
$this->db->where('city_id'$city_id);
        return 
$this->db->count_all_results();
    }
    
    function 
count_all_by($city_id$conditions=array())
    {
        
$this->db->from($this->table_name);
        
$this->db->where('city_id'$city_id);
        
        if (isset(
$conditions['searchterm']) && trim($conditions['searchterm']) != "") {
            
$this->db->where("(
                title LIKE '%"
$this->db->escape_like_str$conditions['searchterm'] ) ."%' OR 
                description LIKE '%"
$this->db->escape_like_str$conditions['searchterm'] ) ."%'
            )"
NULLFALSE);
        }
        return 
$this->db->count_all_results();
    }
    
    function 
get_all_by($city_id$conditions=array(), $limit=false$offset=false)
    {
        
$this->db->from($this->table_name);
        
$this->db->where('city_id'$city_id);
        
        if (isset(
$conditions['searchterm']) && trim($conditions['searchterm']) != "") {
            
$this->db->where("(
                title LIKE '%"
$this->db->escape_like_str$conditions['searchterm'] ) ."%' OR 
                description LIKE '%"
$this->db->escape_like_str$conditions['searchterm'] ) ."%'
             )"
NULLFALSE);
        }
        
        if (
$limit) {
            
$this->db->limit($limit);
        }
        
        if (
$offset) {
            
$this->db->offset($offset);
        }
        
        
$this->db->order_by('added','desc');
        return 
$this->db->get();
    }

    function 
delete($id)
    {
        
$this->db->where('id',$id);
        return 
$this->db->delete($this->table_name);
    }
    
    function 
delete_by_city($city_id)
    {
        
$this->db->where('city_id'$city_id);
        return 
$this->db->delete($this->table_name);
    }
    
    function 
read_more_text($string)
    {
        
$string strip_tags($string);
        
        if (
strlen($string) > 100) {
        
        
    // truncate string
        
    $stringCut substr($string0100);
        
        
    // make sure it ends in a word so assassinate doesn't become ass...
        
    $string substr($stringCut0strrpos($stringCut' ')).'...'
        }
        return 
$string;
    }
}
?>

thanks
Reply
#2

@Trainingday,

Any reason why your not using CI 3.x?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB