How to Fetch and Update Data in same page CodeIgniter?

scripts

#1

I got stuck in a a problem in which I need to update some info from database in the same page that is shown.

On this case I'm fetching some "global settings" from a website in an index page which comes with a form in it. Here is a picture of it just to make it more clear to understand what I mean.

As you can see I created the button and I made it possible to see the values from the database, the problem is that I can not figure it out how to update it from there. Can somebody suggest an idea?

Here is my controller:

`   public function index()
    {
        
        $this->form_validation->set_rules('website_favicon', 'Favicon', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('website_logo', 'Logon', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('website_name', 'Website Name', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('website_credits', 'Credits', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('website_credits_link', 'Credits Link', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('website_copyright', 'Copyright', 'trim|required|min_length[4]');
        
        if($this->form_validation->run() == FALSE){
        
            // Get Current Subject
            $data['item'] = $this->Settings_model->get_website_data();
            //Load View Into Template
            $this->template->load('admin', 'default', 'settings/index', $data);
            
        } else {
            
            // Create website settings
            $data = array(
                'website_favicon'    => $this->input->post('website_favicon'),
                'website_logo'        => $this->input->post('website_logo'),
                'website_name'        => $this->input->post('webiste_name'),
                'website_credits'    => $this->input->post('website_credits'),
                'website_credits_link'    => $this->input->post('website_credits_link'),
                'website_copyright'    => $this->input->post('website_copyright'),
            );
            
            // Update User
            $this->Settings_model->update($id, $data);

            // Activity Array
            $data = array(
                'resource_id' => $this->db->insert_id(),
                'type'        => 'website settings',
                'action'      => 'updated',
                'user_id'     => $this->session->userdata('user_id'),
                'message'     => 'User (' . $data["username"] . ') updated the website settings'
            );

            // Add Activity  
            $this->Activity_model->add($data);

            //Create Message
            $this->session->set_flashdata('success', 'Website setting has been updated');

            //Redirect to Users
            redirect('admin/settings');
        
        } 
    } `

Here is my model:

<?php

class Settings_model extends CI_MODEL
{

    function __construct()
    {
        parent::__construct();
        $this->table = 'website_settings';
    }


    public function update($id, $data)
    {
        $this->db->where('id', $id);
        $this->db->update($this->table, $data);
    }


    public function get_website_data()
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('id', 1);
        $this->db->limit(1);

        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return $query->row();
        } else {
            return false;
        }

    }

}

and here is my view:

<h2 class="page-header">Website Settings</h2>
<?php if($this->session->flashdata('success')) : ?>
<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i> Alert!</h4>
<?php echo $this->session->flashdata('success') ?></div>
<?php endif; ?>

<?php if($this->session->flashdata('error')) : ?>
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i> Alert!</h4>
<?php echo $this->session->flashdata('error') ?></div>
<?php endif; ?>

<?php echo validation_errors('<p class="alert alert-danger">'); ?>
<?php echo form_open('admin/settings/index/'.$item->id); ?>
   <!-- Website Favicon -->
   <div class="form-group">
       <?php echo form_label('Website Favicon', 'title'); ?>
       <?php
           $data = array(
               'name' => 'website_favicon',
               'id'    => 'website_favicon',
               'maxlength' => '100',
               'class'     => 'form-control',
               'value'     => $item->website_favicon
           );
       ?>
       <?php echo form_input($data); ?>
   </div>
   <!-- Website Logo -->
   <div class="form-group">
       <?php echo form_label('Website Logo', 'website_logo'); ?>
       <?php
           $data = array(
               'name' => 'website_logo',
               'id'    => 'website_logo',
               'maxlength' => '100',
               'class'     => 'form-control',
               'value'     => $item->website_logo
           );
       ?>
       <?php echo form_input($data); ?>
   </div>
   <!-- Website Name -->
   <div class="form-group">
       <?php echo form_label('Website Name', 'website_name'); ?>
       <?php
           $data = array(
               'name' => 'website_name',
               'id'    => 'website_name',
               'maxlength' => '100',
               'class'     => 'form-control',
               'value'     => $item->website_name
           );
       ?>
       <?php echo form_input($data); ?>
   </div>
   <!-- Website Credits -->
   <div class="form-group">
       <?php echo form_label('Website Credits to', 'website_credits'); ?>
       <?php
           $data = array(
               'name' => 'website_credits',
               'id'    => 'website_credits',
               'maxlength' => '100',
               'class'     => 'form-control',
               'value'     => $item->website_credits
           );
       ?>
       <?php echo form_input($data); ?>
   </div>
   <!-- Website Credits Link -->
   <div class="form-group">
       <?php echo form_label('Website Credits to Link', 'website_credits_link'); ?>
       <?php
           $data = array(
               'name' => 'website_credits_link',
               'id'    => 'website_credits_link',
               'maxlength' => '100',
               'class'     => 'form-control',
               'value'     => $item->website_credits_link
           );
       ?>
       <?php echo form_input($data); ?>
   </div>
   <!-- Website Copyright -->
   <div class="form-group">
       <?php echo form_label('Copyrights', 'website_copyright'); ?>
       <?php
           $data = array(
               'name' => 'website_copyright',
               'id'    => 'website_copyright',
               'maxlength' => '100',
               'class'     => 'form-control',
               'value'     => $item->website_copyright
           );
       ?>
       <?php echo form_input($data); ?>
   </div>
   <!-- Website First Ad -->
   <div class="form-group">
       <?php echo form_label('Ad One', 'website_first_ad'); ?>
       <?php
           $data = array(
               'name' => 'website_first_ad',
               'id'    => 'website_first_ad',
               'maxlength' => '100',
               'class'     => 'form-control',
               'value'     => $item->website_first_ad
           );
       ?>
       <?php echo form_textarea($data); ?>
   </div>
   <!-- Website Second Ad -->
   <div class="form-group">
       <?php echo form_label('Ad Two', 'website_second_ad'); ?>
       <?php
           $data = array(
               'name' => 'website_second_ad',
               'id'    => 'website_second_ad',
               'maxlength' => '100',
               'class'     => 'form-control',
               'value'     => $item->website_second_ad
           );
       ?>
       <?php echo form_textarea($data); ?>
   </div>
   <!-- Website Third Ad -->
   <div class="form-group">
       <?php echo form_label('Ad Three', 'website_third_ad'); ?>
       <?php
           $data = array(
               'name' => 'website_third_ad',
               'id'    => 'website_third_ad',
               'maxlength' => '100',
               'class'     => 'form-control',
               'value'     => $item->website_third_ad
           );
       ?>
       <?php echo form_textarea($data); ?>
   </div>

   <?php echo form_submit('mysubmit', 'Update Website', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>

Thanks for helping.
Also any idea how can I use this globally in a template?


#2

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.