**Controller file i have form_ctrl.php and code given below**
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class form_ctrl extends CI_Controller {
public function index()
{
//$this->load->view('welcome_message');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('data_model');
//$this->form_validation->set_rules('name', 'Username', 'required');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$this->load->view('results');
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data = array(
'name' =>$name ,
'pass' => $pass,
'email' => $email,
'mobile' => $mobile,
'address' => $address
);
$this->data_model->insert_fun('form', $data);
}
}
function GetAll()
{
$this->load->model('emp_model');
$data['query']=$this->emp_model->emp_getall();
$this->load->view('emp_viewall',$data);
}
}
**model file i have data_model.php**
<?php
class data_model extends CI_Model {
function __construct() {
parent::__construct ();
}
public function insert_fun($tableName,$data){
return $this->db->insert($tableName, $data);
}
function emp_getall()
{
$this->load->database();
$query=$this->db->get('form');
return $query->result();
}
}
?>
view file i have results.php
<html>
<head>
<title>My Form</title>
</head>
<body>
<table width="100%" border="1">
<tr>
<td>Name</td>
<td>Email</td>
<td>Mobile</td>
<td>Address</td>
<td>Action</td>
</tr>
<?php
foreach($query as $row)
{
print_r($row);exit;
}
?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>
data is inserting properly but view not showing the database fields it means IN model file function (function emp_getall()) orin controller file (function GetAll()) is not working give me the solution where error in this code...
function GetAll()
{
$this->load->model('emp_model');
$data['query']=$this->emp_model->emp_getall();
$this->load->view('results',$data);
}
you wrong view call check this
<html> <head>
<title>My Form</title>
</head>
<body>
<table width="100%" border="1">
<tr>
<td>Name</td>
<td>Email</td>
<td>Mobile</td>
<td>Address</td>
<td>Action</td>
</tr>
<?php
foreach($query as $row) : ?>
<tr>
<td><?php echo $row->name;?></td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->mobile;?></td>
<td><?php echo $row->address;?></td>
<td><?php echo $row->action;?></td>
</tr>
</table>
<?php endforeach ?>
</body>
</html>
try this once .............
Related
I am new in CodeIgniter. I am using CodeIgniter for my project. I have done inserting data in database and retrieving data from the database and now I'm trying to delete row from the table in the database but I'm unable to do it.
I have tried many other way to do it but still no luck. My code is given below.
controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class School extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper(array('url','language'));
$this->load->model('Main_model');
}
public function index()
{
if($this->input->post('submit'))
{
$data=array(
'name'=> $this->input->post('name'),
'email'=> $this->input->post('email'),
'phone'=> $this->input->post('phone'));
$insert=$this->Main_model->std($data);
}
$this->data["fetch_user"]=$this->Main_model->fetch_user();
$this->load->view('form',$this->data);
}
public function delete_data()
{
$id=$this->uri->segment(3);
$this->Main_model->delete_data($id);
redirect(base_url()."deleted");
}
public function deleted()
{
$this->index();
}
}
?>
model:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main_model extends CI_Model
{
public function std($data)
{
$insert=$this->db->insert('user',$data);
return $this->db->insert_id();
}
function fetch_user()
{
$this->db->select('*');
$this->db->from('user');
$query=$this->db->get();
return $query->result();
}
function delete_data($id)
{
$this->db->where("id",$id);
$this->db->delete("user");
}
}
?>
view:
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
<style>
input[type=text],input[type=number],input[type=email]
{
height:30px;
width:250px;
outline:none;
}
input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer-spin-button
{
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
margin: 0;
}
td,th,h3
{
font-size:18px;
font-family:arial;
}
input[type="submit"]
{
padding:10px 20px;
border:none;
border-top-left-radius: 25px;
border-bottom-right-radius: 25px;
background:#7e57c2;
color:#ffffff;
outline:none;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h3>School Student data</h3>
<form method="post">
<table cellspacing="5" cellpadding="5">
<tr>
<td>Name</td>
<td><input type="text" name="name" required></td>
</tr>
<tr>
<td>Phone No.</td>
<td><input type="number" name="phone" required></td>
</tr>
<tr>
<td>E-Mail</td>
<td><input type="email" name="email" required></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Insert"></td>
</tr>
</table>
</form>
<br><br>
<table border="1" cellspacing="5" cellpadding="5" >
<tr>
<th style="padding:10px 20px;">ID</th>
<th style="padding:10px 20px;">Name</th>
<th style="padding:10px 20px;">Email</th>
<th style="padding:10px 20px;">Phone No.</th>
<th style="padding:10px 20px;">Action</th>
</tr>
<?php
if($fetch_user !=null)
{
foreach($fetch_user as $row)
{
?>
<tr>
<td><?php echo $row->id;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->phone;?></td>
<td>Delete</td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="4">sorry no data found</td>
</tr>
<?php
}
?>
</table>
<script>
$(document).ready(function(){
$('.delete_data').click(function(){
var id=$(this).attr("id");
if(confirm("are you sure you want to delete this?"))
{
window.location="<?php echo base_url(); ?>delete_data/"+id;
}
else
{
return false;
}
});
});
</script>
</body>
</html>
Instead of using jQuery for this you could have directly set the anchor tag link :
<td>Delete</td>
This will directly send you to the function delete_data of the controller.
(instead of using class and click events in jquery)
Plus return yourself from the model to the controller on success.
function delete_data($id)
{
$this->db->where("id",$id);
$this->db->delete("user");
return;//onsuccess
}
Below is how the ci controller looks like.
I'm just following tutorial from Youtube Channel.
How do you add pagination so the page only load 20 results per page ?
<?php
class Product_details extends CI_Controller{
function index(){
$this->load->library('pagination');
$this->load->model('product_model');
$data['userArray'] = $this->product_model->return_products();
$this->load->view('product_listing',$data);
}
}
View
<table>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Product Image</th>
</tr>
<?php
foreach ($userArray as $key => $value) {
echo "<tr>
<td>".$value['id']."</td>
<td>".$value['post_id']."</td>
<td>".$value['price']."</td>
<td><img src=".$value['imageUrl']."/></td>
</tr>";
}
?>
</table>
Thank you
you can pass page number in the model.
Controller
<?php
class Product_details extends CI_Controller{
function index($pageNo){
$this->load->model('product_model');
$data['userArray'] = $this->product_model->return_products($pageNo);
$this->load->view('product_listing',$data);
}
}
Model
public function all($pageNo){
$pageNo -= 1;
$this->db->select("*")
->from('products')
->order_by('id',"ASC")
->limit(20, $pageNo * 20);
$query = $this->db->get();
return $query->result_array();
See the example how to add pagination.
Let's My controller is Dashboard.php and it's method is index.
Now configure pagination.
public function index()
{
$config['base_url'] = site_url('/dashboard/index/');
$config['total_rows'] = $this->dashboard_model->num_rows();
$config['per_page'] = 5;
$config['use_page_numbers'] = FALSE;
$this->pagination->initialize($config);
$data['data'] = $this->dashboard_model->all_blog($config['per_page'], $this->uri->segment(3));
$this->load->view("/dashboard/blog", $data);
}
Model is Dashboard_model.php
public function all_blog($limit, $offset)
{
$this->db->from('blogs');
$this->db->select('*');
$this->db->order_by("created_at", "desc");
$q = $this->db->get();
$data = $q->result_array();
return $data;
}
public function num_rows()
{
$this->db->from('blogs');
$this->db->select('*');
$this->db->order_by("created_at", "desc");
$this->db->limit($limit, $offset);
$q = $this->db->get();
$data = $q->num_rows();
return $data;
}
Now it's my view blog.php
<div class="table-responsive">
<table class="footable table table-stripped toggle-arrow-tiny" data-page-size="8" data-filter=#filter; id="filter">
<thead>
<tr>
<th></th>
<th>S.No</th>
<th>Title </th>
<th>Blog Image</th>
<th>Added For</th>
</tr>
</thead>
<tbody>
<?php
$count = $this->uri->segment(3, 0);
foreach ($data as $key) :
?>
<tr>
<td><input type="checkbox" class="i-checks" name="input[]"></td>
<td><?php echo ++$count; ?></td>
<td><?php echo $key['blog_title']; ?></td>
<td><img src="<?php echo $key['blog_image']; ?>" style="width: 150px; height: 80px" /></td>
<td><?php echo $key['user_type']; ?></td>
</tr>
<?php endforeach?>
</tbody>
</table>
</div>
<?php
echo $this->pagination->create_links();
?>
I want to SAVE DATA into my sql using codeigniter, but when I click the
submit button,it shows error page cannot be found ....the view file shows but on click on submit button it shows error on next page.........
my view file
add.php
<html>
<head>
<title>Insert Employee Records in Database</title>
</head>
<body>
<form action="<?php echo base_url('emp_add/save'); ?>" method="post">
<table align="center">
<tr>
<td>First Name:</td>
<td><?php echo form_input(array('id'=>'fname', 'name'=>'fname',
'placeholder' => 'First Name', 'size'=>25));?></td>
</tr>
<tr>
<td>Last Name:</td>
<td><?php echo form_input(array('id'=>'lname', 'name'=>'lname',
'placeholder' => '
<tr>lname', 'size'=>25));?></td>
</tr>
<td>Email:</td>
<td><?php echo form_input(array('id'=>'email', 'name'=>'email',
'placeholder' => 'Email', 'size'=>25));?></td>
</tr>
<tr>
<td>Credits:</td>
<td><?php echo form_input(array('id'=>'credits',
'name'=>'credits',
'placeholder' => 'credits', 'size'=>25));?></td>
</tr>
<tr>
<td></td>
<td><button type="submit" id="submit" name="submit" >ADD</button>
</td>
</tr>
</table>
</form>
</body>
</html>
my controller file
emp_addd.php
<?php class Emp_add extends CI_Controller {
public function_construct() {
parent:: _construct();
$this->load->model('employee_model');
$this->load->helper(array('form','url'));
}
public function index()
{
$this->load->view('add'); }
public function save() {
$this->load->model('employee_model');
if($this->input->post('submit'))
{
$this->employee_model->process();
}
redirect('emp_add');
}
my model file
employee_model.php
<?php if (!defined('BASEPATH')) exit('No direct script access
allowed'); class Employee_model extends CI_Model {
public function process()
{
$save = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'credits' => $this->input->post('credits')
);
$this->db->insert('customers',$save);
}
} }
?>
SHOWING VIEW PAGE WELL. BUT ON SUBMIT IT SHOWS ERROR
I have a problem about retrieving again in another page of student information.
I can view all students using this through model and view:
public function viewdata()
{
$this->db->order_by("Idnumber", "asc");
$query= $this->db->get('studentinformation');
return $query->result();
}
<table class=" table table-hover ">
<thead>
<tr>
<th>ID Number</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Lastname</th>
<th>Sex</th>
</tr>
</thead>
<?php
foreach($this->model_adminlogin->viewdata() as $row){
?>
<tbody>
<tr>
<td><?php echo $row->Idnumber?></td>
<td><?php echo $row->Firstname?></td>
<td><?php echo $row->Middlename?></td>
<td><?php echo $row->Lastname?></td>
<td><?php echo $row->Sex ?></td>
<td>
Option
</td>
</tr>
</tbody>
<?php
}
?>
</table>
also I can view them every student information using this in model and controller and view:
function getonerow($id)
{
$this->db->where('Id',$id);
$query=$this->db->get('studentinformation');
return $query->row();
}
public function viewspecific($id)
{
if($this->session->userdata('logged_in'))
{
$this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
$this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
$this->output->set_header('Pragma: no-cache');
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$row=$this->model_adminlogin->getonerow($id);
$data['r']=$row;
$this->load->view('individualviewstudent',$data);
} else{
redirect('welcome', 'refresh');
}
if((!isset($session_data) || $session_data !=TRUE)) {
redirect('welcome', 'refresh');
}
}
and view using $r
<label class="z label-primary">Name:</label> <span class="z"> <?php echo $r -> Firstname." ". $r-> Middlename." ".$r-> Lastname; ?></span><br>
</div>
<div class="spacesz">
<label class="z label-primary">Id Number: </label><span class="z"> <?php echo $r->Idnumber; ?></span>
</div>
<div class="spacesz">
<label class="z label-primary">Age:</label><span class="z"> <?php echo $r-> Age; ?></span>
</div>
etc.......................................................................................
my problem now how can I retrieve again to another view page with that student I click for my updating purpose?? this is my image of the problem ->
retrieve in another page of that student
Use the id available in the object $r.
<?php echo site_url('student/update/'.$r->Id) ?>
Place the above code in your Update Student link. Like below:
Update Student
Hi i have this delete thing, and im using it on ajax to delete the data. My problem is it wont delete on the database, when i refresh the browser the data remains, Can someone help me out figured this thing??
Here's my controller below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class News_and_events extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('admin_model', 'am');
}
public function index(){
if($this->session->userdata('logged_in')){
$this->data['title'] = 'News and Events | Spring Rain Global Consultancy Inc Admin Panel';
$this->data['logout'] = 'Logout';
$session_data = $this->session->userdata('logged_in');
$this->data['id'] = $session_data['id'];
$this->data['username'] = $session_data['username'];
$this->data['allData'] = $this->am->getAllData();
$this->load->view('pages/admin_header', $this->data);
$this->load->view('content/news_and_events', $this->data);
$this->load->view('pages/admin_footer');
}else{
redirect('login', 'refresh');
}
}
public function add(){
if($this->session->userdata('logged_in')){
$this->form_validation->set_rules('date', 'Date', 'trim|required|xss_clean');
$this->form_validation->set_rules('event', 'Event', 'trim|required|xss_clean');
$this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
if($this->form_validation->run() == FALSE){
$this->data['title'] = 'News and Events | Spring Rain Global Consultancy Inc Admin Panel';
$this->data['logout'] = 'Logout';
$session_data = $this->session->userdata('logged_in');
$this->data['id'] = $session_data['id'];
$this->data['username'] = $session_data['username'];
$this->data['allData'] = $this->am->getAllData();
$this->load->view('pages/admin_header', $this->data);
$this->load->view('content/news_and_events', $this->data);
$this->load->view('pages/admin_footer');
}else{
$array = array(
'Date' => $this->input->post('date'),
'Event' => $this->input->post('event'),
'Description' => $this->input->post('description')
);
$this->am->saveData($array);
$this->session->set_flashdata('add_another',1);
redirect('news_and_events', 'refresh');
}
}else{
redirect('homepage', 'refresh');
}
}
public function delete(){
$id = $this->uri->segment(2);
$this->am->delete($id);
redirect(base_url().'news-and-events');
}
}
and my views
<script type="text/javascript">
$(document).ready(function(){
$("#add_another").click(function(){
});
});
function goDelete(id){
var agree = confirm("Are you sure you want to delete this?");
if(agree){
$("#news-and-event"+id).fadeOut('slow');
$.post('<?php echo base_url().'news-and-events/delete/'?>', {id:id}, function(){
});
}else{
return false;
}
}
</script>
<div class="container" >
<br />
<br />
<br />
<ul id="nav">
<li><h4>Home</h4></li>
<li><h4>News and Events</h4></li>
<li><h4>Activities</h4></li>
</ul>
<div class="starter-template">
<h1>News And Events</h1>
<?php if(!$this->session->flashdata('add_another')):?>
<form action="<?php echo base_url().'news-and-events/add'?>" method="post">
<?php echo validation_errors('<div class="error">', '</div>');?>
<table class="table-striped">
<tr>
<td>Date: </td>
<td><input type="text" id="datepicker" name="date" value="<?php echo set_value('date');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td >Event: </td>
<td ><input type="text" name="event" value="<?php echo set_value('event');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td width="20%">Description: </td>
<td><textarea cols="30" rows="5" name="description" ><?php echo set_value('description');?></textarea></td>
</tr>
<tr>
<td width="20%"> </td>
<td><input type="submit" value="Add" class="btn btn-success" /></td>
</tr>
</table>
</form>
<?php else: ?>
<div id="add_another" style="float:left;">
<input type="button" value="Add Another" class="btn btn-primary" />
</div>
<?php endif; ?>
<br />
<br />
<table class="table" >
<tr>
<th>Date</th>
<th width="47%" >Event</th>
<th width="32%">Description</th>
<th>Options</th>
</tr>
<?php foreach($allData as $x => $allDatas): ?>
<tr id="news-and-event<?php echo $allDatas->id; ?>">
<td width="10%"><?php echo $allDatas->Date; ?></td>
<td style="text-align:left;"><?php echo $allDatas->Event; ?></td>
<td style="text-align:left;"><?php echo $allDatas->Description; ?></td>
<td width="10%">
Edit |
<a href="javascript:;" onclick="return goDelete('<?php echo $allDatas->id;?>');" >Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div><!-- /.container -->
<script>
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('#datepicker').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate),
dateFormat: "yy-mm-dd"
});
</script>
and my model to delete the database
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Admin_model extends CI_Model{
public function saveData($array){
$this->db->insert('news_and_updates', $array);
}
public function getAllData(){
return $this->db->order_by("Date", "desc")
->get('news_and_updates')
->result_object();
}
public function delete($id){
$this->db->where('id', $id)->delete('news_and_updates');
}
}
?>
im using the $this->uri->segment(2); any help? is much greatly appreciated
thanks
You are sending data via POST but you are trying to delete based on the uri segment.
Try this instead in your controller:
public function delete(){
$id = $this->input->post('id');
$this->am->delete($id);
redirect(base_url().'news-and-events');
}