i have database list for users and user's coupons and i want to add +1 ticket per coupon if bigger than two between numbers.
For example:
100 between 200 = 1 ticket
200 between 300 = 2 ticket
300 between 400 = 3 ticket
.......
.......
1200 between 1300 = 12 ticket
i put photo for example:
My Code is :
$q=$db->query("SELECT DISTINCT client_id FROM kuponlar ORDER BY client_id LIMIT 20");
foreach($q as $cat){
echo '<li id="'.$cat['client_id'].'" class="files">';
echo 'User ID: '.$cat['client_id'].'';
echo '<ul class="sub-menu">';
$linkq=$db->query("SELECT * FROM kuponlar WHERE client_id='" . $cat['client_id'] . "'");
foreach($linkq as $link){
echo '<li>Coupon ID: '.$link['kuponid'].' - Coupon Price: '.$link['yatirimi'].' ₺ / Won Ticket: '.substr($link['yatirimi'], 0, 1).' </li>';
}
echo '</ul></li>';
}
As discussed in the comments;
So if the number is 12 000, you want 120 as a result? Basically divide by 100? – Qirel
Yes #Qirel this is perfect comment. i mean like this. (...) – Ismail Altunören
So put simply, you want to divide the number by 100. You must then floor it, to get a full integer and get rid of any decimal points.
floor($link['yatirimi']/100);
You would replace that with your substr(), making the full line
echo '<li>Coupon ID: '.$link['kuponid'].' - Coupon Price: '.$link['yatirimi'].' ₺ / Won Ticket: '.floor($link['yatirimi']/100).' </li>';
floor() docs
You can do this using floor():
http://php.net/manual/en/function.floor.php
floor — Round fractions down
So this should do the trick: floor($link['yatirimi'] / 100)
Use that in place of your substr.
you can user this: $wonTicketsCount = round(($link['yatirimi'] / 100 ) - 0.5); instead substr($link['yatirimi'], 0, 1).
Refering to your comments if you only use hundreds steps :
Modify this line :
substr($link['yatirimi'], 0, 1)
This line will always take the first number.
By : this one
This will take all numbers excepts the two lasts.
substr($link['yatirimi'],0,-2);
By keeping the substr, it will not work for number between 0-100,
It's better to use #Qirel's Anwser.
Related
Good day, below is a piece of code that increments the total cost for each product in the session array. My problem is displaying 0 at the end of the total when the last peny = 0.
Example 1, 2.20 + 2.20 = 4.40 but only 4.4 is shown
Example 2, 2.20 + 2.25 = 4.45 and 4.45 is shown
$total = 0;
if (isset($_SESSION['cartItems'])){
foreach ($_SESSION['cartItems'] as $product){
$total += $product['cost'];
}
}
echo $total;
Any advice on how to show/include when a 0 is entered?
This was already answered in a comment by WebCode.ie but here is a detailed answer that may help those facing the same problem:
To customize a number format, you can use the PHP Function string number_format() , that accepts either one, two, or four parameters, this way:
$my_number=25200;
$number_decimals=2;
$dec_separator=".";
$thousands_separator=" ";
echo number_format($my_number , $number_decimals, $dec_separator, $thousands_separator);
// This will output 25 200.00
The number you want to format
The number of decimals
The decimal separator
The thousands separator
Please also note that you can only use 1, 2 or 4 parameters.
Here is the problem
I have 200 votes and 53 of them must be calculated as the porcent so I run this example to test the math first
$total="200";
$votes="53";
$calculate=($total / 100) * $votes;
echo "The porcent of votes is ".$calculate." ";
The result must be something like 26% but I get
Result 53% ...
What can be wrong ?
Here:
<?php
$total="200";
$votes="53";
$calculate=($votes * 100) / $total;
echo "The porcent of votes is ".$calculate."%";
Your calculate formula is wrong. If you want to calculate the percent of any number, you need the given or scored number and the total number. In your case, given is or score is 53 and total it 200. So you divide given or score by total and the multiply the results by 100 to get the percent. See below.
$total="200";
$votes="53";
$calculate=($votes / $total) * 100;
echo "The porcent of votes is ".$calculate." ";
Let me know if this helps. Stay warm!
I don't understand what I am doing wrong. I've got a number, eg 220. Then I need to increase it with for example 11%, so 220 * 11% = 244.2, but my answer is 2420?
I've tried the following:
echo '<br>';
echo $col0 . '<br>'; //outputs 220
settype($col0New, "decimal");
$col0New = ($col0 * '11%') + $col0;
echo $col0New . '<br>'; //outputs 2640 but should be 244.2?
$col0New1 = number_format($col0New,2);
echo $col0New1 . '<br>'; //outputs 2,640.00
Please help.
There's no need for making it any more complex than you need. Basic math allows you to just multiply with a constant, more specific, if you multiply by 1.11, you'll get an increase of 11%.
You can simply do it like this
echo $col0 * 1.11; // Outputs 244.20
You can't use the % as percentage in php. To Accompish what you want, you need to rewrite your percentage to it's decimal form. Ex: 55% will be 0.55
You need to stay with pure numbers: 240 * 1.11. Your '11%' is a string literal, which PHP must fist convert to the number 11, and then multiplies by 240, which explains where 2420 comes from.
.11 is 11%, but you want to add 1 to increase the value to get 111%.
I understand how to format a number from for example 2.10 to 2.1 nut how would i format a number so that 381 -> 38.1% or 38.1
Either result is fine as I can just add a percent sign after the first result. I was thinking of maybe splitting the number then adding a decimal after the second number but this wouldn't work with 100%?
If 381 is a percentage of 1000, then 381 divided by 1000 = 0.381
Multiply 0.381 by 100 to get 38.1
The SQL CODE would be:
DECLARE #Number1 Decimal(18,1) = 381, #Number2 Decimal(18,1) = 0
SET #Number2 = CAST(ROUND((#Number1 / 1000) *(100),1,1) AS DECIMAL(18,1))
SELECT #Number2
Hope this helps.
This should work for you:
$number = 381;
echo sprintf("%02.2f%%", $number/10);
Output:
38.10%
If you don't want it with 2 decimal just change .2 as you want it
Doing $number = $number / 10; should turn the trick.
Searching for a function ro round numbers to the nearest multiple of 5
22 -> 20
23 -> 25
40 -> 40
46 -> 45
48 -> 50
and so on.
Tried this which always returns the higher value:
5 * ceil($n / 5);
Use round() instead of ceil().
5 * round($n / 5);
ceil() rounds a floating point number up to its next integer in sequence. round() will round to the nearest integer using standard rounding rules.
Back to maths, since round works with decimals, multiply by 5 and divide by 10 and then round, it. Multiply by 5 again to get what u want. (Other answer works as well, just a different way of looking at it)
function round_5($in)
{
return round(($in*2)/10)*5;
}
echo round_5(48);
See if this helps
Well, facing this issue while helping make an POS for a Canadian company, came up with this solution, hope it helps someone. (Canada removed the penny in 2012). Also includes for doing tax included pricing, just pass '1' as second argh.
//calculate price and tax
function calctax($amt,$tax_included = NULL){
$taxa = 'tax rate 1 here';
$taxb = 'tax rate 2 here';
$taxc = ($taxa + $taxb) + 1;
if(is_null($tax_included)){
$p = $amt;
}else{
$p = number_format(round($amt / $taxc,2),2);
}
$ta = round($p * $taxa,2);
$tb = round($p * $taxb,2);
$sp = number_format(round($p+($ta + $tb),2),2);
$tp = number_format(round(($sp*2)/10,2)*5,2);
$ret = array($ta,$tb,$tp);
return $ret;
}