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);
}
}
?>
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;
?>
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
Related
I'm working with CodeIgniter. I have a form which enters data in a database but I'm facing a problem here saying "Unknown column Student_Name in field list in codeigniter". I have tried but I can't solve it so far.
Here is my code.
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);
}
}
?>
You are making a mistake in passing values to desired database columns. Make sure $data array's key exist with same column name in database table:
$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') );
You are passing the value in Student_name index and it must be present in the table. Careful about case sensitive column names.
Let me know if you still face the issue.
i have been working on it and didn't get a proper solution.what i want to do is i have a function in controller that gets data from model .
this is my controller function:
function index() {
$data['results1'] = $this->RetrieveData_model->GetData();
$this->load->view('ViewData',$data);
}
i want to access that $data['results1'] variable in my editview to auto fill the fields.this is my view:
<div id="container">
<?php echo form_open('insert_ctrl/updateData/'); ?>
<h1>Update Data Into Database Using CodeIgniter</h1><hr/>
<?php if (isset($message)) { ?>
<CENTER><h3 style="color:green;">Data updated 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', 'value' => $results1[0]->Student_Name)); ?><br />
<?php echo form_label('Student Email :'); ?> <?php echo form_error('demail'); ?><br />
<?php echo form_input(array('id' => 'demail', 'name' => 'demail', 'value' => $results1[0]->Student_Email)); ?><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.', 'value' => $results[0]->Student_Mobile)); ?><br />
<?php echo form_label('Student Address :'); ?> <?php echo form_error('daddress'); ?><br />
<?php echo form_input(array('id' => 'daddress', 'name' => 'daddress','value' => $results1[0]->Student_Address)); ?><br />
<?php echo form_hidden( array('id' => 'studentId', 'name' => 'studentId','value' => $results1[0]->Student_id) ); ?><br />
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?><br/>
<div id="fugo">
</div>
<button type="button" onclick="window.location='<?php echo base_url();?>ViewData_ctrl/index'">
View data Record
</button>
</div>
when i accessed that "result1" in view it is giving error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: results1
Filename: views/EditView.php
Line Number: 16
Backtrace:
File: D:\dev\htdocs\CI2\application\views\EditView.php
Line: 16
Function: _error_handler
File: D:\dev\htdocs\CI2\application\controllers\insert_ctrl.php
Line: 104
Function: view
File: D:\dev\htdocs\CI2\index.php
Line: 292
Function: require_once
Please help how can i solve this as i am new to Codeignitor..
Try this in your controller you given wrong view name
function index() {
$data['results1'] = $this->RetrieveData_model->GetData();
$this->load->view('EditView',$data);
}
and your view Var_dump your array.
<?php
var_dump($results1);
?>
check your file name
$this->load->view('ViewData',$data);
// change to
$this->load->view('EditView',$data);
there is an other mistake in your editview file:
you have skipped "1" in $results1 array it may help you out
$results1[0]->Student_Mobile;
Try this answer.you wil get the result very easily
in controller
function index() {
$data['results1'] = $this->RetrieveData_model->GetData();
$this->load->view('ViewData',$data);
}
in view page
<?php
foreach($results1 as $row)
{
echo $row->id; //echo your fields like this
}
?>
In view page you can use
<?php echo form_open('insert_ctrl/updateData/'); ?>
<?php
foreach($results1 as $row)
{
//try this to get details
echo $row->Student_Address;
echo $row->Student_id;
//or try this
echo $row['Student_Address'];
echo $row['Student_id'];
}
?>
After submitting the form, i insert the information in database.Everything is fine,except, the image is not inserted in databse,it shows 0 in image name field in db.Please guys, watch in the form_open() tag,and my img input,what is wrong here? It will be great help .
Thank you
The Controller:
<?php
class insert_ctrl extends Controller {
function __construct() {
parent::__construct();
$this->load->model('insert_model');
}
function index() {
$this->load->model('category_model');
$fata['all_categories'] = $this->category_model->all_categories();
//Setting values for tabel columns
$data = array(
'product_name' => $this->input->post('dname'),
'product_desc' => $this->input->post('desc'),
'product_price' => $this->input->post('price'),
'image'=> $this->input->post('img'),
'product_options'=> $this->input->post('options'),
);
$this->insert_model->form_insert($data);
$product_id = $this->db->insert_id();
$categories= $this->input->post('cat');
/*echo '<pre>';
print_r($categories);
*
*/
if(!empty($categories)){
foreach($categories as $value){
$res_arr=explode('_',$value);
$cat_id=$res_arr[0];
$cat_name=$res_arr[1];
$this->insert_model->cate_insert($product_id,$cat_id);
}
}
//$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('insert_view',$fata);
}
}
?>
The view:
<html>
<head>
<title>Insert Data Into Database Using CodeIgniter Form</title>
<link href='http://fonts.googleapis.com/css?family=Marcellus' rel='stylesheet' type='text/css'/>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>include/insert_style.css" />
<style>
input[type=checkbox]{
margin: 5px -167px 20px;
}
</style>
</head>
<body>
<div id="container">
<?php echo form_open_multipart('insert_ctrl'); ?>
<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('Product Name :'); ?> <?php echo form_error('dname'); ?><br />
<?php echo form_input(array('id' => 'dname', 'name' => 'dname')); ?><br />
<?php echo form_label('Product Description :'); ?> <?php echo form_error('desc'); ?><br />
<?php echo form_input(array('id' => 'desc', 'name' => 'desc')); ?><br />
<?php echo form_label('Product Price :'); ?> <?php echo form_error('price'); ?><br />
<?php echo form_input(array('id' => 'price', 'name' => 'price', 'placeholder' =>'' )); ?><br />
<?php echo form_label('Product options :'); ?> <?php echo form_error('options'); ?><br />
<?php echo form_input(array('id' => 'options', 'name' => 'options', 'placeholder' =>'' )); ?><br />
<?php echo form_open_multipart('insert_ctrl');?>
<?php echo form_input(array('id'=>'img','name'=>'img','type'=>'file'));?>
<
<?php foreach ($all_categories as $v_menu) { ?>
<input type="checkbox" id="cat_<?php echo $v_menu->id; ?>" value="<?php echo $v_menu->id.'_'.$v_menu->name; ?>" name="cat[]">
<?php echo $v_menu->name; ?>
<?php echo '<br/>'; ?>
<?php } ?>
<?php echo form_submit(array('id' => 'submit','name'=>'submit', 'value' => 'Add Product')); ?>
<?php echo form_close(); ?><br/>
<div id="fugo">
</div>
</div>
</body>
</html>
This won't work.
$this->input->post('img')
$_FILES used for getting image in core php and in CI use codeigniter library for uploading image.
You are put form_open_multipart two times in a form.
For more Information - http://www.codeigniter.com/user_guide/libraries/file_uploading.html
I have used codeigniter in the past but I am building a social network and I am working on the registration. I have autoloaded the form and form validation helpers. I have a form going to my user controller/register function and I have validation rules setup but its ignoring them and proceeding to the next page. I cant for the life of me figure out why. Any help is appreciated..the code and rules are not complete (obviously) but the validation should work at this point
VIEW
<?php echo validation_errors('<p class="error">'); ?>
<?php echo form_open('user/register/1'); ?>
<?php echo form_hidden('register_id', $randomcode); ?>
<?php echo form_label('First Name', 'first_name'); ?>
<?php echo form_input('first_name',set_value('first_name')); ?>
<br />
<?php echo form_label('Last Name', 'last_name'); ?>
<?php echo form_input('last_name',set_value('last_name')); ?>
<br />
<?php echo form_label('Gender', 'gender:'); ?>
<?php echo form_radio('gender', 'male'); ?> Male
<?php echo form_radio('gender', 'female'); ?> Female
<br />
<?php echo form_label('Date of Birth:', 'birthdate'); ?>
<?php echo form_date('birthdate'); ?>
<br />
<?php echo form_label('Zipcode:', 'zipcode'); ?>
<?php echo form_input('zipcode',set_value('zipcode')); ?>
<br />
<?php echo form_label('Email:', 'email'); ?>
<?php echo form_email('email',set_value('email')); ?>
<br />
<?php echo form_label('Password', 'password'); ?>
<?php echo form_password('password'); ?>
<br />
<?php echo form_label('Confirm', 'password2'); ?>
<?php echo form_password('password2'); ?>
<br />
<?php
$data = array(
'name' => 'submit',
'class' => 'big-blue-btn',
'value' => 'Sign Up'
);
?>
<?php echo form_submit($data); ?>
<?php echo form_close(); ?>
CONTROLLER
public function register($step){
if($step == 1){
$this->form_validation->set_rules('first_name', 'first_name', 'required');
$this->form_validation->set_rules('last_name', 'last_name', 'required');
if($this->form_validation->run() == FALSE){
//Views
$data['main_content'] = 'public/user/register1';
$this->load->view('public/template', $data);
} else {
//Post values to array
$data = array(
'register_id' => $this->input->post('register_id'),
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'gender' => $this->input->post('gender'),
'birthdate' => $this->input->post('birthdate'),
'zipcode' => $this->input->post('zipcode'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'password2' => $this->input->post('password2')
);
//Send input to model
$this->User_model->register1($data);
$data['show_sidebar'] = FALSE;
}
} elseif($step == 2){
//Send input to model
$this->User_model->register2($data);
} elseif($step == 3){
//Send input to model
$this->User_model->register3($data);
}
}
Review your code, you're missing a closing bracket on your first if.
i am working on a small form that summits data to a table on to a database nothing spectacular. i am trying to add some validation rules but they are not working i am still a beginner at php and codeigniter so cant figure out why can someone look at my code and help me out tnx in advance.
view
<html>
<head>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('http://localhost/Surva/index.php/info/credentials/'); ?>
<ul id="info">
<li><label for='name'>Name:</label><?php echo form_input('name')?>
</li>
<li><label for='second_name'>Second Name:<label> <?php echo form_input('second_name');?>
</li>
<li><label fro='phpne'>Phone:</label> <?php echo form_input('phone');?></li>
<li><label for='email'>Email: </label><?php echo form_input('email');?>
</li>
<li><?php echo form_submit('submit', 'Start survay!!' );?></li>
</ul>
<?php echo form_close();?>
</body>
</html>
controller
<?php
class Info extends CI_Controller{
function index(){
$this->load->view('info_view');
}
function credentials()
{
$data = array(
'name' => $this->input->post('name'),
'second_name' => $this->input->post('second_name'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
);
$this->info_model->add_record($data);
}
function validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|alpha|xss_clean');
$this->from_validation->set_rules('second_name', 'required|alpha|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE){
$this->load->view('info_view');
}else{
redirect('survaycontroller/index');
}
}
}
?>
i used the codeigniter user guide for validation in there explanation it looked very easy the whole structure is taken from the guide i daunt understand what the problem is .
You can do it Simple with one controller. I test it and it works!
Class name test.php
Controller name index
function index(){
$this->load->library('form_validation');
$data['name'] = $this->input->post('name');
$data['second_name'] = $this->input->post('second_name');
$data['phone'] = $this->input->post('phone');
$data['email'] = $this->input->post('email');
if($this->input->post('submit')) {
$this->form_validation->set_rules('name', 'Name', 'required|alpha|xss_clean');
$this->form_validation->set_rules('second_name', 'Second Name', 'required|alpha|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run()){
$this->info_model->add_record($data);
}
}
$this->load->view('test');
}
View: test.php
<html>
<head>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('test/index'); ?>
<ul id="info">
<li><label for='name'>Name:</label><?php echo form_input('name')?>
</li>
<li><label for='second_name'>Second Name:<label> <?php echo form_input('second_name');?>
</li>
<li><label fro='phpne'>Phone:</label> <?php echo form_input('phone');?></li>
<li><label for='email'>Email: </label><?php echo form_input('email');?>
</li>
<li><?php echo form_submit('submit', 'Start survay!!' );?></li>
</ul>
<?php echo form_close();?>
</body>
</html>
You're not sending the form to the method validation
view
<html>
<head>
</head>
<body>
<?php echo validation_errors();
//you must redirect the form to the right method, in this case your method is "validation" on the controller is "info"
//another thing, you don't need to put all the url, just put the controller and the method, this way when you migrate your website to a server you don't have to worry changing the url
echo form_open('info/validation'); ?>
<ul id="info">
<li><label for='name'>Name:</label><?php echo form_input('name')?>
</li>
<li><label for='second_name'>Second Name:<label> <?php echo form_input('second_name');?>
</li>
<li><label fro='phpne'>Phone:</label> <?php echo form_input('phone');?></li>
<li><label for='email'>Email: </label><?php echo form_input('email');?>
</li>
<li><?php echo form_submit('submit', 'Start survay!!' );?></li>
</ul>
<?php echo form_close();?>
</body>
</html>
controller
<?php
class Info extends CI_Controller{
//I've added the public before function
public function index(){
$this->load->view('info_view');
}
//In this one I've added private, Why? Because you don't peopple to use this method if they go to http://(yourdomain). This way you can only use this function inside this controller
private function credentials()
{
$data = array(
'name' => $this->input->post('name'),
'second_name' => $this->input->post('second_name'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
);
$this->info_model->add_record($data);
}
//this one is also as public, and it's the one who we'll receive the post request from your form
public function validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|alpha|xss_clean');
$this->from_validation->set_rules('second_name', 'required|alpha|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE){
$this->load->view('info_view');
}else{
//if the form is valid then you call the private function credentials and save the data to your database
$this->credentials();
redirect('survaycontroller/index');
}
}
}
?>
Check the alterations I made to your code, if you need some more explain or help you're welcome
second param missing in second rule
$this->from_validation->set_rules('second_name', 'Second Name', 'required|alpha|xss_clean');
Look at the example below, You have to check $this->form_validation->run() inside your credentials() function.
public function add_category()
{
$this->load->library('form_validation');
$data = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
//Validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required');;
$this->form_validation->set_rules('description', 'Description', 'trim|required');
$this->form_validation->set_rules('position', 'Position', 'trim|numeric');
$artist_category = $this->input->post('artist_category', TRUE);
$data['artist_category'] = $artist_category;
if($this->form_validation->run() === FALSE)
{
$this->template->write('title', 'Category : Add Category');
$this->template->write_view('content', 'category/add_category',$data);
$this->template->render();
}
else
{
$name = trim($this->input->post('name',TRUE));
$description = trim($this->input->post('description',TRUE));
$position = trim($this->input->post('position',TRUE));
$colour = trim($this->input->post('colour',TRUE));
$language_id = trim($this->input->post('language_id',TRUE));
//Add record to categories table
$category_id = $this->Category_model->insert_category($name,$description,$position,$colour,$date_added);
$this->session->set_flashdata('success_msg', 'Category added successfully.');
//Redirect to manage categories
redirect('category');
}
}
else
{
$this->template->write('title', 'Category : Add Category');
$this->template->write_view('content', 'category/add_category');
$this->template->render();
}
}