PHP Script Snippet (only one holiday):
<?PHP
function calculateBankHolidays($yr) {
$bankHols = Array();
// New year's:
switch ( date("w", strtotime("01-01-$yr 12:00:00")) ) {
case 6:
$bankHols[] = "03-01-$yr";
break;
case 0:
$bankHols[] = "02-01-$yr";
break;
default:
$bankHols[] = "01-01-$yr";
}
// Good friday:
$bankHols[] = date("d-m-y", strtotime( "+".(easter_days($yr) - 2)." days", strtotime("21-03-$yr 12:00:00") ));
// Easter Monday:
$bankHols[] = date("d-m-y", strtotime( "+".(easter_days($yr) + 1)." days", strtotime("21-03-$yr 12:00:00") ));
// May Day:
if ($yr == 1995) {
$bankHols[] = "08-05-1995"; // VE day 50th anniversary year exception
} else {
switch (date("w", strtotime("01-05-$yr 12:00:00"))) {
case 0:
$bankHols[] = "02-05-$yr";
break;
case 1:
$bankHols[] = "01-05-$yr";
break;
case 2:
$bankHols[] = "07-05-$yr";
break;
case 3:
$bankHols[] = "06-05-$yr";
break;
case 4:
$bankHols[] = "05-05-$yr";
break;
case 5:
$bankHols[] = "04-05-$yr";
break;
case 6:
$bankHols[] = "03-05-$yr";
break;
}
}
return $bankHols;
}
header('Content-Type: application/json');
$bankHolsThisYear = calculateBankHolidays(2017);
echo (json_encode($bankHolsThisYear, JSON_PRETTY_PRINT));
?>
Result:
[
"02-01-2017",
"14-04-17",
"17-04-17",
"01-05-2017",
"2017-05-29",
"2017-08-28",
"2017-12-25",
"2017-12-26"
]
Shows current full scripts results
Desired Outcome:
{
"Holiday Name": {
"Start Date": ,
"End Date": ,
"Holiday type": ,
"Where it is observed": ,
},
Questions:
How do I add a "Holiday Name" to the parent of each value?
How do I add "Start Date" to each current value?
You could create an multidimensional associative array of objects and do something like:
$listOfHolidays=array(
'halloween'=>array('start'=>'10-31','end'=>'10-31','type'=>'trick or treat','celebratedBy'=>'childhood'),
'newYear'=>array('start'=>'12-31','end'=>'01-01','type'=>'new year','celebratedBy'=>'everyone'),
);
echo json_encode($listOfHolidays);
Tested: this is my output:
{
"halloween":
{"start":"10-31",
"end":"10-31",
"type":"trick ortreat",
"celebratedBy":"childhood"
},
"newYear":
{"start":"12-31",
"end":"01-01",
"type":"new year",
"celebratedBy":"everyone"
}
}
EDIT: as you commented about a switch, i'm not sure i understand the precision but you can easily get the 'holidays' by using the associative keys like so:
$boo=$array['halloween'];
And then get the value from this holiday with:
$boo['type']; //trick ortreat
OR you could alternativly get the value straight from the original array:
echo $array['newYear']['end']; //01-01
further more you can also add a value to the array:
$array['newYear']['bonus']='300$';
Also, just a friendly reminder that you can resotre the aray from jason simply by using the TRUE switch in json_decode like so:
$array=json_decode($json,true);
As for the switch, still i don'T see how you could be using a switch unless you loop through the holidays:
foreach($array as $k=>$v){
switch($k){
case 'halloween': echo $v['end']; break; //10-31
case 'newYear': echo $v['bonus']; break; //300$
default: echo 'normal work day'; break;
}
}
Hope this helps.
Related
I'm trying to display some content based on a hour.
This hour is coming from DB as string field.
If the current time match or passed the time from DB, a certain content should be displayed else it's the default content
What i've tried so far :
$time = strtotime($p_objetct->getHour());
//we check if this has already passed to
//retrieve corresponding content else we display default content
if ($time >= date("h:i:sa")) {
echo 'time to display new content !';
}else{
echo 'display content default';
}
This code is not working well as you can guess 'cause if I want to display new content at 20:00:00, the 1st condition is true when the current time is 08:00:00..
I've also tried that
$time = strtotime(p_objetct->getHour());
$selection = floor((date('G',$time) / 2));
switch ($selection) {
case 0:
echo 'time between 0-2!';
break;
case 1:
echo 'time between 2-4!';
break;
case 2:
echo 'time between 4-6!';
break;
case 3:
echo 'time between 6-8!';
break;
case 4:
echo 'time between 8-10!';
break;
case 5:
echo 'time between 10-12!';
break;
case 6:
echo 'time between 12-14!';
break;
case 7:
echo 'time between 14-16!';
break;
case 8:
echo 'time between 16-18!';
break;
case 9:
echo 'time between 18-20!';
break;
case 10:
echo 'time between 20-22!';
break;
case 11:
echo 'time between 22-24!';
break;
}
But this wasnt the good approach because i cant guess in which case the time from DB will be..
Any ideas are welcomed !
Edit :
I have 2 contents (new and default).
The new content should be display from time from DB until 00:00:00 then it should be the default one
If you get only the hour in 24h format from your $p_objetct->getHour() method you can simply compare it to date('H');.
If you wat exact matches you can use:
<?php
$hourToCheck = $p_objetct->getHour();
$currentHour = date('H'); // As of PHP manual "H" returns the hour in 24h format
$contentToDisplay = "What ever you wanna display as default.";
if ($hourToCheck === $currentHour) {
$contentToDisplay = "Your matching content.";
}
If you need it as a lower bound time feel free to use and change this snippet:
<?php
$hourToCheck = $p_objetct->getHour();
$currentHour = date('H'); // As of PHP manual "H" returns the hour in 24h format
$contentToDisplay = "What ever you wanna display as default.";
if ($hourToCheck <= $currentHour) {
$contentToDisplay = "Your matching content 1.";
}
You could try to set a variable with a name based on time, matching an existing file with that name and require it with php. Like this:
<?php
$time = date('H',time());
$name_of_content_to_require = false;
switch ($time) {
case 0:
$name_of_content_to_require = '0-2';
break;
case 1:
$name_of_content_to_require = '2-4';
break;
case 2:
$name_of_content_to_require = '4-6';
break;
case 3:
$name_of_content_to_require = '6-8';
break;
case 4:
$name_of_content_to_require = '8-10';
break;
case 5:
$name_of_content_to_require = '10-12';
break;
case 6:
$name_of_content_to_require = '12-14';
break;
case 7:
$name_of_content_to_require = '14-16';
break;
case 8:
$name_of_content_to_require = '16-18';
break;
case 9:
$name_of_content_to_require = '18-20';
break;
case 10:
$name_of_content_to_require = '20-22';
break;
case 11:
$name_of_content_to_require = '22-24';
break;
}
if( $name_of_content_to_require ) {
require_once __DIR__ . $name_of_content_to_require . '.php';
} else {
die('with some error, or throw an exception');
}
How can I correct/simplify this and put it in an array?
A link is passing: somelink.php?w=a (or b,c,d)
I want the page (somelink.php) to determine if "w" is set, and if set and the var matches, include the specified page.
<?php
if(isset($_GET['w'])&&($GET['w'] == "a")){include("1.htm");}
if(isset($_GET['w'])&&($GET['w'] == "b")){include("2.htm");}
if(isset($_GET['w'])&&($GET['w'] == "c")){include("3.htm");}
if(isset($_GET['w'])&&($GET['w'] == "d")){include("4.htm");}
else{include("1.htm");}
?>
try using:
$w = $_GET['w'];
if(isset($w)) {
switch(strtolower($w)) {
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
default:
include("not-found.htm");
break;
}
}
Use a switch statement:
if(isset($_GET['w']))
{
switch($_GET['w'])
{
case 'a': include("1.html"); break;
case 'b': include("2.html"); break;
case 'c': include("3.html"); break;
case 'd': include("4.html"); break;
default: include("1.html"); break;
}
} else {
include("1.html");
}
how about a simple array
$x=array('a'=>'1.html','b'=>'2.html');
then
include $x[$GET['w']];
Like this:
if(isset($_GET['w'])){
switch($_GET['w']){
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
}
}
But I wouldn't do it that way. I'd make it so that the name of the page corresponds to the value being retrieved from the $_GET variable. That way you could do something like this.
if(!empty($_GET['w'])){
include($_GET['w'] . ".htm");
}
Of course, you'd want a little filtering of the $_GET var too to make sure it doesn't get something you don't want there. Maybe like this.
$acceptable_values = array("some","acceptable","values");
if(!empty($_GET['w']) && in_array($_GET['w'],$acceptable_values) ){
include($_GET['w'] . ".htm");
}
As I'm sure you are aware, passing variables directly into include statements or database queries is a TERRIBLE idea. See here for why in this case.
http://websec.wordpress.com/2010/02/22/exploiting-php-file-inclusion-overview/
You could do a few things, lets take a look at some of them.
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
switch($webpage)
{
case 'b':
include '2.html';
break;
case 'c':
include '3.html';
break;
case 'd':
include '4.html';
break;
default:
include '1.html';
break;
}
Or we could use arrays
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
$included_pages = array(
'a' => '1.htm',
'b' => '2.htm',
'c' => '3.htm',
'd' => '4.htm',
);
// Check inside our array
if(array_key_exists($webpage, $includes))
{
include $included_pages[$webpage];
}
else
{
// Couldn't find the site
include '1.htm';
}
is it possible to simplify this code? I am trying to put all cases in a switch but always d break in the first case and i need all echo's in the html. What is possible? Thank you!
$resultservices = mysqli_query($connecDB,"SELECT * FROM clients WHERE id_client = $id_client");
while($rowservice = mysqli_fetch_array($resultservices)){
$php = (int)$rowservice['php'];
$java = (int)$rowservice['java'];
$ruby = (int)$rowservice['ruby'];
$node = (int)$rowservice['node'];
}
// Values can be "1" or "0". Example: php:1, java:1, ruby:0, node:1
switch ($php) {
case 0: break;
case 1: echo "<li>php</li>"; break;
}
switch ($java) {
case 0: break;
case 1: echo "<li>java</li>"; break;
}
switch ($ruby) {
case 0: break;
case 1: echo "<li>ruby</li>"; break;
}
switch ($node) {
case 0: break;
case 1: echo "<li>node</li>"; break;
}
While I'm not sure what you're trying to do, how about:
$resultservices = mysqli_query($connecDB,"SELECT * FROM clients WHERE id_client = $id_client");
while($rowservice = mysqli_fetch_array($resultservices)){
$service[1] = (int)$rowservice['1'];
$service[2] = (int)$rowservice['1'];
$service[3] = (int)$rowservice['0'];
$service[4] = (int)$rowservice['1'];
}
foreach ($service as $k=>$v) {
if ($v) {
echo "<li>service".$k."</li>";
}
}
[edit] I see we've got some new variables.
while($rowservice = mysqli_fetch_array($resultservices)){
$service['php'] = (int)$rowservice['php'];
$service['java'] = (int)$rowservice['java'];
$service['ruby'] = (int)$rowservice['ruby'];
$service['node'] = (int)$rowservice['node'];
}
foreach ($service as $k=>$v) {
if ($v) {
echo "<li>".$k."</li>";
}
}
Although really, all you're doing is outputting the last row of your MySQL, so you could also do
$resultservices = mysqli_query($connecDB,"SELECT * FROM clients WHERE id_client = '".mysqli_real_escape_string($connecDB, $id_client)."' ORDER BY id DESC LIMIT 1");
while($rowservice = mysqli_fetch_array($resultservices)){
if ($rowservice['php']) {
echo "<li>php</li>"
}
if ($rowservice['java']) {
echo "<li>java</li>"
}
if ($rowservice['ruby']) {
echo "<li>ruby</li>"
}
if ($rowservice['node']) {
echo "<li>node</li>"
}
}
Given that 3 of your four service values are going to have the SAME value, you could eliminate 2 of the switches and end up with the same results:
$service01 = (int)$rowservice['1'];
$service02 = (int)$rowservice['1']; // identical to service01
$service03 = (int)$rowservice['0'];
$service04 = (int)$rowservice['1']; // identical to service01
meaning you could have:
switch($service01) {
case 0: break;
case 1: echo "<li>service01, 02, and 04</li>"; break;
}
And then, assuming these values will never be anything but true/false 0/1 values, you could eliminate the switches entirely and go with a conventional if:
if ($service01) {
echo "service 01, 02 and 04";
}
here is my code :
<?php
// content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
include("mysql _connect .php");
$code="CSC1113";
$ac_yr="2010/2011";
$sql = "SELECT results, COUNT(*) 'No_Of_grades' FROM std_results WHERE code='$code' && ac_year='$ac_yr' GROUP BY results ORDER BY results ASC";
$result = mysql_query($sql) or die(mysql_error());
while($ors = mysql_fetch_array($result)) {
$grd=$ors['results'];
switch ($grd)
{
case "A":
$datay[1]=$ors['No_Of_grades'];
break;
case "A+":
$datay[0]=$ors['No_Of_grades'];
break;
case "A-":
$datay=$ors['No_Of_grades'];
break;
case "B":
$datay[4]=$ors['No_Of_grades'];
break;
case "B+":
$datay[3]=$ors['No_Of_grades'];
break;
case "B-":
$datay[5]=$ors['No_Of_grades'];
break;
case "C":
$datay[7]=$ors['No_Of_grades'];
break;
case "C+":
$datay[6]=$ors['No_Of_grades'];
break;
case "C-":
$datay[8]=$ors['No_Of_grades'];
break;
case "D":
$datay[10]=$ors['No_Of_grades'];
break;
case "D+":
$datay[9]=$ors['No_Of_grades'];
break;
case "E":
$datay[11]=$ors['No_Of_grades'];
break;
case "AB":
$datay[12]=$ors['No_Of_grades'];
break;
case "NE":
$datay[13]=$ors['No_Of_grades'];
break;
default:
$datay[14]=$ors['No_Of_grades'];
}
}
//set vlaue zero for othe grades..
for($i=0;$i<15;$i++){
if(!isset($datay[$i])){
$datay[$i]=0;
}
}
// Create the graph. These two calls are always required
$graph = new Graph(550,320,'auto');
$graph->SetScale("textlin");
//$theme_class="DefaultTheme";
//$graph->SetTheme(new $theme_class());
// set major and minor tick positions manually
$graph->yaxis->SetTickPositions(array(0,4,8,12,16,20), array(2,6,10,14,18));
$graph->SetBox(false);
//$graph->ygrid->SetColor('gray');
$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array('A+','A','A-','B+','B','B-','C+','C','C-','D+','D','E','AB','NE','MC'));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);
// Create the bar plots
$b1plot = new BarPlot($datay);
// ...and add it to the graPH
$graph->Add($b1plot);
$b1plot->SetColor("white");
$b1plot->SetFillGradient("#4B0082","white",GRAD_LEFT_REFLECTION);
$b1plot->SetWidth(25);
$graph->title->Set("Bar Gradient(Left reflection)");
// Display the graph
$graph->Stroke();
?>
here jpgrpah doesn't display..error shows : JpGraph Error: 25067 Your manually specified scale and ticks is not correct. The scale seems to be too small to hold any of the specified tick marks.
but I gave direct data for above $datay array.this code is working perfectly..like this
$datay[0]=2;
$datay[1]=5;
$datay[2]=1;
$datay[3]=2;
$datay[4]=0;
$datay[5]=0;
$datay[6]=3;
$datay[7]=0;
$datay[8]=3;
$datay[9]=0;
$datay[10]=1;
$datay[11]=1;
$datay[12]=0;
$datay[13]=1;
$datay[14]=1;
wht's the wrong with my code ....can't understand....help me...thanxx in advanced...
I think now that i understand you :
case "A-":
$datay=$ors['No_Of_grades'];
break;
should be:
case "A-":
$datay['2']=$ors['No_Of_grades'];
break;
I want use of switch and case in php and codeigniter library, i try it as following code, But I not receive output. what do i do?
Demo: http://codepad.viper-7.com/Wq0Noj
function indicators() {
$CI = &get_instance();
$Year = '1355';
$Month = '03';
switch ($Year) {
case 1354:
$key=array('0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6');
$output = $key[$Month-1];
break;
case 1355:
$key=array('0.6','0.7','0.2','0.4','0.7','0.1','0.7','0.2','0.5','0.9','0.4','0.8');
$output = $key[$Month-1];
break;
echo $output; // The output should be: 0.7
}
}
I think your echo needs to be outside of the switch as well... checking to verify.
Yep, the echo needs to be outside. The type should actually be coerced when comparing.
<?php
$s = '5';
switch ($s) {
case 5:
echo "Foo\n";
break;
default:
echo "Bar\n";
break;
}
echo $s;
OUTPUT
Foo
5
And for your example:
<?php
function indicators() {
$Year = '1355';
$Month = '03';
switch ($Year) {
case 1354:
$key=array('0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6');
$output = $key[$Month-1];
break;
case 1355:
$key=array('0.6','0.7','0.2','0.4','0.7','0.1','0.7','0.2','0.5','0.9','0.4','0.8');
$output = $key[$Month-1];
break;
}
echo $output; // The output should be: 0.7
}
indicators();
OUTPUT
0.2
Which is correct according to the code. '03' - 1 == 2. $key[2] == '0.2'
As pointed out in the comment below by #vstm, the docs state that the "switch/case does loose comparision."