Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Lets say we have 1300 units. now i want to first 500 units to be a price of 12 euro and the other 800 a price of 9 euro.
So what i basicly need is a function that calculates over a total number and gets the first 500 * 12 euro and the other 800 * 9 euro. But the total unit can also be 5000 instead of this example of 1300 unit, its user input ofcouse.
<?php
$total = 1300;
$sub = 1;
$sub2 = 1;
for($i=1; $i<=$total; $i++)
{
for($sub; $i<=500; $sub++)
{
$sub2 += $sub;
break;
}
}
echo $sub2;
?>
<?php
$units = 1300;
$cut_off=500;//made it a var just in case
$a=12;//fist 500
$b=9;//rest
//verbose calculations, you don't really need to create the extra variables
$a_total=$cut_off*$a;
$b_total=($units-$cut_off)*$b;
$grand_total=$a_total+$b_total;
?>
you should probably add a check if the units imputed is less than 500. or things will turn out weird
Updated:
<?php
function calc_total($qtt, $min = 500, $fst_price = 12, $nxt_price = 9){
if($qtt > $min) return false; // Check if the minimum 500 was sent.
/* First calculate the rest, which is the quantity sent by
the user without the minimum x 9. Then add the minimum x 12 */
$total = (($qtt - $min) * 9) + ($min * 12);
return $total;
}
?>
You can call the function like this:
echo "$". calc_total(1300);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I had a list of numbers between 1 until 100000000
I need a function to get a number and return a random number between 1 to 30 For each other number...But unique!
for example each time I call getrandomnumber('3') it always return me 25! (no other random number between 1 ...30 like rand(1,30)
You could use the given number as a seed and generate the number using the standard random number function.
function getrand($num)
{
srand($num);
return rand(1 , 30);
}
The Answer
for($i = 0;$i<100;$i++)
echo '<br>'.getrand($i);
function getrand($num)
{
$num = substr($num, -2);
$num = (int)$num;
if($num < 31) return $num;
elseif($num > 30 && $num < 61) return $num - 30;
elseif($num > 60 && $num < 91) return $num - 60;
elseif($num > 90 && $num < 101) return $num - 70;
}
1-30 repeat:
$x=0;
for($i = 0;$i<100;$i++){
$x++;
if($x>30)$x=1;
echo '<br>'.$x;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to get a percentage of the current value between -60 and 60.
so if
NUMBER == 60
The result should be 100%
NUMBER == -60
The result should be 0%
NUMBER == 0
The result should be 50%
How i can achieve this?
This is basic math
MIN = -60;
MAX = 60;
if (NUMBER < MIN) {
PERCENT = 0;
}
else if (NUMBER > MAX) {
PERCENT = 100;
}
else {
PERCENT = (NUMBER-MIN)*100/(MAX-MIN);
}
(NUMBER + 60) / 1.2
Or
(NUMBER + 60) / 120 / 100
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to display total number from start to end in a range in php .Each time number will increase by 10 .
For example total number is 35
Then number will display this way :
1 to 10
11 to 20
21 to 30
31 to 35
$num=35;
for($i=1;$i<$num;$i=$i+10)
{
$j=$i+9;
if($j>$num)
$j=$num;
echo $i.' to '. $j;
}
try
$num=35;
for($i=1;$i<=$num;$i++) {
if($i %10 == 0)
echo $i.'<br>';
else
echo $i;
}
will output :-
12345678910
11121314151617181920
21222324252627282930
3132333435
Use a for loop like this
for($x = 1; $x <= 35, $x += 10){
echo $x . " to " . $x+9;
}
This will work i hope.
Thanks
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a simple form and a submit button. After pressing the submit button, get the values (they will be numbers) and calculate the sum. And again calculate the sum of individual digits stored in sum.
A better explanation: 1+2+3+4=10 where 1,2,3 and 4 are user inputs from the form. And the sum 10 has to be split-ted and summed again as 1+0=1. And this would be my final result. So far I did the task where it gives me the first sum. I don't know what to do to display the second result which I want to be the finale.
<?php
$a='';
$sum='';
$total='';
if (!isset($_POST['submit'])) {
echo " ";
}
else {
$a = array($_POST['textfield'],$_POST['textfield1'],$_POST['textfield2'],$_POST['textfield3'],$_POST['textfield4'],$_POST['textfield5'],$_POST['textfield6'],$_POST['textfield7'],$_POST['textfield8'],$_POST['textfield9'],$_POST['textfield10'],$_POST['textfield11'],);
for ($i=0; $i<=12; $i++) {
$sum= array_sum($a);
$total= ;
}
}
echo "sbora e: $total ";
?>
$total = array_sum(str_split($sum));
Using Artefacto's method.
On another note,
$a = array($_POST['textfield'],$_POST['textfield1'],$_POST['textfield2'],$_POST['textfield3'],$_POST['textfield4'],$_POST['textfield5'],$_POST['textfield6'],$_POST['textfield7'],$_POST['textfield8'],$_POST['textfield9'],$_POST['textfield10'],$_POST['textfield11'],);
can be written as,
$a = array();
for ($i = 0; $i <= 11; $i++){
if (isset($_POST["textfield_$i"]))
array_push($a, $_POST["textfield_$i"]);
}
if the names of the fields are:
textfield_0, textfield_1, textfield_2...
So you want to build the cross sum of your first result:
$result2 = 0;
//cast integer to string
$strTotal = (string) $total;
//loop over "chars" and add them to your second result
for($i=0; $i < strlen($strTotal); $i++)
{
$result2 += $strTotal{$i};
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
anybody can help me me with this? I have a variable such:
$page = '50';
$newpage = 'http://www.mydomain.com/page/'.$page.'';
I want new page echo such this:
http://www.mydomain.com/page/50
http://www.mydomain.com/page/49
......
.......
....... until page acho such:
http://www.mydomain.com/page/1
<?php
for($page=50;$page>0;$page--) {
$newpage = 'http://www.mydomain.com/page/'.$page.'';
echo "$newpage\n";
}
?>
Look at this for loop:
$prefix = 'http://www.mydomain.com/page/';
for ($page = 50; $page >= 1; $page--) {
echo $prefix.$page;
}
First, the variable $page is initialized. I use 50 instead of '50' because we're dealing with numbers and not with strings.
The next bit is the condition while the loop continues: $page >= 1 - so the loop will stop after 1.
The last part is a decrement operator, it subracts 1 off $page for each loop iteration.
Finally, the prefix and the page number are combined using the concat operator (.).
What you are looking for is :
<?php
for($i=50; 0 <= $i; $i--){
echo $i.'<br />';
}
?>
if you don't want to go upto 0, change <= for <. This will loop from 50 until 0 so it will eacho in order :
50
49
48
47
46
...
3
2
1
0
Here 50 is your start number, 0 is your last number to go. So in this example it print out 50 until 0.