Unable to delete data in CodeIgniter facing Fatal error - php

fetch_view.php
This is my fetch_view.php file through this file I am showing my html.
<?php
foreach ($h->result() as $row)
{ ?>
<tr>
<td><?php echo $row->fname; ?></td>
<td><?php echo $row->lname; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo $row->mobile; ?></td>
<td><?php echo $row->message; ?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php } ?>
select.php
This is my model in this task.
public function deleteuser($id)
{
$this->load->database();
$this->db->where('id', $id);
$this->db->delete('student');
return true;
}
**home.php**
Controller function, In this file I am making a delete function.
, I have attached screenshot when I tried to delete data I am facing that error.
public function delete()
{
$this->load->model('select');
$id=$this->input->get('id');
if($this->select->deleteuser($id))
{
$data['data']=$this->select->getuser();
$this->load->view('fetch_view', $data);
}
}

Everything is working fine but you don't have a getuser() function defined in your model 'select'.
Note: When creating a model Please give name ending with "..._m" like "select_m" you will confuse about it later when your project will get larger.

Related

giving result with repetition of three times for the same. CodeIgniter PHP MySQLi

This query giving same result 3 times. i couldn't identify the error. I am using codeigniter. The query in the function in model is
Model
public function combinedFunctionTest($userID){
$this->db->from('test_report');
$this->db->join('assign_tble','test_report.scode=assign_tble.scode','LEFT');
$this->db->join('course_details','test_report.ccode=course_details.ccode','LEFT');
$this->db->where('test_report.scode',$userID);
$query=$this->db->get();
return $query->result();
}
Controller
public function results()
{
$this->load->model('AllCourses_m');
$data['records']=$this->AllCourses_m-> combinedFunctionTest('2420DC');
$this->load->view('courses/TestResult',$data);
}
View
<?php
foreach ($records as $rec) {
?>
<tr>
<td>
<?php echo $rec->ccode; ?>
</td>
<td><?php echo $rec->cname; ?></td>
<td><?php echo $rec->adate; ?></td>
<td><?php echo $rec->at_date; ?></td>
<td>
<?php echo $rec->score; ?>
</td>
</tr>
<?php
}
?>
Why this is resulting three times repetition for the same. anybody can help me?

Passing Parameters from View to Controller when a hyperlink is clicked and return parameters from Controller to view

This is View file. I am trying to pass- "$row['ISBN'] . ':SearchByAuthor:' . $Author"- abc:SearchByAuthor:aka - value to function increment_author to controller file.
<?php
?>
<tr>
<td><?php
echo $row['ISBN'];
?></td>
<td><?php
echo $row['name'];
?></td>
<td><?php
echo $row['title'];
?></td>
<td><?php
echo $row['year'];
?></td>
<td><?php
echo $row['price'];
?></td>
<td><?php
echo $row['publisher'];
?></td>
<td><?php
echo $row['number'];
?></td>
<td> <a href="<?php
echo base_url();
?>/increment_author/<?php
echo $row['ISBN'] . ':SearchByAuthor:' . $Author;
?>">
Add to cart</a></td>
</tr>
<?php
?>
This is contoller file and I am doing some calculations and trying the same to return to above search page, which is view. However, it's not working. I am trying to convert native php code to codeIgniter framework. Please help me out in passing parameters from Controller to View and vice versa. Thanks in Advance
public function increment_author($SearchByAuthor)
{
session_start();
$db = new mysqli('localhost','root','','cheapbook') or die('Error connecting to MySQL server.');
mysqli_set_charset($db, 'utf8');
if(isset($SearchByAuthor))
{
// echo $_GET['add'];
$pieces=explode(":", $SearchByAuthor);
$quantity="SELECT D.ISBN, D.title, sum(D.number) number FROM
(SELECT A.ISBN, title, number number FROM
book A, Stocks B WHERE A.ISBN='$pieces[0]' and
A.ISBN=B.ISBN)D
GROUP BY D.ISBN, D.title";
$result = mysqli_query($db,$quantity);
while($quantity_row = mysqli_fetch_array($result))
{
if($_SESSION['cart_'.$pieces[0]])
{
$chan=0;
}
else
{
$_SESSION['cart_count']+=1;
}
if($quantity_row['number']!=$_SESSION['cart_'.$pieces[0]] )
{
$_SESSION['cart_'.$pieces[0]]+=1;
$_SESSION["cartTotal"]+=1;
}
}
$data['SearchByAuthor']=$pieces[0];
}
//header("location:search_vi?SearchByAuthor=".$pieces[0]);
//$data['SearchByAuthor']=$pieces[0];
$this->load->view('search',$data);
}
You don't have to use session_start actually you should use Session Library.
Also there is a whole Database layer in CI.
Try to debug your problem in more details. Check if your $data variable contains proper result, and in your view this will be visible via $SearchByAuthor variable.

get data from database in codeigniter

am new to codeigniter and I want to develop todo app.now the problem is get data from database ,anyone findout and tell me what the error is,
get.php
<html>
<head>
<title></title>
</head>
<body>
<?php foreach($data as $row){?>
<tr>
<td><?php echo $row->task; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php }?>
</body>
</html>
Add_task.php(model)
public function get_user()
{
$this->load->database();
$data=$this->db->get('user');
return $data->result();
}
Welcome.php(controller)
public function get_task($data)
{
$this->load->model('Add_task');
$data=$this->Add_task->get_user();
$this->load->view('get',$data);
}
Am getting error like this
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/get.php
Line Number: 8
Backtrace:
File: C:\wamp\www\CodeIgniter-3.0.4\application\views\get.php Line: 8
Function: _error_handler
File:
C:\wamp\www\CodeIgniter-3.0.4\application\controllers\Welcome.php
Line: 43 Function: view
File:
C:\wamp\www\CodeIgniter-3.0.4\application\controllers\Welcome.php
Line: 33 Function: get_task
File: C:\wamp\www\CodeIgniter-3.0.4\index.php Line: 292 Function:
require_once
Just change your controller as:
public function get_task($data) {
$this->load->model('Add_task');
$data["data"] =$this->Add_task->get_user();
$this->load->view('get',$data);
}
You need to pass associative array in load view $data["data"]
Codeigniter read your array by keys, so every variable you want pass to view you must put it as a key in your $data array like this:
public function get_task()
{
$this->load->model('Add_task');
$rows=$this->Add_task->get_user();
$data['title'] = "page title";
$data['rows'] = $rows;
$this->load->view('get',$data);
}
and in your view, replace $data with $rows:
<?php foreach($rows as $row){?>
<tr>
<td><?php echo $row->task; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php }?>
Just change
$this->load->view('get',$data);
to
$this->load->view('get', ['data' => $data]);
and all be fine.
If you want to pass a variable into a view, you need to create correspond array index for it (with the same name but without the $ sign). For example, if you want to have $var1, $my_variable, $myCustomVariable, and $data in the 'get' view, you need to pass it as:
$this->load->view('get', [
'var1' => 'value of $var1',
'my_variable' => 'value of $my_variable',
'myCustomVariable' => 'value of $myCustomVariable',
'data' => $data
]);
You must pass an associative array that contain your database result.
See you the documentation page for views at "Creating Loops" paragrapher
Welcome.php (controller)
public function get_task($data)
{
$this->load->model('Add_task');
$data['users'] = $this->Add_task->get_user();
$this->load->view('get', $data);
}
And change the loop in your get.php (view) :
<?php foreach($users as $user) { ?>
<tr>
<td><?php echo $user->task; ?></td>
<td><?php echo $user->status; ?></td>
<td><?php echo $user->description; ?></td>
<td><?php echo $user->date; ?></td>
</tr>
<?php } ?>

Select statement is not working

I have wrote a select query using function but it is not returning data from MySQL database... can any one help me in this regard. I'm uploading screenshots.
Here is the name of my database, 'classified'
this is conn.php file
$conn = mysql_connect('localhost','root','');
echo mysql_select_db('classified',$conn);
function to fetch record from database, 'adds' table
On index.php i've wrote this code.
include 'functions/crud_functions.php'
$adds = get_all_adds();
foreach($adds as $add) {
<tr>
<td><?php echo $add['id']; ?></td>
<td><?php echo $add['ad_title'];?></td>
<td><?php echo $add['cat_id'];?></td>
<td><?php echo $add['ad_description'];?></td>
<td><?php echo $add['avatar'];?></td>
<td><?php echo $add['price'];?></td>
<td><?php echo $add['contact'];?></td>
<td><?php echo $add['name'];?></td>
<td><?php echo $add['time'];?></td>
<td>Edit | Del </td>
</tr>
but this is not displaying records.
Change :
if ($result) {
return TRUE;
} else {
return FALSE;
}
into this :
if ($result) {
return $data;
} else {
return array();
}

Conditional based on result_array()

I'm using codeigniter, and i want to make a conditional based on return value from model.
here is the model:
public function get_sentitems()
{
$query = $this->db->get('sentitems');
return $query->result_array();
}
and here is the controller:
public function sentitems()
{
$data['sentitems'] = $this->sms_model->get_sentitems();
if($data['sentitems']['Status'] === 'SendingOKNoReport')
{
$data['status_message'] = 'Sent';
}
else
{
$data['status_message'] = 'Failed';
}
$data['title'] = ucwords('sent items');
$this->load->view('templates/header', $data);
$this->load->view('sms/sentitems', $data);
$this->load->view('templates/footer');
}
And here is the view
<h2><?php echo $title; ?></h2>
<table border="1" width="100%">
<thead>
<tr>
<th>No.</th>
<th>Tujuan</th>
<th>Waktu</th>
<th>Isi</th>
<th>Ket.</th>
</tr>
</thead>
<tbody>
<?php foreach ($sentitems as $sentitems_item): ?>
<tr>
<td><?php echo $sentitems_item['ID']; ?></td>
<td><?php echo $sentitems_item['DestinationNumber']; ?></td>
<td><?php echo $sentitems_item['SendingDateTime']; ?></td>
<td><?php echo $sentitems_item['TextDecoded']; ?></td>
<td><?php echo $sentitems_item['Status']; ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
I have a column Status, but why the result in browser is always like this:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: Status
Filename: controllers/sms.php
Line Number: 62
What is the solution? Sorry for my bad English.
The issue is that in your controller $data['sentitems'] contains an array of results from your database, so you cannot get at it with $data['sentitems']['Status']. What you actually have is
$data['sentitems'][0]['Status']
$data['sentitems'][1]['Status']
$data['sentitems'][2]['Status']
and so on.
I think your best option at this point is to remove the following from your controller:
if($data['sentitems']['Status'] === 'SendingOKNoReport')
{
$data['status_message'] = 'Sent';
}
else
{
$data['status_message'] = 'Failed';
}
and amend the line of your template that outputs the status to:
<td><?php echo $sentitems_item['Status'] == 'SendingOKNoReport' ? 'Sent' : 'Failed'; ?></td>

Categories