The listings on my site have three states:
- Active
- Sold
- Expired
I wrote this to get listings that are in Active or Sold to automatically expire if they pass a certain date.
<span class="detail">Status: <?php
if(strtotime($property['data']['field_3211']) < time()){ echo 'Expired'; }
else if($property['raw']['field_3022'] == 5) echo 'Active';
else if($property['raw']['field_3022'] == 8 ) echo 'Sold';
else echo 'Not set';
?>
</span>
I now want to change it to only expire if the status is set to active and ignore the expiry if it is set to sold.
All you need to do is specify in the if that outputs the Expired message that the record must be active as well as < time().
<span class="detail">Status:
<?php
if(strtotime($property['data']['field_3211']) < time() &&
$property['raw']['field_3022'] == 5)
{
echo 'Expired';
}
else if($property['raw']['field_3022'] == 5) {
echo 'Active';
}
else if($property['raw']['field_3022'] == 8 ) {
echo 'Sold';
}
else {
echo 'Not set';
}
?>
</span>
Try this:
<span class="detail">Status: <?php
$status = $property['raw']['field_3022'];
$expired = ( strtotime($property['data']['field_3211']) < time() );
switch (true) {
case ($status == 5) : echo ($expired) ? 'Expired' : 'Active'; break;
case ($status == 8) : echo 'Sold'; break;
default : echo 'Not set';
}
?>
</span>
Related
code:
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if($grade <= 39)
{
echo '<span class="text-danger">Bad</span>';
}
else if($grade >=74)
{
echo '<span class="text-warning">Average</span>';
}
else if($grade >=100)
{
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
Show grade according to:-
0-39 (Bad)
40-74 (Average)
75-100 (Good)
In this question I want to show message bad, average, good according to grade. Suppose if grade is 0-39 then it will show bad similarly if grade is 40-74 then show average like this but the condition I am giving is wrong. So, how can I do it?
Just change greater than to less than.
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if($grade <= 39)
{
echo '<span class="text-danger">Bad</span>';
}
else if($grade <=74) //Change to less than here.
{
echo '<span class="text-warning">Average</span>';
}
else if($grade <=100) //Change to less than here.
{
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
You need to modify the conditions so that no score is missed out of grade.
So, please define 3 ranges of scores using if and `else if'.
Range 1: 0-39: if ($grade <= 39) {
Range 2: 40-74: else if($grade <=74) {
Range 3: 75-100: else if($grade <=100) {
This way, first if checks if the grade is less than or equal to 39.
If yes, grade is Bad.
Else, if score, does not fit in this range, it will go ahead in next if else for the range: 40-74 and same way to 75-100 if it does not fit.
Corrected code:
if ($outoff!=0) {
$grade = ($score/$outoff)*100;
if ($grade <= 39) { // Score range: 0-39
echo '<span class="text-danger">Bad</span>';
}
// If $score is coming to this else if means it is definitely
// greater than 39: that is 40+
// Score range: 40-74 as it is in else if after if of `39`
else if($grade <=74) {
echo '<span class="text-warning">Average</span>';
}
// Score range: 75-100 as it is in else if after 0 - 39 and 40 - 74
else if($grade <=100) {
echo '<span class="text-success">Good</span>';
}
}
You have to make changes to you code as follow:
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if( $grade >= 0 && $grade < 40 ) {
echo '<span class="text-danger">Bad</span>';
}
else if( $grade > 39 && $grade < 75 ) {
echo '<span class="text-warning">Average</span>';
}
else if($grade > 74 && $grade <= 100 ) {
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if( $grade > 0 && $grade <= 39 ) {
echo '<span class="text-danger">Bad</span>';
}
else if( $grade >= 40 && $grade <= 74 ) {
echo '<span class="text-warning">Average</span>';
}
else if($grade >= 75 && $grade <= 100 ) {
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
if($object_type == 'regular') {
if($u_login == $object_user || $u_access >= 3 || $object_access >= 4) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
} else
if($object_type == 'comment') {
if($u_login == $object_user || $u_access >= 2 || $object_access >= 4) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
}
So the thing that if object is different type user need to have diff access level. How this statement can be simplified for don't having duplicates in it?
I generally forgot about groups in if's, thank you for reminding me it!
if($u_login == $object_user || $object_access >= 4 || ($object_type == 'regular' && $u_access >= 3) || ($object_type == 'comment' && $u_access >= 2)) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
Both conditions check for $u_login == $object_user and $object_access >= 4, with only the $object_type and $u_access differing. As such, you can bring these two checks up a level, and check against $object_type and $u_access >= 3 inside the outer condition.
As such, the statement can be re-written like this, shrinking one line of code:
if($u_login == $object_user || $object_access >= 4) {
if($object_type == 'regular' && $u_access >= 3) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
else if($object_type == 'comment' && $u_access >= 2) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
}
Although depending on your definition of 'simplify', you could also cut out the outer conditional entirely by making use of some brackets:
if(($u_login == $object_user || $object_access >= 4) && ($object_type == 'regular' && $u_access >= 3)) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
else if(($u_login == $object_user || $object_access >= 4) && ($object_type == 'comment' && $u_access >= 2)) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
However, it's worth nothing that both of your conditionals currently do the exact same thing, so the code could even be simplified to:
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
Hope this helps! :)
This probably isn't the best place to ask for help refactoring your code, but what the heck. Notice that you have two conditions that are exactly the same and being checked in both if conditions. Why not pull those up to the root level?
if($u_login == $object_user || $object_access >= 4) {
if($object_type == 'regular' && $u_access >= 3) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
if($object_type == 'comment' && $u_access >= 2) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
}
Notice that we don't need an else anywhere in here because the conditions provided are, by nature, mutually exclusive. This can make readability a bit simpler.
So I'm creating a points system for my website which I want to change to an echo instead of the actual integer when shown on the users profile.
For example: When the integer is lower than 1000 it displays as the actual number (lets say: 645). But when it is between 1000 and 1100, it will display as '1k' and so on. What I've got so far does work, but displays incorrectly and does seem a bit of a waste of space.. Is there any way to do this in a much simpler; faster way?
Thanks!
code:
<?php
$points_disp = $user_data['points'];
if($points_disp < 1000){
echo $points_disp;
} else if ($points_disp >= 1000){
echo '1k';
} else if ($points_disp >= 1200){
echo '1.2k';
} else if ($points_disp >= 1400){
echo '1.4k';
} else if ($points_disp >= 1600){
echo '1.6k';
} else if ($points_disp >= 1800){
echo '1.8k';
} else if ($points_disp >= 2000){
echo '2k';
}
?>
Edit: I figured out an easier way to do this;
code (for anyone else who needs to do this):
<?php
$points_disp = $user_data['points'];
$fdigit = substr($points_disp, 0, 1);
$sdigit = substr($points_disp, 1, 1);
if ($points_disp < 1000){
echo $points_disp;
} else if ($points_disp >= 1000){
echo $fdigit . "." . $sdigit . "k";
}
echo $num;
?>
You can use switch case:
$points_disp = $user_data['points'];
switch(true)
{
case ($points_disp < 1000):
$num = $points_disp;
break;
case ($points_disp > 1000 && $points_disp < 1100 ):
$num = '1.2k';
break;
//...so on
}
echo $num;
Try this,
if($points_disp < 1000){
echo $points_disp;
} else if($points_disp >= 1000) {
echo round($points_disp/1000,1) . "K";
}
So, For school i have to make PHP show the time, And for example when it is 12:00 the background is a afternoon one, and on 02:00 it is a Night one. This is my code:
<body>
<?php
<div class="tijd">
date_default_timezone_set('GMT+1');
echo date('h:i');
$Tijd = date('h');
if ($Tijd > 12 || $Tijd <17') {
echo '<div class="Middag"> </div>';
}
if ($Tijd > 12 || $Tijd <17') {
echo '<div class="Avond"> </div>';
}
if ($Tijd >= '22') {
echo '<div class="Nacht"> </div>';
}
if ($Tijd >= '6') {
echo '<div class="Ochtend"> </div>';
}
echo('Dit is een test...')
?>
</div>
</body>
</html>
But, What is not working here? On the webpage it shows the time correctly but it doesnt show the background. It is also not working while i do a background color or something so i know it is not only the background color. I tried also to make a background in PHP but i kind of failed at that.
You should use if, else if:
if ($Tijd > 12 || $Tijd <17) {
echo '<div class="Middag"> </div>';
} else if ($Tijd > 12 || $Tijd <17) {
echo '<div class="Avond"> </div>';
} else if ($Tijd >= 22) {
echo '<div class="Nacht"> </div>';
} else if ($Tijd >= 6) {
echo '<div class="Ochtend"> </div>';
}
Having said that, your first two test conditions are exactly the same, so you should look at that too
(this should be a comment but its a bit long).
In addition to Marc B's comment, there's random quotes all over the place - don't quote numeric values when you're trying to do a numeric comparison. You're mixing HTML and PHP -
<?php
<div class="tijd">
This should be causing your code to throw big errors. If you're not seeing these errors then you need to investigate why.
And the way you are running multiple if statements is messy. You could use if...else if...else if ...else, but if you use a switch statement your code will be much clearer:
switch((integer)$Tijd) {
case 13:
case 14:
case 15:
case 16:
echo '<div class="Middag"> </div>';
break;
case 22:
case 23:
echo '<div class="Nacht"> </div>';
break;
default:
echo '<div class="Avond"> </div>';
break;
}
As you can see - there are gaps here which are not described by your original code.
<?php
$hour = date('H'); //H is for 24 hours interval
if ($hour > 4 && $hour < 6) {
$class = 'earlier-morning';
} elseif ($hour >=6 && $hour <=11) {
$class = 'morning';
} elseif ($hour >=11 && $hour < 15) {
$class = 'midday';
} elseif($hour >= 15 && $hour < 19) {
$class = 'day';
} elseif ($hour >= 19 && $hour < 22) {
$class = 'evening';
//the only one case left - hours between 22 and 4
} else {
$class = 'night';
}
echo sprintf('<div class="%s"></div>', $class);
The trick here is elseif
So only ONE condition will always work here.
P.S. I believe that extra quotes in your example is a typo ;-)
I have this code:
<?php
if ( $amount < 5 ) {
echo 'Credit Balance low! You have';
echo $amount;
echo ' remaining credits.';
} else {
echo 'No recent alerts...';
}
?>
Where it says echo $amount; I want to echo $00.00 if the value of $amount == 0. How would I include this in my code?
echo $amount === 0 ? '$00.00' : $amount;
?
You're probably looking for the money_format() function.
<?php echo money_format('%=0-2', $amount); ?>
=0 fills with the 0 character, and -2 left-justifies to a minimum field width of 2.
<?php
if ( $amount < 5 )
{
echo 'Credit Balance low! You have';
echo $amount == 0 ? '$00.00' : $amount;
echo ' remaining credits.';
}
else
{
echo 'No recent alerts...';
}
?>
if ($amount === 0) {
echo '$0.00';
} elseif ($amount < 5) {
echo "Credit Balance low! You have $amount remaining credits";
} else {
echo 'No recent alerts...';
}
You should do:
<?php
if ($amount === 0) {
echo '$00.00';
}
elseif ($amount < 5 && $amout > 0) {
echo 'Credit Balance low! You have' . $amount . ' remaining credits.';
}
else {
echo 'No recent alerts...';
}
?>