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...';
}
?>
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
}
?>
Well, I have created a simple higher/lower game script. Its working fine, but I want to limit the user actions to 3. This means that the user will have to guess the number with 3 moves. On third action I will execute query and save the user to mysql. How I can limit the actions/moves?
<?php
session_start();
function Start_Again() {
$number = rand(1,100);
$_SESSION['higherlower'] = $number;
echo "Select a Number below.";
Display_Form();
}
function Display_Form() {
echo "<table>";
for ($num=1;$num < 101;$num++) {
if (!preg_match("/(.*?)0/", $num)) { echo "<td><a href=\"?number=".$num."\">".$num."</td>"; }
else { echo "<td><a href=\"?number=".$num."\">".$num."</td></tr><tr>"; }
}
echo "</table>";
}
if (isset($_GET['number'])) {
$User_Number = $_GET['number'];
$Actual_Number = $_SESSION['higherlower'];
$count = 0;
if ($User_Number < $Actual_Number) { echo "Higher"; $count + 1; Display_Form(); }
elseif ($User_Number > $Actual_Number) { echo "Lower"; $count + 1; Display_Form(); }
elseif ($User_Number == $Actual_Number) { echo "Bingo, Correct Guess!<br>"; Start_Again(); }
echo $count;
}elseif (!isset($_POST['higherlower'])) { Start_Again(); }
?>
When you initialize the game, simply store the amount of tries in your session.
$_SESSION['tries'] = 3;
Then, when the user picks a number, lower the tries and check if it's 0.
$_SESSION['tries']--;
if ($_SESSION['tries'] <= 0) {
die("Enough! You've been clicking numbers all afternoon.");
}
Full Implementation
<?php
/**
* Higher-lower game
*/
if (!isset($_SESSION)) {
session_start();
}
function Start_Again() {
$number = rand(1,100);
$_SESSION['higherlower'] = $number;
$_SESSION['tries'] = 3;
echo "Select a Number below.";
Display_Form();
}
function Display_Form() {
echo "<table>";
$chunks = array_chunc(range(1, 100), 10);
foreach ($chunks as $chunk) {
echo "<tr>";
foreach ($chunk as $num) {
echo "<td><a href=\"?number=".$num."\">".$num."</td>";
}
echo "</tr>";
}
echo "</table>";
}
if (isset($_GET['number'])) {
$User_Number = $_GET['number'];
$Actual_Number = $_SESSION['higherlower'];
if ($_SESSION['tries'] <= 0) {
die("Oops! You're bad at this!");
}
if ($User_Number < $Actual_Number) { echo "Higher"; $count + 1; Display_Form(); }
elseif ($User_Number > $Actual_Number) { echo "Lower"; $count + 1; Display_Form(); }
elseif ($User_Number == $Actual_Number) { echo "Bingo, Correct Guess!<br>"; Start_Again(); }
$_SESSION['tries']--;
echo $_SESSION['tries'] . 'chances left';
} elseif (!isset($_POST['higherlower'])) {
Start_Again();
}
You are already starting a session so you can store the value in a session variable i.e. $_SESSION['count'];
session_start();
if( !isset( $_SESSION['count] ) ) $_SESSION['count'] = 1;
Later on in the code you will need to update the counter $_SESSION['count']++;
And check whether the person has used up all the guesses
if( $_SESSION['count'] > 3 ) { ..... }
i have this code .
echo "<br>";
$start = 1;
$angka = $_POST[angka];
$a = $angka;
for($i=$start; $i<=$angka; $i++) {
for($j=$start;$j<=$angka;$j=$j+2){
echo $i;
if($j < $angka) echo $a;
}
$a--;
echo '<br>';
}
Reference: Print Looping for dynamic row php
This is not my expecting result .
First i want the result like this .
-2-4-
1-3-5
-2-4-
1-3-5
-2-4-
The rule is Number of rows and columns follow the number of numbers declared.If the declaration of the number 5, then the results will be displayed are 5 rows and 5 columns like the example above.
i think this code is working
<?php
$_POST['angka'] = 5;
$angka = $_POST['angka'];
for($i=1; $i<=$angka; $i++) {
for($j=1;$j<=$angka;$j++){
if($i%2 == 1) {
if($j%2 == 0) {
echo $j;
} else {
echo '-';
}
} else {
if($j%2 == 0) {
echo '-';
} else {
echo $j;
}
}
}
echo '<br>';
}
Result:
-2-4-
1-3-5
-2-4-
1-3-5
-2-4-
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>
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";
}