I don't know how to do a edit and delete function in codeigniter.. I really need it for the admin side of my system.. I really need help.. I want to delete the id in my table named user_acc and edit the whole column
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mod_admin extends CI_Model{
function __construct()
{
parent::__construct();
}
public function user_acc(){
$this->db->select('*');
$this->db->from('user_acc');
$query = $this->db->get();
return $query->result();
}
public function delete()
{
$this->db->where('id');
$this->db->delete();
}
}
Admin:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->Model('Mod_admin');
}
public function manageuser(){
$this->load->view('template/adminhead');
$data['user_acc'] = $this->Mod_admin->user_acc();
$this->load->view('manageuser', $data);
}
public function delete_product() {
$this->Mod_admin->delete();
$this->session->set_flashdata('message', '<p>Product were successfully deleted!</p>');
redirect('manageuser');
}
}
View:
<html>
<title>Manage User</title>
<body>
<style>
.shit{
border: 1px solid;
border-collapse: collapse;
}
.shit td{
border: 1px solid;
border-collapse: collapse;
padding: 7px;
}
.shit td tr{
border: 1px solid;
}
.shit td th{
border: 1px solid;
}
</style>
<div id="container">
<table class="shit">
<tr>
<th><strong>ID</strong></th>
<th><strong>Firstname</strong></th>
<th><strong>Lastname</strong></th>
<th><strong>Username</strong></th>
<th><strong>Password</strong></th>
<th><strong>E-mail</strong></th>
<th><strong>Contact</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
<?php
foreach($user_acc as $row){
echo
'<tr>
<td>'.$row->id.'</td>
<td>'.$row->firstname.'</td>
<td>'.$row->lastname.'</td>
<td>'.$row->username.'</td>
<td>'.$row->password.'</td>
<td>'.$row->email.'</td>
<td>'.$row->contact.'</td>
<td>'.''.'Edit'.'</td>
<td>'.''.'Delete'.'</td>
</tr>';
}
?>
</table>
</div>
</body>
</html>]
How View looks like
Please help me to edit and delete users thanks
For updating
In controller:
$result = array( 'column1'=>$val1,'column2'=>$val2,'column3'=>$val3);
$this->model_name->updateEvent( $id , $result );
In model:
public function updateEvent($id, $result){
$this->db->where('db_table_column', $id);
$this->db->update('table_name', $result);
}
For deleting
In controller:
$this->model_name->deleteEvent( $id );
In model:
public function deleteEvent( $id )
{
$this->db->where('db_table_column', $id);
$this->db->delete('table_name');
}
Related
why is my queue job timing out? I am using database as a driver I tried the following:
class PdfGenerator implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $userData;
protected $filename;
protected $path;
public $timeout = 1200;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($userData, $filename)
{
//
$this->userData = $userData;
$this->filename = $filename;
$this->path = \public_path('\\pdfs\\'.$this->filename.'.pdf');
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$pdf = App::make('snappy.pdf.wrapper');
$footer = \view('supporting.footer')->render();
$header = \view('supporting.header')->render();
//$userData = \collect([$this->userData[1]]);
$pdf->loadView('order_clean', ['users' => $this->userData])
->setOption('margin-top', '20mm')
->setOption('margin-bottom', '20mm')
->setOption('minimum-font-size', 25)
->setOption('header-html', $header)
->setOption('footer-html', $footer);
$pdf->save($this->path);
}
}
php artisan queue:work --timeout=1200
php artisan queue:listen --timeout=0
yet my queue job still fails due to timeout of 60s according to the logs because the symfony process timed out. I am not using supervisor yet, just trying out how the queue works from the console
edit:: controller code + blade code
controller code:
class OrderController extends Controller
{
public function renderPDF()
{
$user_data = $this->getUserData();
$filename = 'test '.\now()->timestamp;
//no timeouts here
//if not ran on queue and with set_time_limit, takes around 70s
//at current data size
$this->dispatch(new PdfGenerator($user_data,$filename));
//view returns successfully
return \view('test.test', ['filename'=>$filename]);
}
}
Blade file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Order Layout</title>
<style>
*{
font-family: cursive;
}
.wrapper {
display: block;
}
.invoice {
display: block;
width: 80%;
margin: 0 auto;
margin-top: 10px;
padding-top: 10px;
padding-bottom: 10px;
background: #d3d3d3;
page-break-inside: avoid !important;
padding-left: 20px;
}
.order-items {
padding-left: 100px;
}
.table {
width: 90%;
align-self: center;
border: 1px solid black;
orphans: 15;
}
.table>* {
text-align: center;
}
</style>
</head>
<body>
<main class="wrapper">
#foreach ($users as $user)
#php
$orders = $user->orders //just for renaming
#endphp
#foreach ($orders as $order)
<div class="invoice">
#php
$sum=0;
#endphp
<h2>{{$user->name}}: {{$order->id}}</h2>
<div class="order-items">
<table class="table" border="1">
<thead>
<tr>
<th>Product Name</th>
<th>Unit Price</th>
<th>Qty</th>
<th>subtotal</th>
</tr>
</thead>
<tbody>
#foreach ($order->products as $product)
<tr>
<th>
{{$product->name}}<br>
{{$product->name}}<br>
{{$product->name}}<br>
{{$product->name}}<br>
</th>
<td>{{$product->unit_price}}</td>
<td>{{$product->pivot->quantity}}</td>
#php
$sum+= $product->pivot->quantity*$product->unit_price
#endphp
<td>{{$product->pivot->quantity*$product->unit_price}}</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th colspan="3">Total:</th>
<td>{{$sum}}</td>
</tr>
</tfoot>
</table>
</div>
</div>
#endforeach
#endforeach
</main>
</body>
</html>
If you want to be able to set the timeouts you should make sure you have the pcntl PHP extension installed and enabled.
"The pcntl PHP extension must be installed in order to specify job timeouts."
Laravel 8.x Docs - Queues - Dispatching Jobs - Specifying Max Job Attempts / Timeout Values - Timeout
In my education resources controller system, we have to prepare progress reports for student.To get these reports as a pdf I used Laravel dom-pdf.
code of pdfGeneratorController
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use App\Subject;
use PDF;
class pdfGenerator extends Controller
{
public function reportPdf()
{
$users = DB::table('users')->get();
$subjects = DB::table('subjects')->get();
$pdf = PDF::loadview('reports', ['users' => $users],['subjects' => $subjects]);
// $pdf = PDF::loadview('reports', ['subjects' => $subjects]);
return $pdf->download('report.pdf');
}
public function report()
{
// $users = DB::table('users')->get();
$users = DB::table('users')
->join('subjects','users.id','=','subjects.user_id')
->join('marks','subjects.user_id','=','subjects.user_id')
->select('users.*','subjects.subject','marks.marks')
->get();
//$subject = Subject::all();
// return $subject;
return view('reports', ['users' => $users]);
}
}
codes of reports.blade.php
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h1><a href="{{url('/pdfs')}}" > Download pdf</a></h1>
<table>
<caption>Student Details</caption>
<tr>
<td>Name</td>
<td> lname </td>
<td>subject</td>
<td>marks</td>
</tr>
#foreach($users as $user)
<tr>
<td>{{ $user->name}}</td>
<td>{{ $user->lname}}</td>
<td>{{ $user->subject}}</td>
<td>{{ $user->marks}}</td>
#endforeach
</tr>
</table>
</body>
</html>
When I click reports link it shows all the relevant data webpage. but when i click print pdf link it gives a error.the error of this time is ErrorException
Undefined property: stdClass::$subjects (View: E:\nimnayawebsite\resources\views\reports.blade.php)
You are passing two array with comma separated value, pass it making one array.
Change this line-
$pdf = PDF::loadview('reports', ['users' => $users],['subjects' => $subjects]);
To this-
$pdf = PDF::loadview('reports', ['users' => $users,'subjects' => $subjects]);
You're trying yo use $user->subject in the view. Since report() method works for you with the same view, change this:
$users = DB::table('users')->get();
$subjects = DB::table('subjects')->get();
To:
$users = DB::table('users')
->join('subjects','users.id','=','subjects.user_id')
->join('marks','subjects.user_id','=','subjects.user_id')
->select('users.*','subjects.subject','marks.marks')
->get();
I tried to render a table from MS-SQL Database to Webpage and i get this error.
I'm still new in PHP. Please help
Useraccess.php
<?php
$path = dirname(__FILE__);
require_once(dirname(__FILE__)."/simpleusers/config.inc.php");
$SimpleUsers = new SimpleUsers();
$users = $SimpleUsers->getUsers();
class SimpleUsers
{
private $mysqli , $stmt;
private $conn;
private $sessionName = "SimpleUsers";
public $logged_in = false;
public $userdata;
public $uPassword;
public $salt;
public function getUsers()
{
$sql = "SELECT DISTINCT userId, uUsername, uActivity, uCreated FROM users ORDER BY uUsername ASC";
$stmt = sqlsrv_query($this->conn, $sql);
if( $stmt == false){
throw new Exception("Query Failed:".sqlsrv_errors());
}
$stmt->execute();
$stmt->store_result();
if( $stmt->num_rows == 0){
return array();
}
$users = array();
$i = 0;
while( $stmt->fetch() )
{
$users[$i]["userId"] = $userId;
$users[$i]["uUsername"] = $username;
$users[$i]["uActivity"] = $activity;
$users[$i]["uCreated"] = $created;
$i++;
}
}
}
?>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<style type="text/css">
* { margin: 0px; padding: 0px; }
body
{
padding: 30px;
font-family: Calibri, Verdana, "Sans Serif";
font-size: 12px;
}
table
{
width: 800px;
margin: 0px auto;
}
th, td
{
padding: 3px;
}
.right
{
text-align: right;
}
h1
{
color: #FF0000;
border-bottom: 2px solid #000000;
margin-bottom: 15px;
}
p { margin: 10px 0px; }
p.faded { color: #A0A0A0; }
</style>
</head>
<body>
<h1>User administration</h1>
<table cellpadding="0" cellspacing="0" border="1">
<thead>
<tr>
<th>Username</th>
<th>Last activity</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="4" class="right">
Create new user | Logout
</td>
</tr>
</tfoot>
<tbody>
<?php foreach
( $users as $user ): ?>
<tr>
<td><?php echo $user["uUsername"]; ?></td>
<td class="right"><?php echo $user["uActivity"]; ?></td>
<td class="right"><?php echo $user["uCreated"]; ?></td>
<td class="right">Delete | User info | Change password</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
config.inc.php
<?php
$GLOBALS["serverName"] = "DESKTOP-KRF6KT7\SQLEXPRESS";
$GLOBALS["database"] = "SimpleUsers";
$GLOBALS["uid"] = "sa";
$GLOBALS["pwd"] = "twinz0000";
$GLOBALS["connectionInfo"] = array(
"Database"=>$GLOBALS["database"],
"UID"=>$GLOBALS["uid"],
"PWD"=>$GLOBALS["pwd"])
?>
Error Displayed
Warning: sqlsrv_query() expects parameter 1 to be resource, null given in C:\Users\Adam\Desktop\SimpleUsers MSSQL\Useraccess.php on line 26
Notice: Array to string conversion in C:\Users\Adam\Desktop\SimpleUsers MSSQL\Useraccess.php on line 29
Fatal error: Uncaught exception 'Exception' with message 'Query Failed:Array' in C:\Users\Adam\Desktop\SimpleUsers MSSQL\Useraccess.php:29 Stack trace: #0 C:\Users\Adam\Desktop\SimpleUsers MSSQL\Useraccess.php(8): SimpleUsers->getUsers() #1 {main} thrown in C:\Users\Adam\Desktop\SimpleUsers MSSQL\Useraccess.php on line 29
Your conn class property is not set.
Before you call $stmt = sqlsrv_query($this->conn, $sql); you must set it up in sqlsrv_connect.
Try adding construct:
public function __construct()
{
$this->conn = sqlsrv_connect($GLOBALS["serverName"], $GLOBALS["connectionInfo"]);
}
Useraccess.php
<?php
$path = dirname(__FILE__);
require_once(dirname(__FILE__)."/simpleusers/config.inc.php");
$SimpleUsers = new SimpleUsers();
$users = $SimpleUsers->getUsers();
class SimpleUsers
{
private $mysqli , $stmt;
private $conn;
private $sessionName = "SimpleUsers";
public $logged_in = false;
public $userdata;
public $uPassword;
public $salt;
public $conn=$GLOBALS["conn"] ;
public function getUsers()
{
$sql = "SELECT DISTINCT userId, uUsername, uActivity, uCreated FROM users ORDER BY uUsername ASC";
$stmt = sqlsrv_query($this->conn, $sql);
if( $stmt == false){
throw new Exception("Query Failed:".sqlsrv_errors());
}
$stmt->execute();
$stmt->store_result();
if( $stmt->num_rows == 0){
return array();
}
$users = array();
$i = 0;
while( $stmt->fetch() )
{
$users[$i]["userId"] = $userId;
$users[$i]["uUsername"] = $username;
$users[$i]["uActivity"] = $activity;
$users[$i]["uCreated"] = $created;
$i++;
}
}
}
?>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<style type="text/css">
* { margin: 0px; padding: 0px; }
body
{
padding: 30px;
font-family: Calibri, Verdana, "Sans Serif";
font-size: 12px;
}
table
{
width: 800px;
margin: 0px auto;
}
th, td
{
padding: 3px;
}
.right
{
text-align: right;
}
h1
{
color: #FF0000;
border-bottom: 2px solid #000000;
margin-bottom: 15px;
}
p { margin: 10px 0px; }
p.faded { color: #A0A0A0; }
</style>
</head>
<body>
<h1>User administration</h1>
<table cellpadding="0" cellspacing="0" border="1">
<thead>
<tr>
<th>Username</th>
<th>Last activity</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="4" class="right">
Create new user | Logout
</td>
</tr>
</tfoot>
<tbody>
<?php foreach
( $users as $user ): ?>
<tr>
<td><?php echo $user["uUsername"]; ?></td>
<td class="right"><?php echo $user["uActivity"]; ?></td>
<td class="right"><?php echo $user["uCreated"]; ?></td>
<td class="right">Delete | User info | Change password</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
config.inc.php
<?php
$GLOBALS["serverName"] = "DESKTOP-KRF6KT7\SQLEXPRESS";
$GLOBALS["database"] = "SimpleUsers";
$GLOBALS["uid"] = "sa";
$GLOBALS["pwd"] = "twinz0000";
$GLOBALS["connectionInfo"] = array(
"Database"=>$GLOBALS["database"],
"UID"=>$GLOBALS["uid"],
"PWD"=>$GLOBALS["pwd"]);
$GLOBALS["conn"] = sqlsrv_connect($GLOBALS["serverName"], $GLOBALS["connectionInfo"] );
?>
Hope it will help.
I have these two items in my php.ini file enabled:
extension=php_pdo_sqlsrv_53_ts.dll
extension=php_sqlsrv_53_ts.dll
Which is what I had before. Using Wamp Server. PHP 5.3.13 all the same as before. What else am I missing that is not allowing this to connect to the SQL server.
After connection file code.
<?php
$GLOBALS["serverName"] = "localhost";
$GLOBALS["database"] = "SimpleUsers";
$GLOBALS["uid"] = "root";
$GLOBALS["pwd"] = "";
$GLOBALS["connectionInfo"] = array(
"Database"=>$GLOBALS["database"],
"UID"=>$GLOBALS["uid"],
"PWD"=>$GLOBALS["pwd"]);
$conn = sqlsrv_connect($GLOBALS["serverName"], $GLOBALS["connectionInfo"]);
?>
I am trying to display image from database using codeigniter. When a user search for a location, the information will display out including images. But I fail to display out the images. I have saved my image at file outside application. Here is my code.
//view.php
<style>
#searchbutton{
position: absolute;
left:300px;
top:30px;
}
fieldset {
background-color:#EFEAEA;
margin: 0px 0px 10px 0px;
padding: 20px;
border-radius: 1px;
width:900px;
margin-left:220px;
margin-top:-10px;
}
#user{
font-style:italic;
font-size: 12px;
text-align:right;
}
#titlereview {
font-style: italic;
font-size:20px;
}
#review {
font-size:16px;
}
</style>
<?=form_open_multipart('viewreview/view');?>
<?php $search = array('name'=>'search',);?>
<div id = "searchbutton">
<?=form_input($search);?><input type=submit value="Search" /></p>
</div>
<?=form_close();?>
<div class = "tablestyle">
<fieldset>
<?php foreach ($query as $row): ?>
<div id = "user">
User: <?php echo $row->name; ?><br>
Visited time: <?php echo $row->visitedtime; ?><br>
</div>
<div id = "titlereview">"<?php echo $row->titlereview; ?>"<br></div>
<div id = "review"><?php echo $row->yourreview; ?><br></div>
<div id = "image"><?php echo '<img src="data:image/jpeg, $row[images]"/>' ?<br><hr><br></div>
<?php endforeach; ?>
</fieldset>
</div>
//controller
<?php
class viewreview extends CI_Controller {
public function view($page = 'viewreview') //writereview page folder name
{
$this->load->model('viewreview_model');
$data['query'] = $this->viewreview_model->get_data();
$this->load->vars($data);
if ( ! file_exists('application/views/viewreview/'.$page.'.php')) //link
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = 'View Review';
//$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->helper('html');
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('templates/header', $data);
$this->load->view('viewreview/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
//model
<?php
class viewreview_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_data()
{
$match = $this->input->post('search');
$this->db->like('sitename',$match);
$this->db->or_like('titlereview',$match);
$this->db->or_like('yourreview',$match);
$this->db->or_like('suggestion',$match);
$query = $this->db->get('review'); //pass data to query
return $query->result();
}
}
?>
by default Code-igniter will return data in the form of std-object. you need to access that with -> (arrow).
e.g. jd reply in comment
<?php echo '<img src="data:image/jpeg,'.$row->images.'" />' ?>
or you need to pass parameter 'array' in function in your model
use this in your model : return $query->result('array'); return (Array)
Instance : return $query->result(); return (Std-Object)
I'm new to codeigniter and I want to update and delete a data from the table. Can someone help solve this?I've tried other options but wasn't successful.
Here's my view:
<script>
function show_confirm(act) {
if (act == "edit")
var r = confirm("Do you really want to edit this?");
else
var r = confirm("Do you really want to delete this?");
if (r==true){
window.location="<?php echo base_url(); ?>site/"+act;
}
}
</script>
<style>
select{
position: absolute;
left: 35%;
height: 5%;
width: 33%;
border-radius: 5px;
font-family: arial;
}
table {
background: #333;
width: 700px;
border-collapse: 1px;
font-family: arial;
position: absolute;
top: 33%;
left: 25%;
color: #777;
}
th {
padding-top: 10px;
padding-bottom: 10px;
}
td {
background: #999;
padding: 10px;
text-align: center;
font-size: 12px;
}
.data {
color: #555;
}
.data:hover {
background: #FFF;
}
#action {
font-weight: bold;
color: #444;
text-decoration: none;
}
#action:hover {
color: #FFF;
}
</style>
</head>
<body>
<header>
<div class="menu">
<h1 class="sis">SIS</h1>
<p class="welcome">Welcome!   |  </p>
<p class="logout">Logout</p>
<nav class="navi">
<ul>
<li>Home</li>
<li>About
<ul class="about">
<li>Vision/Mission</li>
<li>History</li>
</ul>
</li>
<li>Admission</li>
<li>Calendar</li>
<li>Students
<ul class="stud">
<li>Grade I</li>
<li>Grade II</li>
<li>Grade III</li>
<li>Grade IV</li>
<li>Grade V</li>
<li>Grade VI</li>
<li>Academic Records</li>
</ul>
</li>
<li>Teachers
<ul class="teach">
<li>Gradesheet</li>
<li>Lesson Plan</li>
</ul>
</li>
<li>Contact Us</li>
</ul>
</nav>
</div>
</header>
<header>
<div class="container">
<select>
<option placeholder="section"></option>
<option value="section">Section I</option>
<option value="section">Section II</option>
<option value="section">Section III</option>
<option value="section">Section IV</option>
<option value="section">Section V</option>
<option value="section">Section VI</option>
</select>
<table>
<tr id="field">
<th scope="col"> ID </th>
<th scope="col"> First Name </th>
<th scope="col"> Last Name </th>
<th scope="col"> Contact # </th>
<th scope="col"> Address </th>
<th scope="col" colspan="2"> Action </th>
</tr>
<?php foreach ($user_list as $u_key) { ?>
<tr class="data" >
<td><?php echo $u_key->ID; ?></td>
<td><?php echo $u_key->FNAME; ?></td>
<td><?php echo $u_key->LNAME; ?></td>
<td><?php echo $u_key->CONTACT; ?></td>
<td><?php echo $u_key->ADDRESS; ?></td>
<td width="40" align="left"><a id="action" href="#" onClick="show_confirm('edit',<?php echo $u_key->ID; ?>)">Edit</a></td>
<td width="40" align="left"><a id="action" href="#" onClick="show_confirm('delete',<?php echo $u_key->ID; ?>)">Delete</a></td>
</tr>
<?php } ?>
</table>
</div>
</header>
</body>
</html>
My Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
$this->load->helper('url');
}
public function home(){
$data['title'] = "Home";
$this->load->view("view_main");
}
function vismis(){
$data['title'] = "Vismis";
$this->load->view("view_vismis");
}
function history(){
$data['title'] = "History";
$this->load->view("view_history");
}
function admission(){
$data['title'] = "Admission";
$this->load->view("view_admission");
}
function contact(){
$data['title'] = "Contact";
$this->load->view("view_contact");
}
function calendar($year = null, $month = null){
$data['title'] = "Calendar";
if(!$year) {
$year = date('Y');
}
if(!$month) {
$month = date('m');
}
$this->load->model('cal_model');
if ($day = $this->input->post('day')) {
$this->cal_model->add_cal_data(
"$year-$month-$day",
$this->input->post('data')
);
}
$data['calendar'] = $this->cal_model->generate($year, $month);
$this->load->view("view_calendar",$data);
}
public function gradeI(){
$this->load->model('stud_model');
$data['title'] = "gradeI";
$data['user_list'] = $this->stud_model->get_users();
$this->load->view('view_students', $data);
}
function add_stud(){
$this->load->model('stud_model');
$udata['fname'] = $this->input->post('fname');
$udata['lname'] = $this->input->post('lname');
$udata['contact'] = $this->input->post('contact');
$udata['address'] = $this->input->post('address');
$res = $this->stud_model->insert_user_to_db($udata);
if ($res){
header('location:'.base_url()."site/gradeI");
}
}
public function edit() {
$this->load->model('stud_model');
$data['title'] = "edit";
$id = $this->uri->segment(3);
$data['user'] = $this->stud_model->getById($id);
$this->load->view('view_editstud', $data);
}
public function update_stud() {
$mdata['fname'] = $_POST['fname'];
$mdata['lname'] = $_POST['lname'];
$mdata['contact'] = $_POST['contact'];
$mdata['address'] = $_POST['address'];
$res = $this->stud_model->update_info($mdata, $_POST['id']);
if($res) {
header('location'.base_url()."site/gradeI");
}
}
public function delete() {
$this->load->model('stud_model');
$id = $this->uri->segment(3);
$res = $this->stud_model->del_stud($id);
if ($res) {
header('location:'.base_url()."site/gradeI");
}
}
function gradeII(){
$this->load->model('stud_model');
$data['title'] = "gradeII";
$data['user_list'] = $this->stud_model->get_users();
$this->load->view('view_stud_two',$data);
}
function gradeIII(){
$data['title'] = "gradeIII";
$this->load->view('view_students');
}
function gradeIV(){
$data['title'] = "gradeIV";
$this->load->view('view_students');
}
function gradeV(){
$data['title'] = "gradeV";
$this->load->view('view_students');
}
function gradeVI(){
$data['title'] = "gradeVI";
$this->load->view('view_students');
}
function acadrecs(){
$data['title'] = "acadrecs";
$this->load->view('view_acadrecs');
}
public function enroll(){
$data['title'] = "enroll";
$this->load->view('view_enroll');
}
}
and my Model:
<?php
class Stud_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database("sis");
}
public function get_users() {
$query = $this->db->get('students');
return $query->result();
}
public function insert_user_to_db($udata) {
return $this->db->insert('students', $udata);
}
public function getById($id) {
$query = $this->db->get_where('students', array('id'=>$id));
return $query->row_array();
}
public function update_info($data, $id) {
$this->db->where('ID', $id);
return $this->db->update('students', $data);
}
public function del_stud($id) {
$this->db->where('ID', $id);
return $this->db->delete('students');
}
}
nothing happens when I click on delete.
On the edit link...
The view:
<form method="post" action="<?php echo base_url();?>site/update_stud">
<?php extract($user); ?>
<table>
<tr>
<th scope="row">Enter your first name</th>
<td><input type="text" name="fname" size="20" value="<?php echo $fname; ?>" /></td>
</tr>
<tr>
<th scope="row">Enter your last name</th>
<td><input type="text" name="lname" size="20" value="<?php echo $lname; ?>" /></td>
</tr>
<tr>
<th scope="row">Enter your contact number</th>
<td><input type="text" name="contact" size="20" value="<?php echo $contact; ?>" /></td>
</tr>
<tr>
<th scope="row">Enter your address</th>
<td><textarea name="address" rows="5" cols="20"><?php echo $address; ?> </textarea></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="submit" name="submit" value="Update" /></td>
</tr>
</table>
This error will show up:
Enter your first name A PHP Error was encountered
Severity: Notice
Message: Undefined variable: fname
Filename: views/view_editstud.php
Line Number: 60
" />
and so on with other input types.
Firstly, neither of the javascript confirm links will work because you're not actually passing the id through with the act:
<script>
function show_confirm(act, id) {
if (act == "edit") {
var r = confirm("Do you really want to edit this?");
} else {
var r = confirm("Do you really want to delete this?");
}
//Just FYI, the above can be written like this:
//var r = (act == 'edit') ? confirm("Do you really want to edit this?") : confirm("Do you really want to delete this?");
//or even
//var r = confirm("Do you really want to "+act+" this?");
if (r == true) {
window.location = "<?php echo base_url(); ?>site/" + act + "/" + id;
}
//Another FYI, the above could be written like this:
// !r || window.location = "<?php echo base_url(); ?>site/" + act + "/" + id;
}
</script>
Obviously, you can delete the comments.
Secondly, you don't need to use $this->uri->segment() to pass params to a controller method as codeigniter will automatically pass them (Unless you're using URI ROUTING), you can instead:
public function delete($id)
{
$this->load->model('stud_model');
$res = $this->stud_model->del_stud($id);
//There is no point in this if statement as it will just display a
//blank page if $res evaluates to FALSE
if ($res) {
//As you aren't using a variable below you might as well use single quotes
//Also, instead of using header('Location') you should use redirect() with codeiginiter
redirect('site/gradeI');
}
}
Lastly, you should really think about validation e.g. codeigniters built in Form_validation Library.
Hope this helps!