I am new in codeigniter.In my view page I am showing the data from database in a table where I have two anchor tags for update and delete. I want to delete a specific row from database through id.
my view page is
<?php foreach($query as $row){ ?>
<tr>
<td><?php echo $row->name ?></td>
<td><?php echo $row->testi ?></td>
<td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
<td><i class="icon-trash"></i></td>
</tr>
<?php } ?>
</table>
my controller page is
function delete_row()
{
$this->load->model('mod1');
$this->mod1->row_delete();
redirect($_SERVER['HTTP_REFERER']);
}
and my model page is
function row_delete()
{
$this->db->where('id', $id);
$this->db->delete('testimonials');
}
I want to delete the row by catching the respective id. Please dont be harsh. thank you
You are using an $id variable in your model, but your are plucking it from nowhere. You need to pass the $id variable from your controller to your model.
Controller
Lets pass the $id to the model via a parameter of the row_delete() method.
function delete_row()
{
$this->load->model('mod1');
// Pass the $id to the row_delete() method
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);
}
Model
Add the $id to the Model methods parameters.
function row_delete($id)
{
$this->db->where('id', $id);
$this->db->delete('testimonials');
}
The problem now is that your passing the $id variable from your controller, but it's not declared anywhere in your controller.
a simple way:
in view(pass the id value):
<td><?php echo anchor('textarea/delete_row?id='.$row->id, 'DELETE', 'id="$row->id"'); ?></td>
in controller(receive the id):
$id = $this->input->get('id');
$this->load->model('mod1');
$this->mod1->row_delete($id);
in model(get the passed args):
function row_delete($id){}
Actually, you should use the ajax to POST the id value to controller and delete the row, not the GET.
**multiple delete not working**
function delete_selection()
{
$id_array = array();
$selection = $this->input->post("selection", TRUE);
$id_array = explode("|", $selection);
foreach ($id_array as $item):
if ($item != ''):
//DELETE ROW
$this->db->where('entry_id', $item);
$this->db->delete('helpline_entry');
endif;
endforeach;
}
It will come in the url so you can get it by two ways.
Fist one
<td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
$id = $this->input->get('id');
2nd one.
$id = $this->uri->segment(3);
But in the second method you have to count the no. of segments in the url that on which no. your id come. 2,3,4 etc. then you have to pass. then in the ();
My controller
public function delete_category() //Created a controller class //
{
$this->load->model('Managecat'); //Load model Managecat here
$id=$this->input->get('id'); // get the requested in a variable
$sql_del=$this->Managecat->deleteRecord($id); //send the parameter $id in Managecat there I have created a function name deleteRecord
if($sql_del){
$data['success'] = "Category Have been deleted Successfully!!"; //success message goes here
}
}
My Model
public function deleteRecord($id) {
$this->db->where('cat_id', $id);
$del=$this->db->delete('category');
return $del;
}
Related
I am new to CodeIgniter. I have retrieved data from one table but I'm not able to retrieve the data from another table. The view part is not working. Due to some reason the PDF is displaying blank (using tcpdf). Can we fetch it as array? If yes, then how can we fetch it as array?Some of the data appears to be printed below the button that I press.It does not display on the pdf that is generated after the we click on the button.
code :
controller
index(){
$this->load->database();
//load the model
$this->load->model('Order_model');
//load the method of model
$data['h']=$this->Order_model->select();
//return the data in view
$this->load->view('includes/orderPdf', $data);
}
model
public function select()
{
//data is retrive from this query
$query = $this->db->get('master_user');
return $query;
}
view
<?php
foreach ($h->result() as $row)
{
?><tr>
<td><?php echo $row->mobile_no ?></td>
<td><?php echo $row->country ?></td>
</tr>
<?php }
?>
Best way to set up is probably similar to:
Controller:
$this->load->model('Order_model', 'Order');
$data['orders'] = $this->Order->getOrder();
Model:
public function getOrder() {
$q = $this->db->get('master_user');
return $q->num_rows() ? $q->result_array() : [];
}
View:
<?php foreach($orders as $order) { ?>
<tr>
<td><?=$order['mobile_no']?></td>
<td><?=$order['country']?></td>
</tr>
<?php } ?>
Now you can also add an if(empty($orders)) and put in "no orders to show" or similar.
I want echo my list of trip, when I try print_r the value can show but when I echo the result always
Message: Undefined variable: adventure
Filename: views/profile.php
Line Number: 121
Backtrace:
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/profile.php
Line Number: 121
this is my controller :
public function getListTrip(){
$this->load->model('userModel');
$data['adventure'] = $this->userModel->getTrip()->result_array();
//echo ($data);
$this->load->view('profile', $data);
}
and this is my model :
function getTrip(){
$userId = $this->session->userdata('user_id');
return $this->db->get_where('adventure',['user_id' => $userId]);
}
this is my view
<table>
<?php
foreach ($adventure as $b){
echo "<tr>
<td>$b->name</td>
<td>$b->place</td>
<td>$b->category</td>
</tr>";
}
?>
</table>
so how should I edit my code to make the value show in my page whit echo or foreach not in print_r... thanks a lot
Change your model
function getTrip(){
$userId = $this->session->userdata('user_id');
$query= $this->db->get_where('adventure',['user_id' => $userId]);
return $query->result();
}
Also chnage in your controller
$data['adventure'] = $this->userModel->getTrip()->result_array();
To
$data['adventure'] = $this->userModel->getTrip();
echo is used to output one or more strings, since your $data is an array you see the error, you need to pass data to view and iterate it in the view itself, like:
public function getListTrip(){
$this->load->model('userModel');
$data['adventure'] = $this->userModel->getTrip()->result_array();
//pass data to your view
$this->load->view('yourviewname', $data);
}
For more information check Codeigniter Views
Update
Check if your array is not empty before trying to iterate thru it, like in your view::
<?php
if( !empty($adventure) ) {
foreach($adventure as $b):
?>
<tr>
<td><?php echo $b['name']; ?></td>
<td><?php echo $b['place']; ?></td>
<td><?php echo $b['category']; ?></td>
</tr>
<?php
endforeach;
}
?>
You cannot echo the content of an Array.
So whenever you want to view the Content of an array use print_r($arrayName);
And When you just want to print any variable just use echo $variableName;
I don't see any issue with your code. If you're not using autoloader to load the session library that might be your issue:
$this->load->library('session');
You can check the documentation Codeigniter sessions
Using Codeigniter 3, I would like to display a HTML table consisting of some book details, namely Item ID, Item Image and Item Title. I would like to populate the Item Image via Google Books.
So far my code works, in the sense that I can retrieve all the data from the MySQL database and display it in a simple HTML table. However I'm not sure how to populate the image field in the HTML table with the corresponding image from Google Books.
I am retrieving the ISBN from my database, can I use this to lookup the Google Books URL?
I tried to create a foreach as you will see but it doesn't work so I have commented it out. The error message I receive in the foreach is;
Message: Undefined index: items on line 23
My current code is below;
Model
class Items_model extends CI_Model {
public function itemList() {
$query = $this->db->get('item', 10);
return $query->result_array();
}
}
Controller
class Items extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('items_model');
$this->load->database();
}
public function index() {
$data['items'] = $this->items_model->itemList();
//foreach ($data['items'] as $row)
//{
// $page = //file_get_contents("https://www.googleapis.com/books/v1/volumes?q=isbn:".$row['item_isbn']);
// $bookData = json_decode($page, true);
// $data['BookImage'] = '<img src="'.$bookData['items'][0]['volumeInfo']['imageLinks']['thumbnail'].'" alt="Cover">'; <-- line 23
//}
$this->load->view('item_view', $data);
}
}
View
<table>
<tr>
<td><strong>ID</strong></td>
<td><strong>ISBN</strong></td>
<td><strong>Image</strong></td>
<td><strong>Title</strong></td>
</tr>
<?php foreach ($items as $item): ?>
<tr>
<td><?php echo $item['item_id']; ?></td>
<td><?php echo $item['item_isbn'] ?></td>
<td><?php // what goes here? ?></td>
<td><?php echo $item['item_title']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Any help is appreciated.
You need to use the Googlebooks API to get the id of your book.
First you have to make a request in this way:
foreach ($items as $item):
$data = #file_get_contents('https://www.googleapis.com/books/v1/volumes?q=isbn+".$yourISBN."');
$data = json_decode($data);
$data = $data->items[0]->id;
// Next you have to insert $data(the item_id) inside of one of the
followings links:
// "smallThumbnail": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
// "thumbnail": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api",
// "small": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=2&edge=curl&source=gbs_api",
// "medium": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=3&edge=curl&source=gbs_api",
// "large": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=4&edge=curl&source=gbs_api",
// "extraLarge": "https://books.google.com/books?id=zyTCAlFPjgYC&printsec=frontcover&img=1&zoom=6&edge=curl&source=gbs_api"
//Example:
$thumbnail = "https://books.google.com/books?id=".$data."&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api";
endforeach;
And then you've got your thumbnail.
Note that your id must have this format "zyTCAlFPjgYC".
Have a look at the doc
I get error saying:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/businessinfo_view.php
My model:
function getPosts(){
$query = $this->db->query("SELECT * FROM customer WHERE
customer_id=12;");
return $query->result_array();
}
My controller:
function ind() {
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
MY view:
<?php foreach($data as $d){?>
<tr>
<td><?php echo $d->customer_id;?></td>
<td><?php echo $d->first_name);?></td>
</tr>
<?php }?>
I tried 100 different ways, still can pass the variables, I know my query is correct, I can fetch and echo data in controller, I can't just pass it into view. someone please help me!
function controller() {
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
Whenever you pass variable in view, try to access it with key inside view.
This $customerinfo variable will have all your data.
For EX. $customerinfo because your actual variable is $data['customerinfo'].
if var name is $data['extraVal'], in view access through $extraVal.
Try if you have the $data['customerinfo'] then on the view you would use like $customerinfo
http://www.codeigniter.com/user_guide/general/views.html#creating-loops
Controller
function ind() {
$data['customerinfo'] = array();
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
Model
function getPosts(){
$this->db->where('customer_id', '12');
$query = $this->db->get('customer');
return $query->result_array();
}
View
<?php foreach($customerinfo as $d){?>
<tr>
<td><?php echo $d['customer_id'];?></td>
<td><?php echo $d['first_name'];?></td>
</tr>
<?php }?>
When model function returns result_array() <?php echo $d['first_name'];?>
When model function returns result() <?php echo $d->first_name;?>
Example found here
Try exactly this :
My model:
function getPosts(){
$query = $this->db->query("SELECT * FROM customer WHERE
customer_id = '12' ");
return $query->result();
}
My controller:
function ind() {
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
MY view:
<?php
if(!empty($customerinfo))
{
foreach($customerinfo as $d){?>
<tr>
<td><?php echo $d->customer_id;?></td>
<td><?php echo $d->first_name;?></td>
</tr>
<?php }
}
else{
echo 'Some problem with the variable !! :(';
}
?>
im new to codeigniter and php, need some enlightment to display 2 tables with mvc method. just displaying two tables in one page (camera and visitor table). here is my code
Model :
function m_report() {
$camera = $this->db->get('camera');
return $camera->result();
$report = $this->db->get('visitor');
return $report->result();
}
View:
<?php foreach($data1 as $report){ ?>
<tr>
<td><?php echo $report->Country; ?></td>
<td><?php echo $report->Days; ?></td>
</tr>
<?php } ?>
<?php foreach($data_camera as $camera){ ?>
<tr>
<td><?php echo $camera->cameratype; ?></td>
</tr>
<?php } ?>
Controller :
function report(){
$data['data_camera']=$this->m_data->m_report();
$data1['data1']=$this->m_data->m_report();
$this->load->view('v_report',$data,$data1);
}
the problem is, i can display camera table but visitor got error Message: Undefined variable: data1
Can anyone help me to figure it out? Much appreciate
You can only return ONE thing from a method - once you return something, execution of code stops.
function m_report() {
$camera = $this->db->get('camera')->result();
$report = $this->db->get('visitor')->result();
return array_merge($camera, $report);
}
Now you get an array with all the results from both "camera" and "visitor". You can specify it out if you'd like with an associative array.
function m_report() {
$data['camera'] = $this->db->get('camera')->result();
$data['visitor'] = $this->db->get('visitor')->result();
return $data;
}
you can not make 2 returns in one method
function m_report() {
$camera = $this->db->get('camera');
return $camera->result();
$report = $this->db->get('visitor');
return $report->result();
}
i think it is better to make each query in single function
function m_camera_report() {
$camera = $this->db->get('camera');
return $camera->result();
}
function m_visitor_report() {
$report = $this->db->get('visitor');
return $report->result();
}
then call them separately in controller
function report(){
$data['data_camera']=$this->m_data->m_camera_report();
$data['data_visitor']=$this->m_data->m_visitor_report();
$data1['data1']=$this->m_data->m_report();
$this->load->view('v_report',$data,$data1);
}