如何使这些字段从数据库自动填充? [英] How do I make these fields autopopulate from the database?

查看:210
本文介绍了如何使这些字段从数据库自动填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ items_pool = Array(
[0])我有一个数组,它包含这样的值: =>数组([id] => 2 [数量] => 1)
[2] => Array([id] => 72 [quantity] => 6)
[3] => Array )
[4] => Array([id] => 5 [quantity] => 1)
[5] => Array([id] => 7 [quantity] = 1)
[7] =>数组([id] => 1)
[6] =>数组([id] => 8 [quantity] => 9 [quantity] => 1)
[8] => Array([id] => 19 [quantity] => 1)
[9] =& ] => 20 [数量] => 1)
[10] =>数组([id] => 22 [数量] => 1) Array([id] => 29 [quantity] => 0)

接下来,我有一个表单,我试图填充。它循环遍历项目数据库,打印出所有可能的项目,并检查已经存在于$ items_pool中的项目。

  <?php foreach($ items ['items_poolpackage']  - > result()as $ item):?> 

< input type =checkboxname =measure [<?= $ item-> id?>] [checkmark]value =<?= $ item-> ; ID>>

<?php endforeach; ?>

我知道我在这里想要完成什么逻辑,但我无法弄清楚编程。



我在找的东西,写得松散的是这样的(不是真正的代码):

< input type =checkboxname =measure [<?= $ item-> id?>] [checkmark]value =<?= $ item-> id ?>中<?php if($ items_pool ['$ item-> id']){echoSELECTED;特别是通过数组通过数组,通过所有的键值(ID)和if
$ b

<?php if($ items_pool ['$ item-> id']){回声SELECTED; } else {}?>



我从这样的循环中了解到,它可能意味着很多'额外'处理。 / p>

TL; DR - 我需要在数组中循环,检查键'id',然后打印一个字符串。

解决方案

如果我理解正确,你需要类似这样的东西?

  array_walk($ items_pool,create_function('$ array','global $ item; if(in_array($ item-> id,$ array)){echochecked = \checked \ ;}')); 

考虑到这一点,如果您将产品存储在一维数组中(假定id始终是独特的

  $ items_pool = array(id,quantity)

您不会因重复输入产品而导致数量增加/减少。

  $ items_pool [id] ++; / *或* / $ items_pool [id] = $ items_pool [id] + 2; 

如果id存在于数组中,那么这两种技术都应该能够工作。



如果你可以在这个数组中存储除数量以外的其他attritubes,你可以

  $ items_pool = array(id => array (数量=> 3,颜色=>红色)); 
$ items_pool [id] [quantity] - ;


I have an array, which holds values like this:

$items_pool = Array ( 
[0] => Array ( [id] => 1 [quantity] => 1 ) 
[1] => Array ( [id] => 2 [quantity] => 1 ) 
[2] => Array ( [id] => 72 [quantity] => 6 ) 
[3] => Array ( [id] => 4 [quantity] => 1 )
[4] => Array ( [id] => 5 [quantity] => 1 ) 
[5] => Array ( [id] => 7 [quantity] => 1 ) 
[6] => Array ( [id] => 8 [quantity] => 1 ) 
[7] => Array ( [id] => 9 [quantity] => 1 ) 
[8] => Array ( [id] => 19 [quantity] => 1 ) 
[9] => Array ( [id] => 20 [quantity] => 1 ) 
[10] => Array ( [id] => 22 [quantity] => 1 ) 
[11] => Array ( [id] => 29 [quantity] => 0 ) 
) 

Next, I have a form that I am trying to populate. It loops through the item database, prints out all the possible items, and checks the ones that are already present in $items_pool.

<?php foreach ($items['items_poolpackage']->result() as $item): ?>

<input type="checkbox" name="measure[<?=$item->id?>][checkmark]" value="<?=$item->id?>"> 

<?php endforeach; ?>

I know what logically I'm trying to accomplish here, but I can't figure out the programming.

What I'm looking for, written loosely is something like this (not real code):

<input type="checkbox" name="measure[<?=$item->id?>][checkmark]" value="<?=$item->id?>" <?php if ($items_pool['$item->id']) { echo "SELECTED"; } else { }?>>

Specifically this conditional loop through the array, through all the key values (the ID) and if there's a match, the checkbox is selected.

<?php if ($items_pool['$item->id']) { echo "SELECTED"; } else { }?>

I understand from a loop structured like this that it may mean a lot of 'extra' processing.

TL;DR - I need to loop within the array, check for the key 'id', then print a string.

解决方案

If I understand correctly, you need something like this?

array_walk($items_pool, create_function('$array', 'global $item; if( in_array($item->id, $array) ) { echo "checked=\"checked\""; }'));

Conside this though, if you stored your products in a single dimensional array (presuming the ids will always be unique

$items_pool = array(id, quantity)

You will not end up with having duplicated entries of products and can incremement/decrement the quantity easier.

$items_pool[id]++; /* or */ $items_pool[id] = $items_pool[id] + 2;

Both techniques should work if the id exists in the array or not.

If you are hoping to store other attritubes in this array other than quantity, you can

$items_pool = array(id=>array(quantity=>3, colour=>"red"));    
$items_pool[id][quantity]--;

这篇关于如何使这些字段从数据库自动填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆