I'm a newbiw in Codeigniter and i so desperate facing this problem. So, I just made a simple application with CI. When I want to check an array of object (in this case array of $kunj->DIAG), I get an error notice.
Here's some appearance of my view:
View:
<table class="table table-striped table-bordered table-condensed" id="tablescroll" role="grid">
<thead>
<tr class="tablesorter-headerRow" role="row">
<th>No.</th>
<th>Nama</th>
<?php if(!empty($kunj->DIAG)){ echo "<th>ICD</th>"; } ?>
</tr>
</thead>
<tbody aria-live="polite" aria-relevant="all">
<?php if(!empty($kunj)) print_r(count($kunj));
if(!empty($kunj)){
$no = 0;
foreach ($kunj as $i): ?>
<tr align='center'>
<td><?php $no++; echo $no; ?></td>
<td><?php echo $i->NAMA; ?></td>
<?php if(!empty($i->DIAG)){ echo "<td>".$i->DIAG."</td>"; } ?>
</tr>
<?php endforeach ?>
<?php } ?>
</tbody>
</table>
Controller:
function kunjunganpx(){
$this->load->model('simrs/diagnosa_model');
$cari = array(
'icd' => $this->input->post('icd'),
'asr' => $this->input->post('asuransi'),
'tgl_strt' => $this->input->post('tgl_strt'),
'tgl_end' => $this->input->post('tgl_end'),
'stat' => $this->input->post('px_stat'),
'urut' => $this->input->post('urut'),
'opsi' => $this->input->post('opsi'));
$data['kunj'] = $this->diagnosa_model->get_kunjungan_list($cari);
$this->load->view('kunjunganpx_view',$data);
}
And this is notice I get:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: simrs/kunjunganpx_view.php
From googling some site, I realize that array of object can not checked with empty(). But I desperately want to know how to fix this, since I generate my query result in object.
All I wanna know is how to 'hide' the hardprint of ICD when there is no value in $kunj->DIAG. And show it if the variable has some values. Thanks in advance.
UPDATE
As request from a member I attach the var_dump($data); result here:
array(1) { ["kunj"]=> array(2)
{ [0]=> object(stdClass)#21 (20)
{ ["NAMA"]=> string(13) "SUPRIYANTO BP"
["DIAG"]=> string(5) "D48.0" }
[1]=> object(stdClass)#22 (20)
{ ["NAMA"]=> string(13) "SUPRIYANTO BP"
["DIAG"]=> string(5) "C44.9" }
}
}
OK ... Based on the fact that $kunj=array() this is what I came up with.
empty() is not going to work, because the object has the property or not. If it has the property, then you want to parse the extra column.
You will need to test if the property DIAG exists in the object.
if (property_exists($kunj[0], "DIAG"))
After that you can test if $kunj is empty, just like you are doing. In the foreach loop you will again have to test on the property before parsing the extra table cell. If that property is present, you will not have to test for empty.
I have also changed some of your logic. It all results to this:
<table class="table table-striped table-bordered table-condensed" id="tablescroll" role="grid">
<thead>
<tr class="tablesorter-headerRow" role="row">
<th>No.</th>
<th>Nama</th>
<?php
if (property_exists($kunj[0], "DIAG")) {
echo "<th>ICD</th>";
}
?>
</tr>
</thead>
<tbody aria-live="polite" aria-relevant="all">
<?php if(!empty($kunj)){ foreach ($kunj as $k => $i): ?>
<tr align='center'>
<td><?php echo ++$k; ?></td>
<td><?php echo $i -> NAMA; ?></td>
<?php
if (property_exists($kunj[0], "DIAG")) {
echo "<td>{$i->DIAG}</td>";
}
?>
</tr>
<?php endforeach; } ?>
</tbody>
</table>
codepad example: http://codepad.viper-7.com/3Jxi7k
if($kunj->DIAG != ''){ echo "<th>ICD</th>"; } ?>
I think this will fix it, since some times
as i have seen, or understod, u dont get null values, but empty in thos vars.
Could try this:
if (is_object($kunj) && !empty($kunj->DIAG)) {
// do your stuff
}
I am having this problem and is_null is the solution
try this :
if(!is_null($kunj->DIAG)){echo "<th>ICD</th>";}
Related
I unable to delete specific record from session array. I want to delete specific row in table when I click on delete link.
<?php
session_start();
if(isset($_GET["product"]) && isset($_GET["category"])){
$nomProduct = trim($_GET["product"]);
$category = trim($_GET["category"]);
$_SESSION['product'][] = array(
"nomProduct" => $nomProduct ,
"category" => $category
);
//session_destroy();
}
?>
html table
<table class="table">
<?php foreach($_SESSION["product"] as $items) { ?>
<tr>
<th width="250px"><?php echo $items['nomProduct']; ?></th>
<td><?php echo $items['category']; ?></td>
<td style="text-align: right">Delete<td>
</tr>
<?php }?>
</table>
`
$key=array_search($_GET['product'],$_SESSION['product']);
if($key!==false)
unset($_SESSION['product'][$key]);
$_SESSION["product"] = array_values($_SESSION["product"]);
`
Maybe this might help!
You need to find the key as this is an array.
EDIT:
Made an example for you, here when you click the link, it deletes the first name from the session array.
<?php
session_start();
$_SESSION["user"] = ["fname"=>"William","lname"=>"Henry" ];
if(isset($_GET["delete"]))
{
if($_GET["key"])
{
$key=$_GET["key"];
unset($_SESSION['user'][$key]);
}
}
?>
HTML on the same page
<h1>
<?php
if(isset($_SESSION["user"]["fname"]))echo $_SESSION["user"]["fname"]." ";
if(isset($_SESSION["user"]["lname"]))echo $_SESSION["user"]["lname"];
?>
</h1>
Delete First Name
If you want to delete the lastname (lname), change the key=lname in the href of the link, hope this example helps in your case
Modify your HTML
<table class="table">
<?php foreach($_SESSION["product"] as $key => $items) { ?>
<tr>
<th width="250px"><?php echo $items['nomProduct']; ?></th>
<td><?php echo $items['category']; ?></td>
<td style="text-align: right">Delete<td>
</tr>
<?php }?>
</table>
Catch the array key and remove it from session array.
$key = filter_input(INPUT_GET, 'key');
unset($_SESSION['product'][$key]);
I have a problem with datatable, it's working but it's not working right..
I have the next code:
<table id="dt_basic" class="table table-striped table-bordered table-hover" width="100%">
<thead>
<tr>
<th>Codigo PT</th>
<th><i class="text-muted hidden-md hidden-sm hidden-xs"></i>Producto Terminado</th>
</tr>
</thead>
<tbody>
<?php
foreach ($registros as $reg)
{
?>
<tr style="border-bottom:2px solid #000;background: #cee8ff;">
<td><?php echo $reg -> id_codigo;?></td>
<td><?php echo utf8_encode($reg ->id_product_term);?></td>
</tr>
<?php
$AllRows = $_interfaz -> get_all($reg -> id_interfaz);
foreach ($AllRows as $row)
{
?>
<tr>
<td><b style="font-size:20px;">—</b></td>
<td><?php echo $row -> id_product_term;?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
It has a forearch() { forearch() { } } two foreach, a nested foreach.
The problem is that the datatable put the first foreach result at last and the result of the nested (SECOND) foreach in the beginning, like this:
second foreach result
second foreach result
second foreach result
first foreach result
first foreach result
first foreach result
why does datable organize the table as above?
why does not it do this?:
first foreach result
second foreach result
first foreach result
second foreach result
first foreach result
second foreach result
because one foreach is nested..
how can i fix it?
my js script is:
$('#dt_basic').dataTable({
"sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'col-sm-6 col-xs-12 hidden-xs'l>r>"+
"t"+
"<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
"autoWidth" : true,
"oLanguage": {
"sSearch": '<span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>'
}});
Thanks.
why does datable organize the table as above?
Because dataTables is ordering the rows by default as [[0, 'asc']]. Thats why all your rows with
<tr><td><b style="font-size:20px;">—</b></td>...</tr>
is shown first. There is a extension called rowGroup which purpose is exactly what you are trying to do. Include the rowGroup source, then add an extra invisible column to group by :
<tr style="border-bottom:2px solid #000;background: #cee8ff;">
<td><?php echo $reg -> id_codigo;?></td>
<td><?php echo $reg -> id_codigo;?></td>
<td><?php echo utf8_encode($reg ->id_product_term);?></td>
</tr>
...
<tr>
<td><?php echo $reg -> id_codigo;?></td>
<td><b style="font-size:20px;">—</b></td>
<td><?php echo $row -> id_product_term;?></td>
</tr>
Make the new group column invisible and rowGroup that column :
$('#dt_basic').dataTable({
columnDefs: [
targets: 0, visible: false }
],
rowGroup: {
dataSrc: 0
}
...
})
Here is an example of using rowGroup upon an invisible column -> http://jsfiddle.net/8r88gsfk/
Hello i'm stil learning, using Codeigniter can someone tell me, or give example code?
what i need is in Round Id we have 111 i want give it link and search database with value 111 how to do that? here the code i tried but still not right
<div class="row" id="ajaxdata">
<table border="1">
<tr>
<th>Round Id</th>
<th>Player Id</th>
<th>Bet Place</th>
<th>Total Bet</th>
<th>Win</th>
<th>Lose</th>
</tr>
<?php foreach ($tbl_bet_spot as $data) {?>
<tr>
<td><?php echo $data->round_id;?>
<td><?php echo $data->id;?></td>
<td><?php echo $data->bet;?></td>
<td><?php echo $data->total_bet;?></td>
<td><?php echo $data->win;?></td>
<td><?php echo $data->lose;?></td>
</tr>
<?php } ?>
</table>
</table>
</div>
controller
public function detail_round_id(){
$select = $_GET['select'];
$data['tbl_bet_spot'] = $this->login_model->selectRoundId_by_round($select)->result();
print_r ($data);
}
i just try with my code and it work now, but it's static in here
<td><?php echo $data->round_id;?>
how i can send this value <?php echo $data->round_id;?> properly into controller? thanks a lot.
Use this code
<td><?php echo $data->round_id;?></td>
controller
public function detail_round_id(){
$select = $this->uri->segment(3);
$data['tbl_bet_spot'] = $this->login_model->selectRoundId_by_round($select)->result();
print_r ($data);
}
Try this may help you,
In view make link like this,
<td><?php echo $data->round_id;?>
And in controller add parameter like this,
public function detail_round_id($id){
$data['tbl_bet_spot'] = $this->login_model->selectRoundId_by_round($id)->result();
print_r ($data);
}
view page you pass value like this
<?php echo $data->round_id;?>
In controller get value like this
$select=$this->uri->segment(4);
hope this will help
I am trying to display some data from Socrata using Soda API and got the following message
Error "0" from server:
What does this mean?
This is my code:
$socrata = new Socrata("https://data.medicare.gov", $app_token);
$params = array("\$where" => "starts_with(zip, $model->zipcode)");
$response = $socrata->get("/resource/aeay-dfax.json",$params);
?>
<?= Html::encode($model->zipcode) ?>
<h2>Results</h2>
<?# Create a table for our actual data ?>
<table border="1">
<tr>
<th>Last Name</th>
<th>First Name</th>
</tr>
<?# Print rows ?>
<?php foreach($response as $row) { ?>
<tr>
<td><?= $row["lst_nm"] ?></td>
<td><?= $row["fst_nm"] ?></td>
</tr>
<?php } ?>
</table>
<h3>Raw Response</h3>
<pre><?= var_dump($response) ?></pre>
The reason you are getting an error from Socrata is because of a type mismatch between your query and the available data. The zip column is treated as plain text so you must give it a string in your SoQL query as well. Simply wrap $model->zipcode in escaped quotes like so:
$params = array("\$where" => "starts_with(zip, \"$model->zipcode\")");
Otherwise, you'd be giving the query an integer. You could also use single quotes if you don't want to escape the double quotes.
I have got a question regarding setting up proper categories for my posts that would be done automatically. I'm using PHP.
Here is my sample example document from the database:
{
"_id" : "css-clearfix-explained",
"title" : "CSS Clearfix Explained",
"category" : [
"CSS/HTML",
" Wordpress"
],
"date_time" : ISODate("2014-08-13T21:03:45.367Z"),
"description" : "Blalalal ",
"content" : " ",
"author" : "Maciej Sitko"
}
So, for that purpse I wrote the code that inserts the categories in the view page, to be more specific, it inserts them into the table. This is the code:
<?php if(!isset($_GET['cat'])) {
$categories = $collection->find();?>
<table class="table table-hover table-striped">
<thead class='table-head' >
<tr ><th colspan='2'>Categories</th></tr>
</thead>
<tbody>
<?php foreach($categories as $category) {
foreach($category as $keys) {
if((is_array($keys)) && (!empty($keys))) {
foreach($keys as $key => $value) { ?>
<tr>
<td><a class="normalize" href="">
<?php echo $keys[$key]; ?></a></td>
<td class="small"> Posts</td>
</tr>
<?php } } } } ?>
</tbody>
</table>
The problem is, and you see it (I bet it), that when it is executed this way there would be duplicates of the categories in the table shown. How can I prevent those categories from repeating themselves in the listing? I know its a rookie question, but I'm still learning.
You could write $category into an array and every iteration - before displaying your data - you could check if $category is already in there. You could use "in_array": http://php.net/manual/de/function.in-array.php
<?php if(!isset($_GET['cat'])) {
$categories = $collection->find();?>
<table class="table table-hover table-striped">
<thead class='table-head' >
<tr ><th colspan='2'>Categories</th></tr>
</thead>
<tbody>
<?php
$uniqueCats = array();
foreach($categories as $category) {
foreach($category as $keys) {
if((is_array($keys)) && (!empty($keys))) {
foreach($keys as $key => $value) {
if( in_array($value, $uniqueCats) ) { continue; }
$uniqueCats[] = $value;
?>
<tr>
<td><a class="normalize" href="">
<?php echo $value; ?></a></td>
<td class="small"> Posts</td>
</tr>
<?php } } } } ?>
</tbody>
</table>
I hope that's what you were looking for :)
The code/variables slightly differs from how I would read the data in the example so I might have misinterpreted your question :)