MySQL Table Editing w/ PHP CodeIgniter - php

I'm unsure how to get my editfields.php file to load when an administrator selects the Edit button for whatever row in my SQL table (adminlogin.php).
I feel my editfields.php file has the right stuff, but I'm stuck when it comes to getting the data and having that data display in my view as opposed to what I have printing according to my edituser function.
** PLEASE ignore the approve function for now, as well as the if conditionak in adminlogin.php, both items for separate discussion **
Below are the following files:
Controller (Myform.php)
<?php
class Myform extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('user[first_name]', 'First Name', 'required');
$this->form_validation->set_rules('user[last_name]', 'Last Name', 'required');
$this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim');
if ($this->form_validation->run() == FALSE){
$this->load->view('myform');
}
else{
$user = $this->input->post('user');
$company = $this->input->post('company');
echo '<pre>';
print_r($user);
print_r($company);
$this->db->insert("User_Inputs", $user);
$this->db->insert("companies", $company);
redirect("/myform/successmessage");
}
}
public function successmessage(){
$this->load->view('formsuccess');
}
public function getDatabase(){
$data['users'] = $this->db->get("User_Inputs")->result();
$this->load->view('adminlogin', $data);
}
public function approve(){
$id = $this->input->get("user_id");
$this->db->where("id", $id);
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h1>". $data['user']->email."</h1>";
die();
}
public function edituser(){
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h3>Showing results for ID #". $data['user']->id."</h3>";
die();
/* $data = array($id);
$this->db->where('id', $id);
$this->db->update('users', $data); */
}
}
Table View (adminlogin.php)
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php foreach($users as $u){
$class = "btn btn-primary";
$button = "Approve";
if ($u->id == 7){
$class = "btn btn-danger";
$button = "Deny";
} ?>
<tr style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="/ci/index.php/myform/edituser?user_id=<?php echo $u->id;?>">Edit</a></td>
<td><a class="<?php echo $class;?>" href="/ci/index.php/myform/approve?user_id=<?php echo $u->id;?>"><?php echo $button;?></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
Edit View (editfields.php)
<html>
<head>
<title>Edit User Info</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong><?php echo $id; ?></p>
<strong>First Name</strong><input type="text" name='first_name' value="<?php echo $first_name; ?>"?>"/><br/>
<strong>Last Name</strong><input type="text" name='last_name' value="<?php echo $last_name; ?>"/><br/>
<strong>Email</strong><input type="hidden" name='email' value="<?php echo $email; ?>"/><br/>
<input type="submit" name="submit" value="Update">
</div>
</form>
</body>
</html>
The most basic, fundamental example I want to achieve at this juncture is at the following, though this example may not fit according to my code:
http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/

I've updated my answer with the full test code for controller and views:
Myform.php
<?php
class Myform extends CI_Controller {
public function __construct()
{
parent::__construct();
// Added form and url helper so all functions can use them.
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('user[first_name]', 'First Name', 'required');
$this->form_validation->set_rules('user[last_name]', 'Last Name', 'required');
$this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim|valid_email');
// The only reason i store the values in this variable is to
// allow the re-use of the same form for both index and edituser
// functions. This allows the re-population rule to work.
$form['dbValue'] = [
'first_name' => $this->input->post('user[first_name]'),
'last_name' => $this->input->post('user[last_name]'),
'email' => $this->input->post('user[email]'),
'id' => $this->input->post('id'),
];
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform', $form);
}
else
{
// I've disabled company here as i could not find any reference to it
// in the form you have given as an example
$user = $this->input->post('user');
// $company = $this->input->post('company');
echo '<pre>';
print_r($user);
// print_r($company);
if ($this->input->post('id') !== '' && $this->input->post('id') !==NULL)
{
// id is defined so we update the user
$this->db
->where('id', (int) $this->input->post('id'))
->update("User_Inputs", $user);
}
else
{
// id is not define so we create the user
$this->db->insert("User_Inputs", $user);
}
// $this->db->insert("companies", $company);
redirect("/myform/getDatabase");
}
}
public function successmessage()
{
$this->load->view('formsuccess');
}
public function getDatabase()
{
$data['users'] = $this->db->get("User_Inputs")->result();
$this->load->view('adminlogin', $data);
}
public function approve()
{
$id = $this->input->get("user_id");
$this->db->where("id", $id);
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h1>" . $data['user']->email . "</h1>";
die();
}
public function edituser($user_id = NULL)
{
// Just making sure we are capturing the id as an integer
$user_id = (int) $user_id;
// Get the user based on the $user_id
$data['user'] = $this->db
->where('id', $user_id)
->get("User_Inputs")
->row();
// We will use the below variable to set the form fields to the correct values.
$form['dbValue'] = [
'first_name' => $data['user']->first_name,
'last_name' => $data['user']->last_name,
'email' => $data['user']->email,
'id' => $user_id
];
$this->load->view('myform', $form);
}
}
View: adminlogin.php
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php
foreach ($users as $u)
{
$class = "btn btn-primary";
$button = "Approve";
if ($u->id == 7)
{
$class = "btn btn-danger";
$button = "Deny";
}
?>
<tr style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
<td><a class="<?php echo $class; ?>" href="<?php echo site_url('myform/approve/' . $u->id); ?>"><?php echo $button; ?></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
View: myform.php
<?php
// if the set_value default values are not set set them to NULL
echo form_open('myform');
echo form_label('First Name', 'first_name');
echo form_input('user[first_name]', set_value('user[first_name]', isset($dbValue['first_name'])) ? $dbValue['first_name'] : '' );
echo form_label('Last Name', 'last_name');
echo form_input('user[last_name]', set_value('user[last_name]', isset($dbValue['last_name'])) ? $dbValue['last_name'] : '' );
echo form_label('Email', 'email');
echo form_input('user[email]', set_value('user[email]', isset($dbValue['email'])) ? $dbValue['email'] : '' );
echo form_hidden('id', set_value('id', isset($dbValue['id'])) ? $dbValue['id'] : '' );
echo form_submit('Submit', 'submit');
echo form_close();
echo validation_errors();

OK, for ajax see the below modifications:
I've added jquery library and bootstrap library,
adminlogin.php
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php foreach ($users as $u): ?>
<?php
// added approve class
$class = "approve btn btn-primary";
$button = "Approve";
if ($u->id == 7)
{
$class = " deny btn btn-danger";
$button = "Deny";
}
?>
<!-- id is added to each row to uniquely identify them so we can change it's color-->
<tr id="row_<?= $u->id ?>"style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
<!-- data-id was added to store the user id -->
<td><a class="<?php echo $class; ?>" data-id="<?= $u->id ?>" href="<?php echo site_url('myform/approve/' . $u->id); ?>"><?php echo $button; ?></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<script>
$('.approve').on("click", function (e) {
// prevent the browser to loading the page directly
e.preventDefault();
// get the user id from the button we clicked.
var userid = $(this).data('id');
//initiate ajax request
$.ajax({
url: $(this).attr('href'),
method: 'POST',
success: function (data) {
// on success add the bootstrap success class to the row
$('#row_' + userid).addClass('success')
},
error: function (data) {
// on error add boorstrap dange class to the row
$('#row_' + userid).addClass('danger')
}
});
});
</script>
</body>
</html>
for testing purposes i've changed the myform/approve function
public function approve()
{
//do something this is for testing
$result = true;
if ($result)
{
// success you have done was OK and we send back success code
$this->output->set_status_header(200);
} else
{
// failure we send back an 4* cient error code
$this->output->set_status_header(400);
}
}

Related

I need to use the data on another page

I want to load another view using the function within the same controller. I'm new to codeIgniter so Please go easy on me :D
loaded home page by default -> From Home page filled in fields -> submit -> using ajax grabbed data from database -> use data on another page by loading the new view in the same controller as the default.
Controller:
<?php
class logInCon extends CI_Controller
{
public function login()
{
//$data['creds'] = $this->accsModel->getUser();
$this->load->view("Login");
}
public function validate_LogIn()
{
$uname = $this->input->post('uname');
$pass = $this->input->post('pass');
$this->load->model("accsModel");
$data = $this->accsModel->logInCheck($uname, $pass);
$check = $data['verified'];
if ($check <= 0)
{
echo "Not a valid member";
}else
{
$data['logged'] = $this->accsModel->getUser($uname, $pass);
$this->load->view('comms.php');//want this to load
}
}
}
?>
Model
<?php
/**
*/
class accsModel extends CI_Model
{
public function getBps()
{
$query = $this->db->get('bps');
return $query->result();
}
public function getUser($uname, $psswrd)
{
$getCreds = $this->db->query("SELECT `bps`.*, `users`.bps_id
FROM `bps`
LEFT JOIN `users`
ON `bps`.id = `users`.bps_id
AND `users`.`uname` = '$uname'
AND `users`.`pwd` ='$psswrd'
WHERE `users`.bps_id != ''
OR `users`.bps_id IS NOT NULL");
return $getCreds->result();
}
public function logInCheck($uname, $psswrd)
{
$this->db->select('COUNT(*) AS verified');
$this->db->where('uname', $uname);
$this->db->where('pwd', $psswrd);
$this->db->limit(1);
return $this->db->get('users')->row_array();
}
}
?>
View
<body>
<div class="container">
<div class="row">
<div class="LogForm">
<div class="col-md-12">
<input type="text" id="bpCode" name="bpCode">
</div>
<div class="col-md-12">
<input type="password" id="pass" name="pass">
</div>
<div class="btn">
<span id="emptyF" hidden style="color:red;"></span>
<button id="submit" class="btn btn-info btn-lg btn-block">Login</button>
<span id="result"></span>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function()
{
$("#submit").click(function()
{
var uname = $('#bpCode').val();
var pass = $('#pass').val();
if(uname == "" || pass =="")
{
$("#emptyF").attr("hidden", false).html("Please fill in the form");
}else{
$("#emptyF").attr("hidden", true);
$.ajax({//start of ajax function
type: "POST",
url: "<?php echo base_url('logInCon/validate_LogIn'); ?>",
data:
{
uname : uname,
pass : pass
}
//end of ajax function
});
}
});
});
</script>
</body>
View comms want this to be loaded and use the the data from the getUser() function
<body>
<p><?php print_r($logged) ?></p>
<h1>Commissions View</h1>
<table style="width:100%">
<?php
foreach ($logged as $lgdIn) {
?>
<tr>
<th>ID</th>
<th>bpID</th>
<th>pc</th>
<th>up</th>
<th>tmLdr</th>
<th>fName</th>
<th>mName</th>
<th>lName</th>
<th>contactNo</th>
<th>Status-Id</th>
<th>bpclass_id</th>
</tr>
<tr>
<td><?php echo $lgdIn->id; ?></td>
<td><?php echo $lgdIn->bpID; ?></td>
<td><?php echo $lgdIn->pc; ?></td>
<td><?php echo $lgdIn->up; ?></td>
<td><?php echo $lgdIn->tmLdr; ?></td>
<td><?php echo $lgdIn->fName; ?></td>
<td><?php echo $lgdIn->mName; ?></td>
<td><?php echo $lgdIn->lName; ?></td>
<td><?php echo $lgdIn->contactNo; ?></td>
<td><?php echo $lgdIn->status_id; ?></td>
<td><?php echo $lgdIn->bpclass_id; ?></td>
</tr>
<?php
}
?>
</table>
</body>
You need to pass $data in load view just like below code
public function validate_LogIn()
{
$uname = $this->input->post('uname');
$pass = $this->input->post('pass');
$this->load->model("accsModel");
$data = $this->accsModel->logInCheck($uname, $pass);
$check = $data['verified'];
if ($check <= 0)
{
echo "Not a valid member";
}else{
$data['logged'] = $this->accsModel->getUser($uname, $pass);
$this->load->view('comms',$data);//want this to load
}
}
little change in view too
<body>
<p><?php print_r($logged) ?></p>
<h1>Commissions View</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>bpID</th>
<th>pc</th>
<th>up</th>
<th>tmLdr</th>
<th>fName</th>
<th>mName</th>
<th>lName</th>
<th>contactNo</th>
<th>Status-Id</th>
<th>bpclass_id</th>
</tr>
<?php if(!empty($logged)) { foreach ($logged as $lgdIn) { ?>
<tr>
<td><?php echo $lgdIn->id; ?></td>
<td><?php echo $lgdIn->bpID; ?></td>
<td><?php echo $lgdIn->pc; ?></td>
<td><?php echo $lgdIn->up; ?></td>
<td><?php echo $lgdIn->tmLdr; ?></td>
<td><?php echo $lgdIn->fName; ?></td>
<td><?php echo $lgdIn->mName; ?></td>
<td><?php echo $lgdIn->lName; ?></td>
<td><?php echo $lgdIn->contactNo; ?></td>
<td><?php echo $lgdIn->status_id; ?></td>
<td><?php echo $lgdIn->bpclass_id; ?></td>
</tr>
<?php } } else { ?>
<tr colspan="11"><td>No record found.</td></tr>
<?php } ?>
</table>
</body>

PHP codeigniter - how to return to DB table after updating row

I want my admin table view to reload after uses selects Edit, changes entry and selects Update Entry with the edits reflected in the revised table row.
The admin edit page is working fine, and I know the solve for redirecging the update to the table page is basic. Some light guidance would be much appreciated!
Admin View:
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php
foreach($users as $u)
{
$class = "btn btn-primary";
$button = "Approve";
?>
<tr style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
<td><a class="<?php echo $class;?>" href="/ci/index.php/myform/approve/?user_id=<?php echo $u->id;?>"><?php echo $button;?></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
Admin Edit View:
<html>
<head>
<title>Edit User Info</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<div class="jumbotron">
<form action="" method="post">
<p><font color="black" font size='5'/><strong>Entry for ID </strong><strong>"<?php echo $id; ?>"</strong></font></p>
<font color="black" font size='5'/><strong>First Name </strong></font><input type="text" name='first_name' value="<?php echo $first_name; ?>"?><br/>
<font color="black" font size='5'/><strong>Last Name </strong></font><input type="text" name='last_name' value="<?php echo $last_name; ?>"/><br/>
<font color="black" font size='5'/><strong>Email </strong></font><input type="text" name='email' value="<?php echo $email; ?>"/><br/>
<br/>
<input type="button" class="btn btn-info" value="Update Entry">
</div>
</div>
</form>
</body>
</html>
Controller:
<?php
class Myform extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('user[first_name]', 'First Name', 'required');
$this->form_validation->set_rules('user[last_name]', 'Last Name', 'required');
$this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim');
$form['userValues'] = [
'first_name' => $this->input->post('user[first_name]'),
'last_name' => $this->input->post('user[last_name]'),
'email' => $this->input->post('user[email]'),
'id' => $this->input->post('id'),
];
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$user = $this->input->post('user');
//$company = $this->input->post('company');
echo '<pre>';
print_r($user);
//print_r($company);
if ($this->input->post('id') !== '' && $this->input->post('id') !== NULL)
{
$this->db->where('id', (int) $this->input->post('id'));
$this->db->update("User_Inputs", $user);
}
else
{
$this->db->insert("User_Inputs", $user);
}
//$this->db->insert("companies", $company);
redirect("/myform/successmessage");
}
}
public function successmessage(){
$this->load->view('formsuccess');
}
public function getDatabase(){
$data['users'] = $this->db->get("User_Inputs")->result();
$this->load->view('adminlogin', $data);
}
public function approve(){
$id = $this->input->get("user_id");
$this->db->where("id", $id);
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h1>". $data['user']->email."</h1>";
die();
}
public function edituser($user_id = NULL)
{
$user_id = (int) $user_id;
$data['user'] = $this->db
->where('id', $user_id)
->get("User_Inputs")
->row();
$form['dbValue'] = [
'first_name' => $data['user']->first_name,
'last_name' => $data['user']->last_name,
'email' => $data['user']->email,
'id' => $user_id
];
$this->load->view('editfields', $data['user']);
}
}

Button in view doesn't call method in controller

i have a problem : I have a view that needs to call a method in my controller EmployeeController to go to the page to add an employee. But it don't work.. also with other views it doe
This is the view:
<body>
<form class="form-horizontal" id='employeeform' id='employeeform' action="<?php echo base_url();?>EmployeeController/addEmployee" method="post">
<div class="container">
<div class="row">
<div class="col-lg-12 col-sm-12">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>ID</th>
<th>Naam werknemer</th>
<th>Datum aanwerving</th>
<th>Salaris</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($employeelist); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $employeelist[$i]->employee_no; ?></td>
<td><?php echo $employeelist[$i]->employee_name; ?></td>
<td><?php echo $employeelist[$i]->hired_date; ?></td>
<td><?php echo $employeelist[$i]->salary; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Add employee" />
</div>
</div>
</div>
</form>
</body>
THIS IS THE CONTROLLER:
class EmployeeController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->database();
$this->load->library('form_validation');
//load the employee model
$this->load->model('employee_model');
}
//index function
function index() {
$employeelist = $this->employee_model->get_employee_list();
$data['employeelist'] = $employeelist;
$this->load->view('employee/employee_add', $data);
}
function createEmployee() {
$this->form_validation->set_rules('employee_no', 'Employee No', 'trim|required|numeric');
$this->form_validation->set_rules('employee_name', 'Employee Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('hired_date', 'Hired Date', 'required');
$this->form_validation->set_rules('salary', 'Salary', 'required|numeric');
if ($this->form_validation->run() == FALSE) {
//fail validation
$this->load->view('employee/employee_add');
} else {
//pass validation
$data = array(
'employee_no' => $this->input->post('employeeno'),
'employee_name' => $this->input->post('employeename'),
'hired_date' => #date('Y-m-d', #strtotime($this->input->post('hireddate'))),
'salary' => $this->input->post('salary'),
);
//insert the form data into database
$this->db->insert('tbl_employee', $data);
//display success message
$employeeresult = $this->employee_model->get_employee_list();
$data['employeelist'] = $employeeresult;
//load the department_view
$this->load->view('employee/employee_view', $data);
redirect('employee/employee_view');
}
}
function addEmployee() {
$this->load->view('employee/employee_add');
//set validation rules
}
}
?>
THE ERROR I GET IS :
The requested URL /BoMaTec_afstudeerproject/CodeIgniter-3.0.1/EmployeeController/createEmployee was not found on this server.
Seems like you don't have any router for that url, or the router is wrong. As per the error, it's trying to load a folder named EmployeeController and within that folder, another folder named createEmployee. Check your .htaccess file and your routers in the framework you are using.

Delete multiple image from database also database using codeigniter

I just created code that deletes an image from database and also from folder which is an image store, but only one image is successfully deleted from thedatabase and folder when I click "check all". What did I do wrong? Here is my view using check box javascript:
<form name="indonesia" action="<?php echo site_url('admin/wallpaper/delete'); ?>" method="post">
<button type="submit" class="btn btn-danger" name="hapus" value="hapus">Hapus</button>
<?php echo anchor('admin/wallpaper/tambah', 'Tambah Wallpaper');?>
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>
<button type="button" class="btn btn-info" onClick="check_all()" >Check</button>
<button type="button" class="btn btn-success" onClick="uncheck_all()" >Un-Check</button>
</th>
<th>id</th>
<th>Keterangan</th>
<th>Gambar</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
foreach ($ListWallpaper->result() as $row)
{
?>
<tr>
<td><input type="checkbox" name="item[]" id="item[]" value="<?=$row->id_wall ?>"></td>
<td><?=$row->id_wall ?></td>
<td><?=$row->ket ?></td>
<td><?=$row->wall ?></td>
<td>
Delete
Update
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
and here is my controller
public function delete()
{
$ownerNames = $this->input->post('item');
foreach ($ownerNames as $ownerName => $k) {
//echo "Array : " . $k . "<br/>";
$photo = $this->wallpaper_model->del_photo($k);
if ($photo->num_rows() > 0)
{
$row = $photo->row();
$file_photo = $row->wall;
echo "$file_name</br>";
$path_file = 'image/wallpaper/';
unlink($path_file.$file_photo);
}
$this->wallpaper_model->drop_photo($k);
redirect('admin/wallpaper','refresh');
}
}
and my model
function del_photo($k)
{
$this->db->where('id_wall',$k);
$query = $getData = $this->db->get('tabel_wall');
if($getData->num_rows() > 0)
return $query;
else
return null;
}
function drop_photo($k)
{
$this->db->where('id_wall',$k);
$this->db->delete('tabel_wall');
}
Only one image is successfully deleted from the folder and database too, but when I try to echo "$file_name</br>"; it show all images. What did I do wrong? if anyone can guide me, I will appreciate that.
First off I would set a array of images from controller to view, $data['wallpapers'] = array(); for some of the site_url you may need to include in your route.php $route['controller/update/(:any)'] = "controller/update/$1"
To Delete selected images, You could do a selected post in array() and then on the value check box name=""
Disclaimer: This is just for a example only.
public function index() {
$k = $this->uri->segment(what ever); // Use uri segment is id example.com/image/1 = $this->uri->segment(2);
$results = $this->model_name->del_photo($k);
$data['wallpapers'] = array();
foreach ($results as $result) {
$data['wallpapers'][] = array(
'id_wall' => $result['id_wall'],
'update' => site_url('controllername/update' .'/'. $result['wall_id']),
'ket' => $result['ket']
);
}
$data['delete'] = site_url('controller/function');
$selected = $this->input->post('selected');
if (isset($selected)) {
$data['selected'] = (array)$selected;
} else {
$data['selected'] = array();
}
$this->load->view('your list', $data);
}
public function update() {
//update info here
}
public function delete() {
$selected = $this->input->post('selected');
if (isset($selected)) {
foreach ($selected as $image_id) {
$wall_id = $image_id
$this->db->query("DELETE FROM " . $this->db->dbprfix . "TABLENAME WHERE wall_id = '" . (int)$wall_id . "'");
}
}
}
View
Added echo delete from controller as site url.
<form action="<?php echo $delete;?>" method="post">
<table>
<thead>
<tr>
<td style="width: 1px;" class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td>
<td>Update</td>
</tr>
</thead>
<tbody>
<?php if ($wallpapers) { ?>
<?php foreach ($wallpapers as $wallpaper) { ?>
<td class="text-center"><?php if (in_array($wallpaper['id_wall'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $wallpaper['id_wall']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $wallpaper['id_wall']; ?>" />
<?php } ?>
</td>
<td>Update</td>
<?php } ?>
<?php } ?>
</tbody>
</table>
</form>
Model
function del_photo($k) {
$this->db->where('id_wall',$k);
$query = $this->db->get('table_name');
if($query->num_rows() > 0) {
$return = $query->result_array();
} else {
return false;
}
}
In controller, move redirect directive outside of foreach loop.

update record database in CodeIgniter

I already add,view,delete record but I can't update record in database. however when i try update name,alamat,etc is unchanged in database. I have listed my controller , model and view below:
model code
function update($id,$siswa)
{
$this->db->where($this->primary_key,$id);
$this->db->update($this->table_name,$siswa);
}
controller code
function update($id=1)
{
$this->_set_fields();
$siswa = $this->siswa_model->get_by_id($id)->row();
$this->form_validation->id = $id;
$this->form_validation->nama = $siswa->nama;
if (isset($_POST['jenis_kelamin'])) {
$_POST['jenis_kelamin'] = strtoupper($data['siswa']['jenis_kelamin']);
}
if (isset($_POST['tanggal_lahir'])) {
$data['siswa']['tanggal_lahir'] = date('d-m-Y',strtotime($data['siswa']['tanggal_lahir']));
}
$data['title'] = 'Update siswa';
$data['message'] = '';
$data['action'] = site_url('siswa/updateSiswa');
$data['link_back']= anchor('siswa/index/','Back to list of siswas',array('class'=>'back'));
$this->load->view('siswaEdit',$data);
}
function updateSiswa()
{
//set common properties
$data['title'] = 'Update siswa';
$data['action'] = site_url('siswa/updateSiswa');
$data['link_back']= anchor('siswa/index/','Back to list of siswas',array('class'=>'back'));
//$this->load->library('form_validation');
//set validation properties
$this->_set_fields();
$this->_set_rules();
//$data['action']=('siswa/update/'.$id);
//run validation
if ($this->form_validation->run() === FALSE) {
$data['message']='';
$data['siswa'] = $this->siswa_model->get_by_id($id)->row_array();
if (isset($_POST['jenis_kelamin'])) {
$_POST['jenis_kelamin'] = strtoupper($data['siswa']['jenis_kelamin']);
}
if (isset($_POST['tanggal_lahir'])) {
$data['siswa']['tanggal_lahir'] = date('d-m-Y',strtotime($data['siswa']['tanggal_lahir']));
}
//set common properties
$data['title']='Update siswa';
$data['message']='';
}
else
{
//save data
$id = $this->input->post('id');
$siswa=array( 'id'=>$id,
'nama'=>$this->input->post('nama'),
'alamat'=>$this->input->post('alamat'),
'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
'tanggal_lahir'=>date('Y-m-d', strtotime($this->input->post('tanggal_lahir'))));
$this->siswa_model->update($id,$siswa);
$data['siswa'] = $this->siswa_model->get_by_id($id)->row_array();
//set user message;
$data['message']='update siswa success';
}
$data['link_back']= anchor('siswa/index/','Lihat Daftar Siswa',array('class'=>'back'));
//load view
$this->load->view('siswaEdit',$data);
}
view code
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SIMPLE CRUD APPLICATION</title>
<link href="<?php echo base_url();?>style/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="content">
<h1><?php echo $title;?></h1>
<div class="data">
<table>
<tr>
<td width="30%">ID</td>
<td><?php
if (isset($siswa->id)) {
echo $siswa->id;}
;?>
</td>
</tr>
<tr>
<td valign="top">Name</td>
<<td><?php
if (isset($siswa->nama)) {
echo $siswa->nama;}
;?>
</td>
</tr>
<tr>
<td valign="top">Alamat</td>
<td><?php
if (isset($siswa->alamat)) {
echo $siswa->alamat;}
;?>
</td>
</tr>
<tr>
<td valign="top">Jenis Kelamin</td>
<td><?php if (isset($siswa->jenis_kelamin)){
echo ($siswa->jenis_kelamin) =='M'?'Laki-Laki':'Perempuan';}
;?>
</td>
</tr>
<tr>
<td valign="top">Tanggal Lahir (dd-mm-yyyy)</td>
<td><?php if (isset($siswa->jenis_kelamin)){
echo date('d-m-Y',strtotime($siswa->tanggal_lahir));}
;?>
</td>
</tr>
</table>
</div>
<br/>
<?php echo $link_back;?>
</div>
</body>
</html>
can you help me find problem solving from my program?
I think the view page that you have shown above, it just list the record. you can list it in form to submit the data.

Categories