无法将从数据库中检索到的信息插入到表单中
Unable to insert the information retrieved from a database inside a form
我是 Nette 和 php 框架的新手。
我正在尝试创建一个带有 selection 菜单的表单,该菜单从数据库中的列中获取 select 可用选项的列表。
<?php
declare(strict_types=1);
namespace App\Presenters;
use Nette\Application\UI;
class HomepagePresenter extends UI\Presenter
{
/** @var Nette\Database\Context */
private $database;
public function __construct(\Nette\Database\Context $database)
{
$this->database = $database;
}
protected function createComponentCalculationForm(): UI\Form
{
$result=$this->database->query('SELECT supp_name FROM suppliers');
foreach($result as $supplier){
$supplierList[]=$supplier;
}
$form = new UI\Form;
$form->addSelect('supplier', 'Dodavatel:',$supplierList);
$form->addText('quantity', 'Ks')
->setRequired()
->addRule($form::INTEGER,"Hodnota musí být číslo" )
->addRule($form::MIN,'Číslo musí být kladné!',0);
$form->addText('price', 'Kč')
->setRequired()
->addRule($form::INTEGER,"Hodnota musí být číslo" )
->addRule($form::MIN,'Číslo musí být kladné!',0);
$form->addButton('calculate', 'Spočítat')
->setHtmlAttribute('onclick', 'calculatePrice()');
$form->addTextArea('result');
return $form;
}
}
我希望 $form select 菜单包含供应商列表
$supplier
变量将包含 Nette\Database\Row
, which you store into the $supplierList
array. Form::addSelect
expects 数组,其中值是可以字符串化的东西。由于 Row
不喜欢被字符串化,因此您需要在列表中添加一些字符串化的内容。提取列应该有效:
$result = $this->database->query('SELECT supp_name FROM suppliers');
foreach($result as $supplier){
$supplierList[] = $supplier['supp_name'];
}
我是 Nette 和 php 框架的新手。 我正在尝试创建一个带有 selection 菜单的表单,该菜单从数据库中的列中获取 select 可用选项的列表。
<?php
declare(strict_types=1);
namespace App\Presenters;
use Nette\Application\UI;
class HomepagePresenter extends UI\Presenter
{
/** @var Nette\Database\Context */
private $database;
public function __construct(\Nette\Database\Context $database)
{
$this->database = $database;
}
protected function createComponentCalculationForm(): UI\Form
{
$result=$this->database->query('SELECT supp_name FROM suppliers');
foreach($result as $supplier){
$supplierList[]=$supplier;
}
$form = new UI\Form;
$form->addSelect('supplier', 'Dodavatel:',$supplierList);
$form->addText('quantity', 'Ks')
->setRequired()
->addRule($form::INTEGER,"Hodnota musí být číslo" )
->addRule($form::MIN,'Číslo musí být kladné!',0);
$form->addText('price', 'Kč')
->setRequired()
->addRule($form::INTEGER,"Hodnota musí být číslo" )
->addRule($form::MIN,'Číslo musí být kladné!',0);
$form->addButton('calculate', 'Spočítat')
->setHtmlAttribute('onclick', 'calculatePrice()');
$form->addTextArea('result');
return $form;
}
}
我希望 $form select 菜单包含供应商列表
$supplier
变量将包含 Nette\Database\Row
, which you store into the $supplierList
array. Form::addSelect
expects 数组,其中值是可以字符串化的东西。由于 Row
不喜欢被字符串化,因此您需要在列表中添加一些字符串化的内容。提取列应该有效:
$result = $this->database->query('SELECT supp_name FROM suppliers');
foreach($result as $supplier){
$supplierList[] = $supplier['supp_name'];
}