This code is supposed to output something like:
You are, at this moment, living in the 11th second of the 2nd minute of the 3rd hour of the 9th day of the 5th month of the 2017th year since the begining of the International calender.
Instead it outputs this: https://prnt.sc/fli92p
Have no idea what the problem is.
date_default_timezone_set(//location...);
say_time();
function say_time()
{
$o = ' of the';
class time_value
{
public $t, $name, $display;
protected $n, $suf;
function __construct()
{
$this->display = " $this->n"."$this->suf"." $this->name";
$this->n = date($this->t,time());
switch ($this->n)
{
case 1:
$suf = 'st';
break;
case 2:
$suf = 'nd';
break;
case 3:
$suf = 'rd';
break;
default:
$suf = 'nth';
break;
}
}
}
$sec = new time_value;
$sec->t = 's';
$sec->name = 'seconds';
$min = new time_value;
$min->t = 'i';
$min->name = 'minutes';
$hr = new time_value;
$hr->t = 'G';
$hr->name = 'hours';
$day = new time_value;
$day->t = 'j';
$day->name = 'days';
$mon = new time_value;
$mon->t = 'n';
$mon->name = 'months';
$yr = new time_value;
$yr->t = 'Y';
$yr->name = 'years';
echo "You are, at this moment, living in the "
.$sec->display .$o
.$min->display .$o
.$hr ->display .$o
.$day->display .$o
.$mon->display .$o
.$yr ->display .
" since the begining of the International calender.";
echo $sec->display;
}
The line:
$this->display = " $this->n"."$this->suf"." $this->name";
is the first line of the class' constructor. It stores in the $display property of the object a string that contains only spaces because the values it contains are not set yet.
Read about double-quotes strings and variables parsing inside double-quotes strings.
In order to work, the class time_value should be like this:
class time_value
{
private $t, $name, $display;
public function __construct($t, $name)
{
$this->t = $t;
$this->name = $name;
$n = date($this->t, time());
switch ($n)
{
case 1:
$suf = 'st';
break;
case 2:
$suf = 'nd';
break;
case 3:
$suf = 'rd';
break;
default:
$suf = 'th';
break;
}
$this->display = " {$n}{$suf} {$this->name}";
}
public function display() { return $this->display; }
}
$sec = new time_value('s', 'seconds');
$min = new time_value('i', 'minutes');
// all the other time components here...
echo $sec->display().$o.$min->display(); // ...
The next step toward object-oriented programming is to encapsulate the generation of all time components into the time_value class (or into another class that uses time_value instances, if you like it more) and have the code of function say_time() look like this:
function say_time()
{
$time = new time_value();
echo "You are, at this moment, living in the ".$time->display("of the")." since the begining of the International calender.";
}
Related
I am trying to insert data into database by checking the days in a week but I am unable to insert if I check only one day in a week, if I check two or more days I am able to insert.
<?php
//other parameters
$days_only = $_REQUEST['days_only'];
using an array to split days in a week
$day_array = explode(",",$days_only);
$count_sample = count($day_array);
if($count_sample==1) {
$in_sample='('.$days_only.')';
}
elseif($count_sample>0) {
$in_sample="('".str_replace(",","','" , $days_only)."')";
}
$test='IN'.$in_sample;
$sth = "SELECT e_id FROM `schedule` WHERE
user_id = '$user_id' AND
date BETWEEN '$start_date' AND '$end_date' AND
(
TIME(start_time) >= '$start_time' AND
TIME(start_time) <= '$end_time' OR
TIME(end_time) >= '$start_time' AND
TIME(end_time) <= '$end_time'
) AND days_only $test";
$res = $db->prepare($sth);
$res->execute();
$count = $res->fetchAll();
if ($count) {
echo "すでにスケジュールが登録されています";
} else {
for($i = strtotime($start_date); $i <= strtotime($end_date); $i +=86400 ){
$date = date("Y-m-d", $i);
$days_only = date('l', $i);
switch ($days_only) {
case 'Monday': $days_only = "月"; break;
case 'Tuesday': $days_only = "火"; break;
case 'Wednesday': $days_only = "水"; break;
case 'Thursday': $days_only = "木"; break;
case 'Friday': $days_only = "金"; break;
case 'Saturday': $days_only = "土"; break;
case 'Sunday': $days_only = "日"; break;
}
echo "$date";
echo "$days_only";
if (in_array($days_only,$day_array)){
query for inserting into database
}
else{
echo "wrong";
}
}
}
?>
Here's the issue:
if( $count_sample==1 ) {
$in_sample='('.$days_only.')';
}
The string that you're passing in $days_only is not being escaped here, but it is when the $count_sample value is greater than one. The above code will give you something like this: (金)
Perhaps you can replace the two outermost single-quotes with a double-quote and drop the concat period:
$in_sample= "('$days_only')";
Or you can replace a good bit of code with a single implode():
if ( $count_sample > 0) {
$in_sample="('" . implode("','", $days_only) . "')";
}
Be sure to remember to add validation to your inputs before sending this to the database, otherwise you leave yourself open to SQL injection 👍🏻
Hello im trying to make an object as learning task which takes a number and calculate it three times and echos every step.
<?
// neues Objekt der Klasse erzeugen
$stringManager = new StringManager();
// output String initialisieren
$meinString = "";
$zahl = "100";
$stringManager->setMeinString($meinString);
$stringManager->setZahl($zahl);
// Schritte ausführen
for ($i=1; $i<=3; $i++) {
$stringManager->machSchritt($i,$zahl);
}
// String ausgeben
echo $stringManager->getMeinString();
// Klasse StringManager
class StringManager {
var $meinString;
var $zahl;
function StringManager() {
}
function machSchritt($welchenSchritt,$zahl) {
switch ($welchenSchritt) {
case 1:
$zahl + 50;
break;
case 2:
$zahl / 2;
break;
case 3:
$zahl * 5;
break;
default:
break;
}
$this->append("schritt".$welchenSchritt." fertig...");
$this->append("Zahl:".$zahl." ");
}
function append($what) {
$this->meinString .= $what;
}
//function append($what) {
//$this->zahl .= $what;
//}
function setMeinString($value) {
$this->meinString = $value;
}
function getMeinString() {
return $this->meinString;
}
function setZahl($value) {
$this->zahl = $value;
}
function getZahl() {
return $this->zahl;
}
}
?>
My output is: "schritt1 fertig...Zahl:100 schritt2 fertig...Zahl:100 schritt3 fertig...Zahl:100"
But I expect it to be "schritt1 fertig...Zahl:150 schritt2 fertig...Zahl:75 schritt3 fertig...Zahl:375
Please help me find what im doing wrong.
your problem is between this lines:
$zahl + 50;
break;
case 2:
$zahl / 2;
break;
case 3:
$zahl * 5;
if you want to increase/manipulate the variable $zahl so you have to use it:
$zahl += 50;
break;
case 2:
$zahl /= 2;
break;
case 3:
$zahl *= 5;
btw: your code is deprecated.
class StringManager {
var $meinString;
var $zahl;
function StringManager() {
}
}
should replace with
class StringManager {
private $meinString;
private $zahl;
public function __construct() {
}
}
update:
ah, now i understand, you need to work with your value.
so use this instead:
$this->zahl += 50;
break;
case 2:
$this->zahl /= 2;
break;
case 3:
$this->zahl *= 5;
$this->append("Zahl:".$this->zahl." ");
But it would be better, to use your setter (with typecasting) and getter methods.
warning
your code looks like php4 code. php4 is not supported since 2008 (http://php.net/eol.php).
I want to transform query string like pending, shipped or cancel to number status.
$q = strtolower($keyword);
if($q == 'pen' or $q == 'pend' or $q == 'pending' ) {
$d = 1;
} elseif($q == 'shi' or $q == 'ship' or $q == 'shipped') {
$d = 2;
} elseif($q == 'can' or $q == 'cancel' ) {
$d = 3;
} else {
$d = 4;
}
$query->whereStatus($d);
Current query working fine but too much or. It's possible to do in shorten way?
str_is(query, stringToSearch) will probably be enough:
if (str_is('pen*', $q)) {
$d = 1;
}
Else you could parse them from arrays:
$pendingArray = ['pen', 'pend', 'pending'];
if (in_array($q, $pendingArray)) {
$d = 1;
}
If these are all the conditions you need, you could always use a switch.
$q = strtolower($keyword);
$d = 4;
switch($q) {
case 'pen':
case 'pend':
case 'pending':
case 'pen':
$d = 1;
break;
case 'shi':
case 'ship':
case 'shipped':
$d = 2;
break;
case 'can':
case 'cancel':
$d = 3;
break;
}
$query->whereStatus($d);
If this needs to be called on a model, it could be saved to a Laravel scope function like so:
on Laravel model
public function scopeSearchStatus($query, $keyword) {
/** All the code above **/
}
Then it can be called cleanly anywhere you'd like:
SomeModel::searchStatus($keyword);
You could also try this:
<?php
$q = strtolower($keyword);
$d = (preg_match('/\b(pen|pend|pending)\b/', $q)) ? 1 : 4;
$d = (preg_match('/\b(shi|ship|shipped)\b/', $q)) ? 2 : 4;
$d = (preg_match('/\b(can|cancel|)\b/', $q)) ? 3 : 4;
$query->whereStatus($d);
is there any way to edit/change the default locale setting in localeconv()?
I would like to use the the money_format function, and it works fine, but the locales for my language/region are not correct.
To be more precise, for Croatia, we use the currency symbol after the number, not before like set in local values?
Are there any ways I can edit this? Or at least manually check, change values, and send new values to setlocale()?
Working on shared hosting btw.
number_format() has nothing to do with currency symbols, you probably meant money_format(), but ... well - just use number_format() and append whatever currency symbol you want to the return value.
If anyone is interested, I made it work with my own replacement of the money_format() function.
It is basically copy-paste from here with added parameters for forceRight and noSpace
class Helper_Locales
{
public static function formatNumber($number, $isMoney=false, $forceRight=false, $noSpace=false) {
$lg = isset($lg) ? $lg : setlocale(LC_MONETARY, '0');
$ret = setLocale(LC_ALL, $lg);
setLocale(LC_TIME, 'Europe/Paris');
if ($ret===FALSE) {
echo "Language '$lg' is not supported by this system.\n";
return;
}
$LocaleConfig = localeConv();
forEach($LocaleConfig as $key => $val) $$key = $val;
// Sign specifications:
if ($number>=0) {
$sign = $positive_sign;
$sign_posn = $p_sign_posn;
$sep_by_space = $p_sep_by_space;
$cs_precedes = $p_cs_precedes;
} else {
$sign = $negative_sign;
$sign_posn = $n_sign_posn;
$sep_by_space = $n_sep_by_space;
$cs_precedes = $n_cs_precedes;
}
// Number format:
$n = number_format(abs($number), $frac_digits,
$decimal_point, $thousands_sep);
$n = str_replace(' ', ' ', $n);
switch($sign_posn) {
case 0: $n = "($n)"; break;
case 1: $n = "$sign$n"; break;
case 2: $n = "$n$sign"; break;
case 3: $n = "$sign$n"; break;
case 4: $n = "$n$sign"; break;
default: $n = "$n [error sign_posn=$sign_posn !]";
}
// Currency format:
$currency_symbol = strtolower($currency_symbol);
$m = number_format(abs($number), $frac_digits,
$mon_decimal_point, $mon_thousands_sep);
if ($sep_by_space && !$noSpace) $space = ' '; else $space = '';
if ($cs_precedes && !$forceRight) $m = "$currency_symbol$space$m";
else $m = "$m$space$currency_symbol";
$m = str_replace(' ', ' ', $m);
switch($sign_posn) {
case 0: $m = "($m)"; break;
case 1: $m = "$sign$m"; break;
case 2: $m = "$m$sign"; break;
case 3: $m = "$sign$m"; break;
case 4: $m = "$m$sign"; break;
default: $m = "$m [error sign_posn=$sign_posn !]";
}
if ($isMoney) return $m; else return $n;
}
}
I've had a look through several of the related questions and just can't seem to see the issue!
the following two functions are within my function include file and for some reason I cannot seem to get the check_orientation function to return scale to the display_months_news function.
I'm fairly sure I'm just missing something really obviously.
the display_news function
function display_months_news()
{
//header('Content-type: image/jpeg');
$year = date("y");
$year = "20" .$year;
$month = date("m");
if ($month) {
switch ($month){
case "01": $month = "January"; break;
case "02": $month = "February"; break;
case "03": $month = "March"; break;
case "04": $month = "April"; break;
case "05": $month = "May"; break;
case "06": $month = "June"; break;
case "07": $month = "July"; break;
case "08": $month = "August"; break;
case "09": $month = "September"; break;
case "10": $month = "October"; break;
case "11": $month = "November"; break;
case "12": $month = "December"; break;
}
} else {
echo '<p>The date field is empty.</p>';
}
$nowdate = $month." ".$year;
$result = mysql_query("SELECT * FROM news ORDER BY sqldate DESC LIMIT 6") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
global $imgurl;
$imgurl = $row['img1'];
check_orientation4($imgurl);
$comcount = comment_count($row['nid']);
if ($comcount == NULL) {$comcount = 0;}
echo '<div class="news-preview-container">';
echo '<div class="news-preview-container-date">';
echo "<P>" .$row['mmonth']. " ".$row['dday']. ", " .$row['yyear']. "</p><BR>";
echo '</div>';
echo '<div class="news-preview-container-title">';
echo "<p><a href='article.php?id=" .$row['nid']."'>" .$row['title']."</a></p><BR>";
echo '</div>';
echo '<div class="news-preview-container-inner">';
echo '<div class="news-preview-container-text">';
echo '<p>'.ShortenText($row['body']).'</p>';
echo '</div>';
echo '<div class="news-preview-container-image"><img src="'.$imgurl.'"'.$scale.'"></div><BR></div>';
echo '<div class="news-preview-container-bottombar">Posted in '.$row[tag].' / '.$comcount.' Comments</div>';
echo '</div>';
}
}
and the check_orientation function
function check_orientation4($imgurls){
if ($imgurls == NULL){ echo ""; }
else {
$imgurlshrunk = NULL;
$maxsize = 120;
$filename = NULL;
$height = NULL;
$width = NULL;
$scale = NULL;
$imgurlshrunk = $imgurls;
$filename = basename($imgurlshrunk);
$filename = "admin/uploads/" . $filename;
list($width,$height,$type,$attr) = getimagesize("$filename");
$wRatio = $maxsize / $width;
$hRatio= $maxsize / $height;
global $scale;
if ( ($width <= $maxsize) && ($height <= $maxsize))
{
$scale = "";
return $scale;
}
elseif ( ($wRatio * $height) < $maxsize)
{
$scale = "width = '100%'";
return $scale;
}
elseif ($height == NULL)
{
echo "empty height";
}
else
{
global $height;
$scale = "height = '100%'";
return $scale;
} }}
Thanks in advance to whoever points out the silly mistake i'm missing!
You don't store the result of check_orientation4($imgurl); anywhere. Try changing that line to this:
$scale = check_orientation4($imgurl);
You aren't doing anything with the return value of check_orientation4
You're not assigning the return value of check_orientation4 to anything.
check_orientation4($imgurl);
should be
$scale = check_orientation4($imgurl);
Shouldn't you be returning scale into a variable of some kind? Right now you're just running the function. You should do something like:
$toScale = check_orientation4($imgurl);
Because you're discarding the return value from check_orientation4, perhaps?