1

Hi im new to codeigniter and i stuck in displaying data from database. I have tried to find solution but yet can't understand them properly. So can anyone help me with this? really need your expert suggestions thankyou!!

View file Userinsert_view.php

<html>
<head>
<title>Insert Data Into Database Using CodeIgniter Form</title>
</head>
<body>

<div id="container">
<?php echo form_open('Userinsert_controller'); ?>
<h1>Insert Data Into Database Using CodeIgniter</h1><hr/>
<?php if (isset($message)) { ?>
<CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER><br>
<?php } ?>
<?php echo form_label('Student Name :'); ?> <?php echo form_error('dname'); ?><br />
<?php echo form_input(array('id' => 'dname', 'name' => 'dname')); ?><br />

<?php echo form_label('Student Email :'); ?> <?php echo form_error('demail'); ?><br />
<?php echo form_input(array('id' => 'demail', 'name' => 'demail')); ?><br />

<?php echo form_label('Student Mobile No. :'); ?> <?php echo form_error('dmobile'); ?><br />
<?php echo form_input(array('id' => 'dmobile', 'name' => 'dmobile', 'placeholder' => '10 Digit Mobile No.')); ?><br />

<?php echo form_label('Student Address :'); ?> <?php echo form_error('daddress'); ?><br />
<?php echo form_input(array('id' => 'daddress', 'name' => 'daddress')); ?><br />

<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?><br/>
<div id="fugo">

</div>
</div>
</body>
</html>

Controller file

Userinsert_controller.php

  <?php

class Userinsert_controller extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->model('Userinsert_model');
}
function index() {
//Including validation library
$this->load->library('form_validation');

$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

//Validating Name Field
$this->form_validation->set_rules('dname', 'Username', 'required|min_length[5]|max_length[15]');

//Validating Email Field
$this->form_validation->set_rules('demail', 'Email', 'required|valid_email');

//Validating Mobile no. Field
$this->form_validation->set_rules('dmobile', 'Mobile No.', 'required|regex_match[/^[0-9]{10}$/]');

//Validating Address Field
$this->form_validation->set_rules('daddress', 'Address', 'required|min_length[10]|max_length[50]');

if ($this->form_validation->run() == FALSE) {
$this->load->view('Userinsert_view');
} else {
//Setting values for tabel columns
$data = array(
'Student_Name' => $this->input->post('dname'),
'Student_Email' => $this->input->post('demail'),
'Student_Mobile' => $this->input->post('dmobile'),
'Student_Address' => $this->input->post('daddress')
);
//Transfering data to Model
$this->Userinsert_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('Userinsert_view', $data);
}
}

Model file

Userinsert_model.php

<?php
class Userinsert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
// Inserting in Table(students) of Database(college)
$this->db->insert('students', $data);
}
}
?>
5
  • what is not working?why did you write Data inserted successfully in view ? echo $message; is much better Commented May 7, 2017 at 12:05
  • Where do you want to display data? I can see you are only coded for inserting data in the database but you didn't code a single line to show data. How should we know where you need to display data? Please explain!! Commented May 7, 2017 at 14:09
  • @AbhishekSingh i removed the code and just uploaded the code which was working properly!! and anywhere where data can be shown using veiw.php file Commented May 7, 2017 at 16:06
  • i can show you my code what i was trying to do if you say so! @AbhishekSingh Commented May 7, 2017 at 16:11
  • As an aside, when you do this "Student_Name' => $this->input->post('dname')," Do it like this "Student_Name' => html_escape($this->input->post('dname')). please escape your inputs. Its just a good habit even is the form is already escaped Commented May 8, 2017 at 0:01

2 Answers 2

3
    in controller function 

public function GetAll(){
    $data['all_data'] = $this->Userinsert_model->selectAllData();
    $this->load->view('view_page', $data);
}




in model 


    public function selectAllData() {
        $query = $this->db->get('students');
        return $query->result();
    }

in view 
        <?php

    foreach ($all_data as $show):
        ?>
          <tr>

            <td><?php echo $show->your_table_column_name?></td>
          </tr>

        <?php

    endforeach;
    ?>
Sign up to request clarification or add additional context in comments.

1 Comment

thankyou sir very very much!!! i was doing something like this but i view file i wasn't able to write code properly!!! thankyou @pervaz sir very much!!
0

i have read your question you didn't write write the code for fetching data. you only submitted it in you database.

here is simple example so you can easily finds out the solution

in your controller file:

public function view()
    {
                $this->load->model('uModel'); //edit it with you model name

                $data['users']= $this->uModel->All();

                $this->load->view('list' , $data);  
    }

in your model file:

//Func for getting all data of a table in a 'users' variable by using get method 

public function All()                   
    {
        return $users = $this->db->get('users')->result_array();
    }

and create a table view file in view folder where you can fetch your data

<tbody>

<?php if (!empty($users)) { foreach ($users as $user) { ?>      

  <tr>
    <td>    <?php echo $user['userid'] ?></td>
    <td>    <?php echo $user['name'] ?></td>
  </tr>
<?php } }  
?>

Hope it will help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.