Reputation:
So I am writing out all my code and it is all the way It should be, but for some reason it doesn't seem to want to output anything on the screen like it should...
Can someone please help me out? Thanks
code:
$points_disp = $user_data['points'];
$oneFDigit = substr($points_disp, 0, 1);
$oneSDigit = substr($points_disp, 1, 1);
$oneRange = range(1000, 9999);
$tenFDigit = substr($points_disp, 0, 2);
$tenSDigit = substr($points_disp, 2, 1);
$tenRange = range(9999, 99999);
$hunFDigit = substr($points_disp, 0, 3);
$hunSDigit = substr($points_disp, 3, 1);
$hunRange = range(99999, 999999);
$oneMillionFD = substr($points_disp, 0, 1);
$oneMillionSD = substr($points_disp, 1, 1);
$oneMillionRange = range(999999, 9999999);
if ($points_disp < 1000){
echo $points_disp;
} else if (in_array($points_disp, $oneRange)){
echo $oneFDigit . "." . $oneSDigit . "k";
} else if (in_array($points_disp, $tenRange)){
echo $tenFDigit . "." . $tenSDigit . "k";
} else if (in_array($points_disp, $hunRange)){
echo $hunFDigit . "." . $hunSDigit . "k";
} else if (in_array($points_disp, $oneMillionRange)){
echo $oneMillionFD . "." . $oneMillionSD . "m";
}
Upvotes: 3
Views: 115
Reputation: 2792
I tried this, just dumped your range() because I had a memory problem. I think range is the wrong function to use here, because you'll get some huge arrays and you just want to know if the number is between this values.
BEWARE: the new range array needs minValue and maxValue in this order!
$user_data['points'] = 9999;
$points_disp = $user_data['points'];
$oneFDigit = substr( $points_disp, 0, 1 );
$oneSDigit = substr( $points_disp, 1, 1 );
$oneRange = array( 1000, 9999 );
$tenFDigit = substr( $points_disp, 0, 2 );
$tenSDigit = substr( $points_disp, 2, 1 );
$tenRange = array( 9999, 99999 );
//
$hunFDigit = substr( $points_disp, 0, 3 );
$hunSDigit = substr( $points_disp, 3, 1 );
$hunRange = array( 99999, 999999 );
//
$oneMillionFD = substr( $points_disp, 0, 1 );
$oneMillionSD = substr( $points_disp, 1, 1 );
$oneMillionRange = array( 999999, 9999999 );
if ( $points_disp < 1000 ) {
echo $points_disp;
} else if ( checkInRange( $points_disp, $oneRange ) ) {
echo $oneFDigit . "." . $oneSDigit . "k";
} else if ( checkInRange( $points_disp, $tenRange ) ) {
echo $tenFDigit . "." . $tenSDigit . "k";
} else if ( checkInRange( $points_disp, $hunRange ) ) {
echo $hunFDigit . "." . $hunSDigit . "k";
} else if ( checkInRange( $points_disp, $oneMillionRange ) ) {
echo $oneMillionFD . "." . $oneMillionSD . "m";
} else {
echo "nothing found";
}
function checkInRange( $needle, $range ) {
$min = $range[0];
$max = $range[1];
return ( $needle >= $min && $needle <= $max ) ? true : false;
}
Upvotes: 3
Reputation:
The code seems a bit overkill for what you are trying to actually achieve.
$points = (string) 99999;
if($points < 1000) {
echo $points;
}
elseif($points < 9999) {
echo sprintf('%s.%sk', $points[0], $points[1]);
}
elseif($points < 99999) {
echo sprintf('%s.%sk', $points[0], $points[1]);
}
elseif($points < 999999) {
echo sprintf('%s.%sk', $points[0], $points[1]);
}
elseif($points < 9999999) {
echo sprintf('%s.%sm', $points[0], $points[1]);
}
Result: 9.9k
Upvotes: 0
Reputation: 98
If the condition is always false, use this:
...
} else {
echo "some value";
}
Upvotes: 5