【发布时间】:2019-09-08 17:20:45
【问题描述】:
您好,我有这个问题,我想从 textarea1 获取输入值,例如:第一行的“300pcs, 200$”和“500pcs, 400$”等等,以执行增加基于件数的价格(我曾经有输入并且它有效,但我被告知我需要一个文本区域)。计算结果类似于: textarea2 "300pcs,220$" 和第 2 行 "500pcs, 540$" 增加了适当的百分比。我的问题是,我需要从文本区域的每一行中提取数字来执行计算,但它似乎并没有像想象的那样工作..
declare(strict_types=1);
namespace App\Presenters;
use App\Forms\FormFactory;
use Nette\Application\UI\Form;
use Nette\Database\Context;
use Nette\Utils\ArrayHash;
class HomepagePresenter extends BasePresenter
{
/**
* @var Context
* @inject
*/
public $database;
/**
* @var FormFactory
* @inject
*/
public $formFactory;
protected function createComponentCalculationForm(): Form
{
$form = $this->formFactory->create();
$form->addSelect('supplier', 'Dodavatel:', $this->database->table('suppliers')->fetchPairs('id', 'supp_name'));
$form->addTextArea('user_input');
$form->addSubmit('calculate', 'Spočítat');
$form->addTextArea('result')
->setHtmlAttribute('class', 'totalPrice')
->setHtmlAttribute('readonly');
$form->onSuccess[] = function (Form $form, ArrayHash $values): void {
$selectedSupp = $values->supplier;
$userInput = $values->user_input;
$lines = $array = explode("\n", $userInput);
foreach ($lines as $line) {
preg_match_all('/[\d]+/', $line, $numbers);
list($quantity, $price) = $numbers[0];
}
$modifier = $this->database->fetch('SELECT percentage FROM prices WHERE supplier_id = ? AND max >= ? AND min <= ?', $selectedSupp, $quantity, $quantity);
$total = round($price + ($price / 100) * $modifier->percentage);
$form['result']->setValue("$quantity Ks, $total Kč + DPH");
};
return $form;
}
}
【问题讨论】: