Conditional based on result_array() - php

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>

Related

Undefined variable: gejala (Code Igniter)

Controller :
public function TampilGejala(){
$data['gejala'] = $this->model_admin->tampil_gejala();
$this->load->view('auth/admin_gejala',$data);
}
Models:
public function tampil_gejala() {
return $this->db->get('gejala')->result_array();
}
View :
<?php
foreach($gejala as $g){
?>
<tr>
<td><?php echo $g->kode_gejala ?></td>
<td><?php echo $g->nama_gejala ?></td>
<td>Button</td>
</tr>
<?php } ?>
Mysql:
enter image description here
Can you help me ?

Unable to delete data in CodeIgniter facing Fatal error

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.

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 } ?>

How Can I Pass Result of Foreach from Controller to View CodeIgniter

just like my title, how can I pass foreach result from controller to view?
So far I did this:
--Controller.php
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
foreach($get_json_subkategori['SubKategoris'] as $itemSubKategori){
$kodeSubKategori = $itemSubKategori['KodeSubKategori'];
$namaSubKategori = $itemSubKategori['Nama'];
$definisiSubKategori = $itemSubKategori['Definisi'];
$unsurAbstrakSubKategori = $itemSubKategori['UnsurAbstrak'];
$aliasSubKategori = $itemSubKategori['Alias'];
$data['kodeSubKategori']=$kodeSubKategori;
$data['namaSubKategori']=$namaSubKategori;
$data['definisiSubKategori']=$definisiSubKategori;
$data['unsurAbstrakSubKategori']=($unsurAbstrakSubKategori) ? 'true' : 'false';
$data['aliasSubKategori']=$aliasSubKategori;
}
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
Then, in my view:
<h3>Sub Kategori</h3>
<table class="table table-responsive table-inherit">
<tr>
<th>Kode Sub Kategori</th>
<th>Nama Kategori</th>
<th>Definisi</th>
<th>Unsur Abstrak</th>
<th>Alias</th>
</tr>
<tr>
<?php
foreach((array)$kodeSubKategori as $itemKodeSubKategori)
echo '<td>'.$itemKodeSubKategori.'</td>';
?>
<?php
foreach((array)$namaSubKategori as $itemNamaSubKategori)
echo '<td>'.$itemNamaSubKategori.'</td>';
?>
<?php
foreach((array)$definisiSubKategori as $itemDefinisiSubKategori)
echo '<td>'.$itemDefinisiSubKategori.'</td>';
?>
<?php
foreach((array)$unsurAbstrakSubKategori as $itemUnsurAbstrakSubKategori)
echo '<td>'.$itemUnsurAbstrakSubKategori.'</td>';
?>
<?php
foreach((array)$aliasSubKategori as $itemAliasSubKategori)
echo '<td>'.$itemAliasSubKategori.'</td>';
?>
</tr>
</table>
It successfully show ONLY THE LAST ONE data. I want to get all data from the result from foreach. How can I do that?
I suggest you to pass all $get_json_subkategori['SubKategoris'] array in data array to your view:
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
$data['itemSubKategori'] = $get_json_subkategori['SubKategoris'];
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
And in your view:
<h3>Sub Kategori</h3>
<table class="table table-responsive table-inherit">
<tr>
<th>Kode Sub Kategori</th>
<th>Nama Kategori</th>
<th>Definisi</th>
<th>Unsur Abstrak</th>
<th>Alias</th>
</tr>
<?php foreach($itemSubKategori as $rowSubKategori): ?>
<tr>
<td><?php echo $rowSubKategori['kodeSubKategori'] ?></td>
<td><?php echo $rowSubKategori['namaSubKategori'] ?></td>
<td><?php echo $rowSubKategori['definisiSubKategori'] ?></td>
<td><?php echo $rowSubKategori['definisiSubKategori'] ?></td>
<td><?php echo $rowSubKategori['unsurAbstrakSubKategori'] ?></td>
<td><?php echo $rowSubKategori['aliasSubKategori'] ?></td>
</tr>
<?php endforeach; ?>
</table>
Because you data array override each time that's y you get only last value every time.
You need to create your array with auto incremented value
$i=0;
foreach($get_json_subkategori['SubKategoris'] as $itemSubKategori){
$kodeSubKategori = $itemSubKategori['KodeSubKategori'];
$namaSubKategori = $itemSubKategori['Nama'];
$definisiSubKategori = $itemSubKategori['Definisi'];
$unsurAbstrakSubKategori = $itemSubKategori['UnsurAbstrak'];
$aliasSubKategori = $itemSubKategori['Alias'];
$data[$i]['kodeSubKategori']=$kodeSubKategori;
$data[$i]['namaSubKategori']=$namaSubKategori;
$data[$i]['definisiSubKategori']=$definisiSubKategori;
$data[$i]['unsurAbstrakSubKategori']=($unsurAbstrakSubKategori) ? 'true' : 'false';
$data[$i]['aliasSubKategori']=$aliasSubKategori;
$i++;
}
Pass data in one array. and you can catch all in view at one foreach llop
Use this
In Controller
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
$data['itemKodeSubKategori'] = $get_json_subkategori;
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
In View
<tr>
<?php
foreach($kodeSubKategori as $itemKodeSubKategori)
{
echo '<td>'.$itemKodeSubKategori['KodeSubKategori'].'</td>';
echo '<td>'.$itemKodeSubKategori['Nama'].'</td>';
echo '<td>'.$itemKodeSubKategori['Definisi'].'</td>';
echo '<td>'.$itemKodeSubKategori['UnsurAbstrak'].'</td>';
echo '<td>'.$itemKodeSubKategori['Alias'].'</td>';
}
?>
</tr>

Get error "You do not have sufficient permissions to access this page" While creating custom plugin

I have created custom plugin in wordpress for get data from the database. And I have added two link to perfrom action on records .One is Edit and another is Delete. I have written code for delete link like this
Delete
When I click on delete link it will show error like
You do not have sufficient permissions to access this page
My plugin code is
function submit_review()
{
add_options_page("Submit Review","Submit Reviews",1,"submit_review","submit_review");
global $wpdb;
$id = get_current_user_id();
$q = "select * from wp_submit_review where user_id = " . $id;
$result = $wpdb->get_results($q,OBJECT);
if(!empty($result))
{
?>
<div class="wrap">
<h1>Submitted Reviews</h1>
<div style="width: 100%;">
<table cellpadding="5" class="wp-list-table widefat fixed pages">
<thead>
<tr>
<th><b>Review Id</b></th>
<th><b>User Name</b></th>
<th><b>Submitted Category</b></th>
<th><b>New Listing Name</b></th>
<th><b>New Listing Address</b></th>
<th><b>New Listing City</b></th>
<th><b>New Listing Description</b></th>
<th><b>Image</b></th>
<th><b>R-option</b></th>
<th><b>New Listing Rating</b></th>
<th><b>New Listing Review</b></th>
<th><b>Actions</b></th>
</tr>
</thead>
<tbody>
<?php
foreach($result as $res)
{
?>
<td><?php echo $res->review_id; ?></td>
<td><?php echo get_the_author_meta('user_login',$res->user_id); ?></td>
<td><?php
if($res->submit_category == 1)
{
echo "Service";
}
else if($res->submit_category == 2)
{
echo "Restaurant";
}
else
{
echo "Product";
}
?>
</td>
<td><?php echo $res->newlistingname; ?></td>
<td><?php echo $res->newlistingaddress; ?></td>
<td><?php echo $res->newlistingcity; ?></td>
<td><?php echo $res->newlistingdesc; ?></td>
<td><img src="<?php echo home_url()."/uploads/".$res->image; ?>" height="50px" width="50px"/></td>
<td><?php echo $res->roption; ?></td>
<td><?php echo $res->newlistingrating; ?></td>
<td><?php echo $res->newlistingreview; ?></td>
<td>Edit | Delete</td>
<?php
}
?>
</tbody>
</table>
</div>
</div>
<?php
}
else
{
echo "No review find";
}
}
function delete_my_review()
{
echo $_GET['id'];
}
add_action("admin_menu","delete_my_review");
function submit_review_menu()
{
add_menu_page("Submit Review","Submit Review","manage_options","submit_review", submit_review);
}
//add_action("admin_menu","submit_review");
add_action("admin_menu","submit_review_menu");
So how to call delete_my_review() function to perform delete action?
simply create a function with ajax and php. following example worked for me.
html delete button
<td><button onclick="delete_ab(<?php echo $results->id; ?>)">Delete</button></td> // pass your id to delete
javascript function
function delete_ab(str)
{
jQuery.post(
// see tip #1 for how we declare global javascript variables
MyAjax.ajaxurl,
{
// here we declare the parameters to send along with the request
// this means the following action hooks will be fired:
// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
action : 'myajax_submit',
// other parameters can be added along with "action"
postID : str
},
function(response)
{
document.getElementById(str).style.display='none';
}
);
}
main function in php
function myajax_submit() {
// get the submitted parameters
$postID = $_POST['postID'];
global $wpdb;
$qry=$wpdb->query("DELETE FROM `price_table` WHERE id=$postID");
// generate the response
echo "hello";
// IMPORTANT: don't forget to "exit"
exit;
}

Categories