Display 1000 as 1k using PHP - php

I have beginners skills with PHP and hence need your help guys.
What i am trying to achieve is to round up a number for total shares count for a plugin.
my variable is $totalshare
i am using return $totalshare to dispaly the results
i would like to achieve results as following example:
1000 = 1k
1.230 = 1.2k
1489 = 1.5k
1.660 = 1.6k
.
.
.
15576 = 15.6k
.
.
1201200 = 12.02mil
1211200 = 12.12mil
Any help would be much appreciated.
Thanks in advance.

You can write like this,
function convertShares($shareValue){
if($shareValue < 1000){
return shareValue;
}elseif($shareValue > 100000 ){
return number_format($shareValue/100000,2) . "mil";
}else{
return number_format($shareValue/1000,2) . "k";
}
}

Simplest way:
if($n>=1000) $n = ($n/1000)."k";
if you want something more i can edit

Related

how to generate a random base64 number to power of 11

im trying to work on urls, so i need a random base64 to the power of 11 number, which changes every time you refresh the page.
Im working in php.
any help would be great,
many thanks.
not sure how to go about but maybe somthing like bellow?
function toChars($number) {
$res = base_convert($number, 10,26);
$res = strtr($res,'0123456789','qrstuvxwyz');
return $res;
}
or
$res = ('0123456789','abcdefghijklmnopqrstuvxwyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
then randomly select from that till you get a 11 digit number aka(base64 to power of 11)
and echo the number here $random_base64_number = ?
Dont worry figured it out.
function base62() {
$index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';
$res = '';
for ($x = 1; $x <= 11; $x++) {
$res .= $index[rand(1, strlen($index))];
}
echo "The number is: $res <br>";
}

PHP repeat action with adding value

I am just learning coding, and couldn't find solution for that:
I'm creating a website that shows pictures that are on its server.
$number = $_POST["NR"];
$picture1 = "{$number}.jpg";
echo '<img src="pictures/' . $picture1 . ' ">';
How can I make it to repeat the echo string, but every time add for "$picture1" calculation +1 ? So, that the maximum count would be 40?
Thanks for helping. I know that it is actually easy, but I still don't know how to do it :)
Assuming that you have images named correctly awaiting this to happen:
$number = $_POST["NR"];
$i = 0;
while ($i < 40) {
$picture = $number + $i.".jpg";
echo '<img src="pictures/' . $picture . ' ">';
$i++;
}
Use a counter and loop. The issue here is that your $number could be anything... For instance if someone passed 500, you would need images named, 500.jpg, 501.jpg, etc. all the way up to 540.jpg. Unless I misunderstood the question.

Make an equation in php

I have a php file where i want to make an equation with the output i get.
It is pretty easy if the line is like this:
<?php echo round(($post['voteup']/($post['voteup'] + $post['votedown'] ))*100); ?>
But i have a php line where it goes like this, and i just can't figure out how to make the equation work:
$str='';
$data = $dbc->query($sql);
if($data!=null && $data->num_rows>0){
while( $row = $data->fetch_array(MYSQLI_ASSOC)){
$str.="<p>".$row['voteup']." + ".$row['votedown'].</p>";
}
It may be a silly question, but this really bothers me.
If you want to add the upvotes and downvotes you would do this -
$newTotal = $row['voteup'] + $row['votedown'];
$str.="<p>" . $newTotal ."</p>";
If you are trying to add $row['voteup'] and $row['votedown'] together you should addition them in a variable first then use it.
$total = $row['voteup'] + $row['votedown'];
$str .= "<p>{$total}</p>";
If you really need to make it inline you can do something like this as well.
$str .= '<p>' . ($row['voteup'] + $row['votedown']) . '</p>';
If you just want to display the "equation" simply fix the error.
$str.="<p>".$row['voteup']." + ".$row['votedown']."</p>";
Or you could actually turn it into an equation:
$str.="<p>".$row['voteup']." + ".$row['votedown']." = ".($row['voteup'] + $row['votedown'])."</p>";

Vote up down total max

Hey tried to search for similar but guess my english fail me lol, well here is what i need help with, im trying to make a vote system with up/down vote and wanna show it like this
5.3/10
but have no idea how to make total "5.3" not go over 100% = 10 here is my code so far
<?php
$Vote_up = 804;
$Vote_down = 942;
$total = $Vote_up + $Vote_down;
$result = 100;
echo number_format($total/$result,1,",",".") . "/10";
?>
result is 17,5/10
ps. new to php so be easy on me ^^
i am really bad at maths but i think this should be work
$Vote_up = 555;
$Vote_down = 555;
$total = $Vote_up + $Vote_down;
if( $total <= 0 ){
$score = -11;
} else {
$score = (($Vote_up / $total) + ($Vote_down / $total) * 10) * -1;
}
echo floor( $score + 11 ) . '/10';
if someone have a better solution, please i would like to know it - thanks!

How to Convert/Round it off

I have some problem regarding converting/truncating a number.
This is the condition:
x.35 where x is any decimal number. If the decimal number is less than or equal to 35 then convert the .35 into 99 and subtract the x value with 1.
Something like this:
45.35 will become 44.99
Any help will be much more appreciated!
There's probably a faster way that doesn't use the explode function and other unnecessary things, but here's my rendition of it.
<?php
$input = 45.35; //input, obviously
$in2 = explode(".", $input);
if($in2[1] <= 35) {
$in2[1] = 99;
}
$output = $in2[0] . "." . $in2[1];
?>
Check out:
http://us.php.net/floor
http://us.php.net/ceil
http://us.php.net/round
Those should help
Following up on this, here's an example.
$val = 45.35;
$decimal = $val - floor($val);
if($decimal<.35) echo floor($val).".99";

Categories