如何从数据库中获取值,并在输入字段中使用和显示它?

赫尔多金

我想创建两个输入字段。一个将保存一个Int,另一个将保存一个K-0001之类的值。保存这些输入字段并重新加载表单后,我想在字段中查看它们保存到数据库中的最后一个值。

我想这样做的原因是因为这样,我只需要更改最后一位数字就可以再次保存表格。问题是我不知道该怎么做。

更好的解释示例:
格式:

字段1:初值20000。我输入数字20001。
字段2:初值K-0000。我输入文字K-0001。

保存表单并重新加载后,我希望它看起来像这样。

字段1:显示值20001。我将其更改为20005。
字段2:显示值K-0001。我将其更改为K-0005。

然后再次:

字段1:显示值20005。我将其更改为20007。
字段2:显示值K-0005。我将其更改为K-0007。
等等。

我想我需要创建一个从数据库中获取值的函数。之后,我需要将它们放在“输入字段”中,至少这是我在想的。

码:

add.ctp

div class="customers form large-9 medium-8 columns content">
    <?= $this->Form->create($customer) ?>
    <fieldset>
        <legend><?= __('Neuen Kunden erstellen') ?></legend>
        <?php
            echo $this->Form->control('tour_id', ['options' => $tours, 'empty' => true]);
/*          echo $this->Form->control('order', array('label' => __('Bestellung', true))); */
            echo $this->Form->control('kdnr', array('label' => __('Kundennummer', true)));
            echo $this->Form->control('debinr', array('label' => __('Debitorennummer', true)));
            echo $this->Form->control('anrede', array('label' => __('Anrede', true)));
            echo $this->Form->control('name', array('label' => __('Name', true)));
            echo $this->Form->control('strasse', array('label' => __('Straße', true)));
            echo $this->Form->control('plz', array('label' => __('PLZ', true)));
            echo $this->Form->control('ort', array('label' => __('Ort', true)));
            echo $this->Form->control('tel', array('label' => __('Telefon', true)));
            echo $this->Form->control('kontonummer', array('label' => __('Kontonummer', true)));
            echo $this->Form->control('bankleitzahl', array('label' => __('Bankleitzahl', true)));
            echo $this->Form->control('lastschrift', array('label' => __('Lastschrift', true)));            
            echo $this->Form->control('detail', array('label' => __('Weitere Details', true)));
            echo $this->Form->control('betreuer_anrede', array('label' => __('Betreuer Anrede', true)));
            echo $this->Form->control('betreuer_name', array('label' => __('Betreuer Name', true)));
            echo $this->Form->control('betreuer_strasse', array('label' => __('Betreuer Straße', true)));
            echo $this->Form->control('betreuer_plz', array('label' => __('Betreuer PLZ', true)));
            echo $this->Form->control('betreuer_ort', array('label' => __('Betreuer Ort', true)));
            echo $this->Form->control('betreuer_on_bill', array('label' => __('Betreuer soll auf der Rechnung stehen', true)));
        ?>
    </fieldset>
    <?= $this->Form->button(__('Bestätigen')) ?>
    <?= $this->Form->end() ?>
</div>


CustomerTable.php

class CustomersTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('customers');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        $this->belongsTo('Tours', [
            'foreignKey' => 'tour_id'
        ]);
        $this->hasMany('Bills', [
            'foreignKey' => 'customer_id'
        ]);
        $this->hasMany('Orders', [
            'foreignKey' => 'customer_id'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmptyString('id', 'create');

        $validator
            ->integer('order')
            ->allowEmptyString('order');

        $validator
            ->scalar('kdnr')
            ->maxLength('kdnr', 45)
            ->allowEmptyString('kdnr');

        $validator
            ->scalar('debinr')
            ->maxLength('debinr', 31)
            ->allowEmptyString('debinr');

        $validator
            ->scalar('anrede')
            ->maxLength('anrede', 45)
            ->allowEmptyString('anrede');

        $validator
            ->scalar('name')
            ->maxLength('name', 45)
            ->allowEmptyString('name');

        $validator
            ->scalar('strasse')
            ->maxLength('strasse', 45)
            ->allowEmptyString('strasse');

        $validator
            ->integer('plz')
            ->allowEmptyString('plz');

        $validator
            ->scalar('ort')
            ->maxLength('ort', 45)
            ->allowEmptyString('ort');

        $validator
            ->scalar('tel')
            ->maxLength('tel', 45)
            ->allowEmptyString('tel');

        $validator
            ->boolean('lastschrift')
            ->allowEmptyString('lastschrift');

        $validator
            ->scalar('kontonummer')
            ->maxLength('kontonummer', 32)
            ->allowEmptyString('kontonummer');

        $validator
            ->integer('bankleitzahl')
            ->maxLength('bankleitzahl', 32)
            ->allowEmptyString('bankleitzahl');

        $validator
            ->scalar('detail')
            ->allowEmptyString('detail');

        $validator
            ->scalar('betreuer_anrede')
            ->maxLength('betreuer_anrede', 45)
            ->allowEmptyString('betreuer_anrede');

        $validator
            ->scalar('betreuer_name')
            ->maxLength('betreuer_name', 45)
            ->allowEmptyString('betreuer_name');

        $validator
            ->scalar('betreuer_strasse')
            ->maxLength('betreuer_strasse', 45)
            ->allowEmptyString('betreuer_strasse');

        $validator
            ->integer('betreuer_plz')
            ->allowEmptyString('betreuer_plz');

        $validator
            ->scalar('betreuer_ort')
            ->maxLength('betreuer_ort', 45)
            ->allowEmptyString('betreuer_ort');

        $validator
            ->boolean('betreuer_on_bill')
            ->allowEmptyString('betreuer_on_bill');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['tour_id'], 'Tours'));

        return $rules;
    }
}


我希望我能很好地解释我的目标,并且不要忘记一些事情。我仍在学习,因此,如果我放弃某些内容,请告诉我,然后我将编辑我的问题。

赫尔多金

找到我的解决方案!

我在控制器内创建了一个函数,以获取所需的值:

CustomersController.php

$query = $this->Customers->find('list', [
            'order' => ['Customers.kdnr' => 'DESC'],
            'valueField' => 'kdnr',
            'limit' => 1]);

        $kdnr = $query->first();
        $kdnr++;

        $query = $this->Customers->find('list', [
            'order' => ['Customers.debinr' => 'DESC'],
            'valueField' => 'debinr',
            'limit' => 1]);

        $debinr = $query->first();
        $debinr++;

        $this->set(compact('customer', 'tours', 'kdnr', 'debinr'));

之后,我只需要将$ kdnr和$ debinr添加到ctp中即可。

add.ctp

echo $this->Form->control('kdnr',['value' => $kdnr]);
echo $this->Form->control('debinr',['value' => $debinr]);

那就是我要做的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用存储过程从数据库中获取值并在 MessageBox C# 中显示?

如何从Joomla 2.5数据库列,字段名称和值中获取值

获取数据库输出以显示在输入字段中

从数据库中获取值并在Laravel中单击按钮后以表格形式显示

如何使用 JAVASCRIPT 从 Input 标签中获取值,输入标签的值是使用 php 从 mysql 数据库中获取的

使用 PHP 从动态输入字段获取值并存储到数据库

使用AsyncTask从数据库获取值并在Fragment中更改TextView值

如何从数据库中获取和显示数据?

CodeIgniter从数据库获取最新输入的行,并在控制器中使用它

如何从引导程序输入标签中获取值,然后插入到 mysql 数据库中

如何从数组列表中获取值并在 v-if 中使用它以在 VUEJS 中显示

如何从最近10天的数据库字段中获取值的数量

从mysql数据库获取值以在JSP中显示(JAVA)

如何从数据库中获取数据并在Kivy + Python中显示在表中

从数据库中获取数据并在AngularJS中的ng-repeat中使用它

如何从数据库中获取数据并在php中显示到表行跨度

如何从数据库中获取数据并在表单中显示以更新内容

如何使用WHERE子句和URL中的变量集从MySQL数据库使用PHP代码从MySQL数据库获取值

我如何不使用$ _GET而从数据库中获取信息并在页面中显示信息

使用laravel 4在下拉列表中显示数据库获取值

从数据库中获取数据并在laravel中显示数据库中的值

PHP在输入字段中获取数据库数据

如何从我的数据库中获取一个值并使用 cookie 或类似方法在页面上显示它?

如何从我的数据库中获取用户 ID 并使用 ASP.NET Core MVC 显示它

如何从qdateEdit获取用户输入并从postgres中的数据库中选择它

从SQL数据库中获取随机记录并显示它

使用php从mysql数据库中获取图像并在android的gridview中显示

如何根据id从数据库中获取值

firebase 如何从数据库(网站)中获取值