Good day, I am a novice programmer. Faced a rather ordinary task, but due to lack of experience I cannot understand in which direction to go.

I need to write the usual js + php calculator to calculate the amount of the order by the coefficients.

Here is a similar calculator (as an example):

With the frontend part of the calculator and the transfer of two numeric variable sliders "My rating" and "I want rating", ajax handler in php handler - I figured it out.

But with the calculation of the total amount of rubles in a php handler, I had difficulties.

I wrote a formula (for the test) that could be used to calculate variables manually, but I don’t know how to implement it from the point of view of php logic.

//ΠŸΠΎΠ»ΡƒΡ‡ΠΈΠ»ΠΈ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΏΠΎΠ»Π·ΡƒΠ½ΠΊΠΎΠ² (для ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°) $current_mmr = 1000; //Мой Ρ€Π΅ΠΉΡ‚ΠΈΠ½Π³ $end_mmr = 5000; //Π₯ΠΎΡ‡Ρƒ Ρ€Π΅ΠΉΡ‚ΠΈΠ½Π³ //Находим расстояниС ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΏΠΎΠ»Π·ΡƒΠ½ΠΊΠ°ΠΌΠΈ $value = $end_mmr - $current_mmr; $a = (2500 - $current_mmr) * 2; $b = (3500 - 2500) * 2.5; $c = (4500 - 3500) * 3.5; $d = ($end_mmr - 4500) * 5.5; $e = $a + $b + $c + $d; //ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ срСдний коэффициСнт $f = $e / $value; //Π£ΠΌΠ½ΠΎΠΆΠ°Π΅ΠΌ расстояниС ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΏΠΎΠ»Π·ΡƒΠ½ΠΊΠ°ΠΌΠΈ Π½Π° срСдний коэффициСнт $result = $value * $f; $html = "Π˜Ρ‚ΠΎΠ³ΠΎ: {$result}.Ρ€ΡƒΠ±"; echo json_encode(array('result' => 'success', 'html' => $html)); 

The difficulty lies in the fact that each numerical range has its own multiplication factor:

 0-2500 value=2 2500-3500 value=2.5 3500-4500 value=3.5 4500-5500 value=5.5 

Here is the logic by which I wrote the algorithm:

enter image description here

 Π”Π°Π½ΠΎ: 1)"Дистанция" ΠΎΡ‚ 0 Π΄ΠΎ 5500 2)ΠšΠΎΡΡ„Ρ„ΠΈΡ†ΠΈΠ΅Π½Ρ‚Ρ‹ умноТСния Π½Π° "ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΊΠ°Ρ…" дистанции: a) ΠΎΡ‚ 0 Π΄ΠΎ 2500 * 2 b) ΠΎΡ‚ 2500 Π΄ΠΎ 3500 * 2.5 c) ΠΎΡ‚ 3500 Π΄ΠΎ 4500 * 3.5 d) ΠΎΡ‚ 4500 Π΄ΠΎ 5500 * 5.5 3) Π”Π²Π΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ…: a, b (Наши ΠΏΠΎΠ»Π·ΡƒΠ½ΠΊΠΈ "Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ ММР" ΠΈ "ΠšΠΎΠ½Π΅Ρ‡Π½Ρ‹ΠΉ ММР") 4) const = a < b (ΠŸΠΎΠ»Π·ΡƒΠ½ΠΎΠΊ "ΠšΠΎΠ½Π΅Ρ‡Π½Ρ‹ΠΉ ММР" всСгда большС Ρ‡Π΅ΠΌ "Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ ММР") НапримСр Π½Π°ΠΌ Ρ‡Π΅Ρ€Π΅Π· ajax ΠΏΠ΅Ρ€Π΅Π΄Π°Π»ΠΈΡΡŒ Ρ‚Π°ΠΊΠΈΠ΅ значСния: a = 1000 "Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ ММР" b = 5000 "ΠšΠΎΠ½Π΅Ρ‡Π½Ρ‹ΠΉ ММР" Найти: ΠžΠ±Ρ‰ΡƒΡŽ ΡΡ‚ΠΎΠΈΠΌΠΎΡΡ‚ΡŒ Π·Π° ΠΊΠ°ΠΆΠ΄ΡƒΡŽ Π΅Π΄ΠΈΠ½ΠΈΡ†Ρƒ "дистанции" ΠΎΡ‚ Ρ‚ΠΎΡ‡ΠΊΠΈ a Π΄ΠΎ Ρ‚ΠΎΡ‡ΠΊΠΈ b, ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½Π½ΡƒΡŽ Π½Π° коэффициСнты ΠΏΠΎ ΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΡŽ ΠΊ своим "ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΊΠ°ΠΌ" Π½Π° дистации. РСшСниС: 1)Найти ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΠΊ дистации ΠΌΠ΅ΠΆΠ΄Ρƒ a ΠΈ b b - a = 4000 (ΠŸΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΠΊ ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΏΠΎΠ»Π·ΡƒΠ½ΠΊΠ°ΠΌΠΈ "Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ ММР" ΠΈ "ΠšΠΎΠ½Π΅Ρ‡Π½Ρ‹ΠΉ ММР" - это ММР ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π½ΡƒΠΆΠ½ΠΎ Π·Π°Π±ΡƒΡΡ‚ΠΈΡ‚ΡŒ) 2) a)2500 - 1000(это Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎΠ»Π·ΡƒΠ½ΠΊΠ° "Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ ММР") * 2 = 3000 Ρ€ΡƒΠ± b)3500 - 2500 * 2.5 = 2500 Ρ€ΡƒΠ± c)4500 - 3500 * 3.5 = 3500 Ρ€ΡƒΠ± d)(это Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎΠ»Π·ΡƒΠ½ΠΊΠ° "ΠšΠΎΠ½Π΅Ρ‡Π½Ρ‹ΠΉ ММР")5000 - 4500 * 5.5 = 2750 Ρ€ΡƒΠ± a + b + c + d = 11750 Ρ€ΡƒΠ± 11750 / 4000 = ~ 2.9 (нашли срСдний коэффициСнт) 3) Найдя срСдний коэффициСнт ΠΈ Π½ΡƒΠΆΠ½ΡƒΡŽ Π΄ΠΈΡΡ‚Π°Π½Ρ†ΠΈΡŽ, ΠΌΡ‹ ΡƒΠΌΠ½ΠΎΠΆΠ°Π΅ΠΌ ΠΈΡ… ΠΈ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ сумму. Π•Π³ΠΎ я искал для Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ Π±Ρ‹ ΠΏΡ€ΠΈΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π² Π»ΠΎΠ³ΠΈΠΊΠ΅ PHP. 

I will be glad to any advice and tips, this is not an order, so there is no hurry, I am learning. Thank.

  • you'd better text your formula formulas. For so far it is clear that there is a current rating and a desired one. What happens next is best described in verbal form. - teran
  • why do you count on php? calculate everything on js and transfer the finished result to php - nueq
  • @nueq possible The problem is not what to count, but how to count? - Demian Shumilov
  • @teran I agree, I added. - Demian Shumilov
  • $e = $result ;) - E_p

1 answer 1

Without checking for borders and validation on JS, it will turn out:

https://jsfiddle.net/0ghrmo2t/2/

 // МоТно ΠΏΡ€Π°Π²ΠΈΠ»Π° ΠΌΠ΅Π½ΡΡ‚ΡŒ, ΠΊΠ°ΠΊ Ρ…ΠΎΡ‡Π΅ΡˆΡŒ! var limits = [ {"lower": 0, "upper": 2499, "coef" : 2 }, {"lower": 2500, "upper": 3499, "coef" : 2.5 }, {"lower": 3500, "upper": 4499, "coef" : 3.5 }, {"lower": 4500, "upper": 5500, "coef" : 5.5 } ]; function getCoef(cVal, nVal) { var diff = nVal - cVal; var tmp = 0; limits.forEach(function (val) { // Π²Π½Π΅ Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½Π°, Π½Π΅ считаСм. if (cVal > val.upper || nVal < val.lower) { return; } // Бколько ΠΏΠΎΠΏΠ°Π»ΠΎ Π² Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½. var l = Math.max (cVal, val.lower - 1); var u = Math.min (nVal, val.upper); tmp += (ul) * val.coef; }); //console.log(tmp, tmp/diff); // МоТно Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ сумму. // return tmp; // ΠΈΠ»ΠΈ коэффициСнт return tmp/diff; } getCoef(2400, 3500); getCoef(1000, 5000); 
  • Thank you, I will definitely try tomorrow! :) - Demian Shumilov