How to Catch variable when data no available - php

I wan't to make Number of patient registration queue, I have trouble when data registration for today isn't available.
here is my code :
Controllers :
function index()
{
$this->load->helper('url');
$data['tbl_pendaftaran'] = $this->m_pendaftaran->getData()->result();
$data['pasien']=$this->m_pendaftaran->getPasien();
$data['kpendaftaran']=$this->m_pendaftaran->get_no_pendaftaran();
//status
$data['status'] = $this->m_pendaftaran->status()->result();
$this->template->load('template','pendaftaran/v_pendaftaran',$data);
}
Models :
function status()
{
$this->db->select('*');
$this->db->from('tbl_pendaftaran');
$this->db->join('tbl_pasien', 'tbl_pendaftaran.id_pasien = tbl_pasien.id');
$this->db->where('status','menunggu');
$this->db->limit(1);
return $this->db->get();
}
View :
<?php
foreach ($status as $key ) {
}
$cur = date("Y-m-d H:i:s",time());
if ($key->tgl_berobat<$cur) {
echo "No Patient for Today";
} else {
echo $key->no_antrian;
}
?>
here is result of Error when data for today Isn't available :
Severity: Notice
Message: Undefined variable: key
Filename: views/v_pendaftaran.php
Line Number: 22

Look like you have typing mistake here or you wrongly closed the foreach loop:
<?php
foreach ($status as $key ) {
}
$cur = date("Y-m-d H:i:s",time());
if ($key->tgl_berobat<$cur) {
echo "No Patient for Today";
} else {
echo $key->no_antrian;
}
?>
TRY THIS:
<?php
foreach ($status as $key ) {
$cur = date("Y-m-d H:i:s",time());
if ($key->tgl_berobat < $cur) {
echo "No Patient for Today";
} else {
echo $key->no_antrian;
}
}
?>
To respond to your comment try this way:
<?php
if(isset($status) && !empty($status)){
foreach ($status as $key ) {
$cur = date("Y-m-d H:i:s",time());
if ($key->tgl_berobat < $cur) {
echo "No Patient for Today";
} else {
echo $key->no_antrian;
}
}
} else {
echo "No Patient for Today";
}
//OR
if(isset($status) && !empty($status)){
foreach ($status as $key ) {
$cur = date("Y-m-d H:i:s",time());
if(isset($key->tgl_berobat) && !empty($key->tgl_berobat)){
if ($key->tgl_berobat < $cur) {
echo "No Patient for Today";
} else {
echo $key->no_antrian;
}
} else {
echo "No Patient for Today";
}
}
} else {
echo "No Patient for Today";
}
?>

I think the trouble is in your View.
Just change the code to
<?php
foreach ($status as $key ) {
$cur = date("Y-m-d H:i:s",time());
if ($key->tgl_berobat<$cur) {
echo "No Patient for Today";
} else {
echo $key->no_antrian;
}
}
?>

In your controller do not call result function, cause maybe there is no an object to result it.
function index()
{
.
.
.
//status
$data['status'] = $this->m_pendaftaran->status();
$this->template->load('template','pendaftaran/v_pendaftaran',$data);
}
In your check first if object exist or not:
<?php
if($status->num_rows()>0)
{
foreach ($status->result() as $key ) {
$cur = date("Y-m-d H:i:s",time());
if ($key->tgl_berobat<$cur) {
echo "No Patient for Today";
} else {
echo $key->no_antrian;
}
}
}
?>

Related

Several nearly identical if statements need to be shortened

So I've got this code...
code:
if(empty($day0[2])) {
echo "<td>".$day0[1]."<br></td>";
} else {
if(strcmp($day0[1],"Absent") == 0) {
echo "<td>".$day0[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day0[1]."<br>Time: ".#$day0[2]." - ".#$day0[3]."</td>";
}
}
if(empty($day1[2])) {
echo "<td>".$day1[1]."<br></td>";
} else {
if(strcmp($day1[1],"Absent") == 0) {
echo "<td>".$day1[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day1[1]."<br>Time: ".#$day1[2]." - ".#$day1[3]."</td>";
}
}
if(empty($day2[2])) {
echo "<td>".$day2[1]."<br></td>";
} else {
if(strcmp($day2[1],"Absent") == 0) {
echo "<td>".$day2[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day2[1]."<br>Time: ".#$day2[2]." - ".#$day2[3]."</td>";
}
}
if(empty($day3[2])) {
echo "<td>".$day3[1]."<br></td>";
} else {
if(strcmp($day3[1],"Absent") == 0) {
echo "<td>".$day3[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day3[1]."<br>Time: ".#$day3[2]." - ".#$day3[3]."</td>";
}
}
if(empty($day4[2])) {
echo "<td>".$day4[1]."<br></td>";
} else {
if(strcmp($day4[1],"Absent") == 0) {
echo "<td>".$day4[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day4[1]."<br>Time: ".#$day4[2]." - ".#$day4[3]."</td>";
}
}
if(empty($day5[2])) {
echo "<td>".$day5[1]."<br></td>";
} else {
if(strcmp($day5[1],"Absent") == 0) {
echo "<td>".$day5[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day5[1]."<br>Time: ".#$day5[2]." - ".#$day5[3]."</td>";
}
}
if(empty($day6[2])) {
echo "<td>".$day6[1]."<br></td>";
} else {
if(strcmp($day6[1],"Absent") == 0) {
echo "<td>".$day6[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day6[1]."<br>Time: ".#$day6[2]." - ".#$day6[3]."</td>";
}
}
Where day 0-6 equals Sunday through Saturday, and the array numbers attached to each one equals a different variable in a multi-dim array.
That is several if statements that are all exactly the same except the variable name inside of each one. I haven't been able to find a way to make this shorter, so I thought I would post here to try and see if anyone has any ideas on how I can combine this into shorter lines of code. I'm all about my code looking neater and functioning better, and I think this could teach a lot of people good ways to shorten their code down a bit.
First merge all array of $day[n] in to $finalArray
foreach($finalArray as $key=>$value){
if(empty($value[2])) {
echo "<td>".$value[1]."<br></td>";
} else {
if(strcmp($value[1],"Absent") == 0) {
echo "<td>".$value[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$value[1]."<br>Time: ".#$value[2]." - ".#$value[3]."</td>";
}
}
}
#MagnusEriksson suggested making a function, I think this is the best way to do it.
From 69 lines of code to 18 lines of code.
function displayTime($day1,$day2,$day3) {
if(empty($day2)) {
return "<td>{$day1}<br></td>";
} else {
if(strcmp($day1,"Absent") == 0) {
return "<td>{$day1}<br>Time: N/A</td>";
}
return "<td>{$day1}<br>Time: {$day2} - {$day3}</td>";
}
}
for ($x = 0; $x <= 6; $x++) {
echo displayTime(${"day$x"}[1],${"day$x"}[2],${"day$x"}[3]);
}
Please try with this code.
$array=array($day0,$day1,$day2,$day3);
for($i=0;$i<count($array);$i++){
if(empty($day.$i[2])) {
echo "<td>".$day.$i[1]."<br></td>";
} else {
if(strcmp($day.$i[1],"Absent") == 0) {
echo "<td>".$day.$i[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day.$i[1]."<br>Time: ".#$day.$i[2]." - ".#$day.$i[3]."</td>";
}
}
}

how to unset a session variable?

I have the following code, but it does not work, I don't know what mistake I am committing due to which this code does not work:
Here is the code:
<?php session_start();
if (isset($_GET['indexNo']) && is_numeric($_GET['indexNo']) && !empty($_GET['indexNo']))
{
$indx = $_GET['indexNo'];
foreach($_SESSION['itemsOrder'] as $key => $val)
{
echo "$key => $val <br> " ;
if($indx == $val)
{
unset($_SESSION['itemsOrder'][$val]);
}
else
{
echo "indexNo was not unset <br>";
}
}
}
else
{
echo "indexNo not received!";
}
?>
Should be $key not the $val . Try with -
if($indx == $key)
{
unset($_SESSION['itemsOrder'][$key]);
}
No need to use isset & empty together.
if (isset($_GET['indexNo']) && is_numeric($_GET['indexNo']))
You need to unset the session array value than you must use its key instead of value.
replaced:
unset($_SESSION['itemsOrder'][$val]);
with:
unset($_SESSION['itemsOrder'][$key]);
**Try This Code**
<?php session_start();
if (isset($_GET['indexNo']) && is_numeric($_GET['indexNo']))
{
$indx = $_GET['indexNo'];
foreach($_SESSION['itemsOrder'] as $key => $val)
{
echo "$key => $val <br> " ;
if($indx == $val)
{
unset($_SESSION['itemsOrder'][$key]);
}
else
{
echo "indexNo was not unset <br>";
}
}
}
else
{
echo "indexNo not received!";
}

how to sort SQL result in an existing sorting function

Hello I have this sorting function ine one template of CMS. I would like to add if the condition is the second one:
if(is_array($array)) {
foreach($array as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.content_item_photo.php');
}
I want to add the following MySQL result to show also
$result = mysql_query("SELECT niches.name, niches.record_num FROM niches ORDER BY name ASC");
which is used in this template for sorting:
include($basepath.'/templates/template.channel_item_title.php');
Any ideas how this can be integrated inside the first code function?
So here is the code of the main index template:
<?
session_start();
if(($_REQUEST[mode] == 'favorites' || $_REQUEST[mode] == 'my_uploads') && !$_SESSION[username]) {
header("Location: /login.php");
}
include('admin/db.php');
include($basepath.'/includes/inc.seo.php');
$cacheName = $_SERVER[REQUEST_URI];
$cacheResult = getCache($cacheName);
$cacheTotalPages = getCache("total_pages$cacheName");
if($cacheResult && $cacheTotalPages) {
$array = $cacheResult;
$total_pages = $cacheTotalPages;
}
else {
include($basepath.'/includes/inc.index_queries.php');
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
if($thispage != 'favorites' && $_GET[mode] != 'my_uploads') {
setCache($cacheName,$array,$overall_cache_time);
setCache("total_pages$cacheName",$total_pages,$overall_cache_time);
}
}
$thisfile = 'index';
$webpage="index";
if($isMobile) {
include($basepath.'/templates/mobile.overall_header.php');
}
else {
include($basepath.'/templates/template.overall_header.php');
}
if(empty($_GET[mode]) && !$_GET[page]) {
include($basepath.'/templates/template.home.php');
}
if($webpage=="index" && empty($_GET[mode]) && !$_GET[page])
{}
else
{
if(is_array($array)) {
foreach($array as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.content_item_photo.php');
}
}
else {
if($isMobile) {
include($basepath.'/templates/mobile.content_item.php');
}
else
{
include($basepath.'/templates/template.content_item.php');
}
}
}
}
else {
echo "Sorry, no results were found.";
}
}
if($isMobile) {
include($basepath.'/templates/mobile.overall_footer.php');
}
else {
include($basepath.'/templates/template.overall_footer.php');
}
?>
I don't think I get your question, but we have to start somewhere...
<?php
if(is_array($array)) {
foreach($array as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.content_item_photo.php');
}
Next block of code:
<?php
$result = mysql_query("SELECT niches.name, niches.record_num FROM niches ORDER BY name ASC");
if(is_array(result)) {
foreach($result as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.channel_item_title.php');
}

count conditional output in foreach loop

I have foreach loop like this
foreach($destinations as $destination)
{
if($destination=="abc")
{
$msg = "Yes";
}
else
{
$msg = "No";
}
}
How can I count number of "Yes" and "No" generated by "if statement" outside "foreach loop"?
Try:
$yes = 0;
$no = 0;
foreach($destinations as $destination)
{
if($destination=="abc")
{
$yes += 1;
}
else
{
$no += 1;
}
}
echo "Yes " . $yes . "<br>" . "No " . $no;
Inside 'if' statement you can try this
$yesCount = 0;
$noCount = 0;
foreach($destinations as $destination)
{
if($destination=="abc")
{
$yesCount++;
$msg = "Yes";
}
else
{
$noCount++;
$msg = "No";
}
}
But i am not sure whether it can be used outside.
$yesCount = 0;
$noCount = 0;
foreach($destinations as $destination) {
if($destination=="abc") {
$msg = "Yes";
$yesCount = $yesCount + 1;
}
else {
$msg = "No";
$noCount = $noCount + 1;
}
}
echo $yesCoynt . " --- " . $noCount;
Just try with the following example :
<?php
$destinations = array('abc','def','ghi');
foreach($destinations as $destination)
{
if($destination=="abc")
{
$msg = "Yes";
$msg_yes_counter[]= "I'm in";
}
else
{
$msg = "No";
$msg_no_counter[]= "I'm in";
}
}
echo "YES -> My Count is :".count($msg_yes_counter);
echo "NO -> My Count is :".count($msg_no_counter);
?>
Create two flag variables and try this
$yesFlag=0;
$noFlag=0;
foreach($destinations as $destination)
{
if($destination=="abc")
{
$msg = "Yes";
$yesFlag++;
}
else
{
$msg = "No";
$noFlag++;
}
}
echo "no. of Yes:".yesFlag;
echo "no. of NO:".noFlag;
Use the array_count_values() function, no need for a loop at all then.
array_count_values($destinations);

pass value from embedded function into conditional of page the embedded function is included on

I have a page that includes/embeds a file that contains a number of functions.
One of the functions has a variable I want to pass back onto the page that the file is embedded on.
<?php
include('functions.php');
userInGroup();
if($user_in_group) {
print 'user is in group';
} else {
print 'user is not in group';
}
?>
function within functions.php
<?php
function userInGroup() {
foreach($group_access as $i => $group) {
if($group_session == $group) {
$user_in_group = TRUE;
break;
} else {
$user_in_group = FALSE;
}
}
}?>
I am unsure as to how I can pass the value from the function userInGroup back to the page it runs the conditional if($user_in_group) on
Any help is appreciated.
Update:
I am userInGroup(array("STAFF","STUDENTS","FACULTY"));
which then is
<?php
function userInGroup($group_access) {
session_start();
if(isset($_SESSION['user_session'])) {
$username = $_SESSION['user_session'];
$group_session = $_SESSION['group_session'];
$user_full_name = $_SESSION['user_full_name'];
foreach($group_access as $i => $group) {
if($group_session == $group) {
$user_in_group = TRUE;
break;
} else {
$user_in_group = FALSE;
}
} return $user_in_group;
} else {
print 'not logged in';
}
?>
Easiest way:
$user_in_group = userInGroup();
function userInGroup() {
foreach($group_access as $i => $group) {
if($group_session == $group) {
$user_in_group = TRUE;
break;
} else {
$user_in_group == FALSE;
}
}
return $user_in_group;
}
Use the return statement.
You can use your original function with one minor modification:
<?php
function userInGroup() {
**global $user_in_group;**
foreach($group_access as $i => $group) {
if($group_session == $group) {
$user_in_group = TRUE;
break;
} else {
$user_in_group = FALSE;
}
}
}?>

Categories