Hi I am creating a view where I will be displaying the list of records from database along with boxes where a user selects checkboxes and click on Activate button. In the database I have given a separate column for featured blogs once user click on activate, these id columns should be updated as 1 and rest will be as 0.
View:
<table>
<thead>
<tr>
<th scope="col">Featured Blogs</th>
<th scope="col">S.No</th>
<th scope="col">Blog Title</th>
<th scope="col">Author</th>
</tr>
</thead>
<tbody>
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php $i=0;foreach($records as $r):$i++;?>
<tr>
<td><input type="checkbox" name="checkbox" value="<?php echo $r->blog_id;?>"
<?php if ($r->featured_blogs == 1) { echo 'checked="checked"'; }else { echo 'disabled="disabled"'; }?> ></td>
<td class="align-center"><?php echo $i;?></td>
<td><?php echo $r->blog_title;?></td>
<td><?php echo $r->username;?></td>
</tr>
<?php endforeach ;endif;?>
</tbody>
</table>
<div class="pagination"><?php echo $links; ?></div>
Activate
</div>
</div>
Controller:
function active()
{
$this->blogs_model->active($this->uri->segment(3));
$this->flash->success('<h2>Successfully Activated the record.<h2>');
redirect('blogs');
}
Model:
function activate($blog_id)
{
$data=array('status'=>1);
$this->db->where(array('blog_id'=>$blog_id));
$this->db->update('blogs',$data);
}
function active()
{
$this->blogs_model->active($this->uri->segment(3));
$this->flash->success('<h2>Successfully Activated the record.<h2>');
redirect('blogs');
}
function active($blog_id)
{
$data=array('status'=>1);
$this->db->where(array('blog_id'=>$blog_id));
$this->db->update('blogs',$data);
}
on your controller you called the model and it's function as active($this->uri->segment(3))
but in your model you called it as function activate($blog_id)
you should need to change it
Related
I've created a notification script and I have a page called notificationCenter.php where the admin user can view ALL the notifications. It will display which users have read which notifications, when it was posted and who by. Everything works fine with it, the only issue is when I go onto the notificationCenter.php page to view all notifications, the table doesn't display the results. However I run an if statement to check if there are any results and I don't get any errors. Here's my code, I'm sure I've done something stupid but I can't see what it is, I'd really appreciate any help!.
//Get User ID
$id = $_SESSION['user_id'];
//Select * Notifications
$notQuery = $serviceConn->query("SELECT * FROM db759709251.notifications WHERE `not_viewedby` NOT LIKE '%$id%' ");
<h4>Nofication Center</h4>
<?php if($notQuery->rowCount()) { ?>
<table>
<thead>
<tr>
<th scope="col">Status</th>
<th scope="col">User</th>
<th scope="col">Notification</th>
<th scope="col">Date</th>
<th scope="col">Viewed By</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php
while ($row = $notQuery->fetch()) {
$notid = $row['not_id'];
$notUser = $row['not_user'];
$notMsg = $row['not_msg'];
$notStatus = $row['not_status'];
$notDate = $row['not_date'];
$notViewedby = $row['not_viewedby'];
?>
<tr>
<td style="background-color: <?php echo $statusColour; ?>" data-label="Status"><?php echo $notStatus; ?></td>
<td data-label="User"><?php echo $notUser; ?></td>
<td data-label="Notification"><?php echo $notMsg; ?></td>
<td data-label="Date"><?php echo $notDate; ?></td>
<td data-label="Viewed By"><?php echo $notViewedby; ?></td>
<td data-label="">
<form action="" method="POST">
<input type="hidden" name="notificationID" value="<?php echo $notid; ?>" >
<input type="hidden" name="notificationBy" value="<?php echo $notViewedby; ?>">
<input type="submit" name="read" value="Dismiss!">
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php } else { ?>
<p>You currently have no notifications, please check back later.</p>
<?php } ?>
Now obviously there is a lot of HTML in the middle for styling and layouts, but I didn't think it was important enough as it shouldn't have any effect on the output of the table. But as you can see the if statement SHOULD print that there are no notifications and to check back later, and it doesn't so in theory there should be records to display.
I am developing an attendance management codeigniter app and I am using switchery switch plugin to update attendance status. The process will be:
System display the list of student with attendance status in checkbox with switchery plugin.
Using check or uncheck the checkbox with switchery plugin, if the student is present or absent or simply user change the status.
System sends the form using AJAX, if the student present 1 or absent 0, with student ID and attendance date.
And simply system will change the attendance status of student.
NOTE* THE VALUE OF $date $month $year $program_id $batch_id and $section_id is loaded before in controller function parameter.
And this my app's preview. I need to this form working using AJAX.
And this is my code for the view:
<!-- student attendence status update form -->
<form method="post" action="<?php echo base_url(); ?>index.php?admin/student_attendance">
<!-- start datatable -->
<table class="table table-hover dataTable table-striped width-full" id="departmentTableTools">
<thead>
<tr>
<th class="text-center"><?php echo get_phrase('rool'); ?></th>
<th class="text-center"><?php echo get_phrase('name'); ?></th>
<th class="text-center"><?php echo get_phrase('status'); ?></th>
</tr>
</thead>
<tbody>
<?php
foreach($students as $row):?>
<tr>
<td class="text-center"><?php echo $row['roll_no'];?></td>
<td class="text-center"><?php echo $row['name'];?></td>
<td class="text-center">
<input type="checkbox" value="1" name="status_<?php echo $row['student_id'];?>" data-plugin="switchery" <?php if($status == 1) echo 'checked'; ?>/>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<input type="hidden" name="date" value="<?php echo $full_date;?>" />
</form>
<!-- end the attendance status update form -->
And this my controller code:
if($_POST) // if the update request is send
{
foreach ($students as $row)
{
$this->crud_model->update_student_attendance($students['student_id'], $this->input->post('status_' . $row['student_id']), $this->input->post('date'));
}
$this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
redirect(base_url() . 'index.php?admin/student_attendance/'.$date.'/'.$month.'/'.$year.'/'.$program_id.'/'.$batch_id , 'refresh');
}
And this is my model code to update and db values:
function update_student_attendance($student_id, $attendance_status, $full_date){
$this->db->where('student_id' , $student_id);
$this->db->where('date' , $full_date);
$this->db->update('attendance' , array('status' => $attendance_status));
}
I wanted to be able to update and delete items for a list of products from an sql database. I already have the table from a database listed and I am able to read and insert a new product but I'm not sure how to update the product or delete it? Any advice or tutorials would be good? I have tried a few but none seem to work.
Here is my code for showing the products and inserting a new product
Product.php - controller
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('product_model');
}
public function index()
{
$data['product_list'] = $this->product_model->getproduct();
$this->load->view('header');
$this->load->view('nav');
$this->load->view('product', $data);
$this->load->view('footer');
}
public function add_form()
{
$this->load->view('header');
$this->load->view('nav');
$this->load->view('insert');
$this->load->view('footer');
}
public function insert_product()
{
$pdata['Name'] = $this->input->post('Name');
$pdata['Type'] = $this->input->post('Type');
$pdata['Price'] = $this->input->post('Price');
$res = $this->product_model->insert_product($pdata);
if($res){
header('location:'.base_url()."index.php/Product/index");
}
}
}
?>
Product_model.php
<?php
class Product_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function getproduct()
{
$query = $this->db->get('testProduct');
return $query->result();
}
public function insert_product($data)
{
return $this->db->insert('testProduct', $data);
}
}
?>
product.php - View
<h2> Product</h2>
<table width="600" border="1" cellpadding="5">
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Type</th>
<th scope="col">Price</th>
</tr>
<?php foreach ($product_list as $p_key){ ?>
<tr>
<td><?php echo $p_key->id; ?></td>
<td><?php echo $p_key->Name; ?></td>
<td><?php echo $p_key->Type; ?></td>
<td><?php echo $p_key->Price; ?></td>
<td width="40" align="left" ><a href="#" <?php echo $p_key->id;?> >Edit</a></td>
<td width="40" align="left" ><a href="#" <?php echo $p_key->id;?>>Delete </a></td>
</tr>
<?php }?>
<tr>
<td colspan="7" align="right"> Insert New Product</td>
</tr>
</table>
What you are making is called CRUD - create, read, update, delete. There are a lot of examples of doing CRUD with CodeIgniter in Google.
Below are (over)simplified conceptual samples.
Based on your HTML, adding links to edit and delete a product:
<td width="40" align="left">
Edit
<!-- Don't really delete like this, seciruty issue! Just shown as a concept for manipulating records with IDs -->
Delete
</td>
And in Product.php - controller we'll add new methods to edit and delete
public function edit($id)
{
if($_SERVER['REQUEST_METHOD'] == "POST")
{
// get fields from form and update
// database record with them
$pdata['Name'] = $this->input->post('Name');
$pdata['Type'] = $this->input->post('Type');
$pdata['Price'] = $this->input->post('Price');
$this->product_model->update_product($id, $pdata);
}
else
{
// show form to edit product
// need to get product from database and pass to a view
$product = $this->product_model->getproduct_by_id($id);
$this->load->view('header');
$this->load->view('nav');
$this->load->view('edit', array("product" => $product));
$this->load->view('footer');
}
}
public function delete($id)
{
$this->product_model->delete($id);
}
edit.php - View
<form action="/index.php/product/edit/<?php echo $product->id ?>" method="post">
<table>
<tr>
<td><input name="name" value="<?php echo $product->Name; ?>"></td>
</tr>
<tr>
<td><input name="type" value="<?php echo $product->Type; ?>"></td>
</tr>
<tr>
<td><input name="price" value="<?php echo $product->Price; ?>"></td>
</tr>
<tr>
<td>
<input type="submit" value="Update">
</td>
</tr>
</table>
</form>
product_model.php
public function update_product($id, $pdata)
{
$this->db->where("product_id", $id);
$this->db->update("product", $pdata);
}
i want 14 records to be uploaded. Before uploading i want to select corresponding check button. At a time only one record can be uploaded. So all 14 records are arranged in a table with two buttons, one for choose file and other for submit. But problem is that only very first document is uploaded, others don't get uploaded. later i knew that i must use more than one form for all submission. how can it possible?
<div class="portal-body" style="min-height: 268px;">
<table class="table striped hover bordered" id="sample_editable_4">
<thead>
<tr>
<th id="eval_style">Sl No.</th>
<th id="eval_style">Document Name</th>
<th id="eval_style">select</th>
<th id="eval_style">Choose</th>
<th id="eval_style">Submit</th>
</tr>
</thead>
<tbody>
<?php $i=1;
foreach($evidence_details->result() as $row_evidence) { ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row_evidence->evidence_name; ?> </td>
<td>
<input type="checkbox" name="evidences" id="<?php echo $row_evidence->evidence_id; ?>" value="<?php echo $row_evidence->evidence_id; ?>" <?php if(in_array($row_evidence->evidence_id,$evidence)) { ?>checked="checked"<?php } ?> />
</td>
<td><input type="file" name="multiUpload" id="multiUpload" multiple /></td>
<td><button type="submit" class="btn blue" name="submitHandler" id="submitHandler" style="margin-top: 15px;margin-left: 54px;">Submit</button></td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
</div>
<script type="text/javascript">
var config = {
support : "image/jpg,image/png,image/bmp,image/jpeg,image/gif,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv,application/pdf",
form: "demoFiler", // Form ID
dragArea: "dragAndDropFiles",
uploadUrl: "<?php echo base_url(); ?>home/multiple_upload"
}
$(document).ready(function()
{
initMultiUploader(config);
});
</script>
controller
function multiple_upload()
{
$application_id=$this->session->userdata('application_id');
if(!is_dir('./application/assets/uploads/'.$application_id))
{
mkdir('./application/assets/uploads/'.$application_id, 0777, TRUE);
}
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(move_uploaded_file($_FILES['file']['tmp_name'], "./application/assets/uploads/".$application_id."/".$_FILES['file']['name'])){
echo($_POST['index']);
}
exit;
}
}
**multiupload.js**
function initMultiUploader(){
new multiUploader(config);
}
Here's the issue : CodeIgniter loads data from the controller but not from the view
When I go the the source of the page and click the form action link its load data in the source code but not in the html view.
--view
<table class="table hovered">
<thead>
<tr>
<th class="text-left">User ID</th>
<th class="text-left">Type</th>
<th class="text-left">Name</th>
<th class="text-left">Email</th>
<th class="text-left">Phone</th>
</tr>
</thead>
<tbody>
<?php echo form_open('admin/manage_customer') ?><!-- start of the form -->
<?php if(isset($records)) : foreach($records as $row) : ?>
<tr>
<td class="right"><?php echo $row->cus_id; ?></td>
<td class="right">Admin</td>
<td class="right"><?php echo $row->cus_name; ?></td>
<td class="right"><?php echo $row->cus_email; ?></td>
<td class="right"><?php echo $row->cus_phone; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php echo form_close(); ?><!-- END of the form -->
</tbody>
<tfoot></tfoot>
</table>
---controller
function inbox()
{
$data = array();
if($query = $this->mod_contactus->get_records())
{
$data['records'] = $query;
}
$this->load->view('admin/admin_messages',$data);
}
---Model
<?php
class Mod_contactus extends CI_Model //Users (model name) share all the class and functions CodeIgniter models have
{
function get_records()
{
$query = $this->db->get('tbl_contactus');
return $query->result();
}
function add_record($data)
{
$this->db->insert('tbl_contactus', $data);
return;
}
function update_record()
{
}
}
i would go with hussain in this. first check if the necessary data is returned by the model or not. Only then you should check the html structure. But even if there is a problem with structure, something should be displayed. So, check the flow of data to the view first.