将数据存储在关联数组php中 [英] storing data in associative array php

查看:99
本文介绍了将数据存储在关联数组php中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这里的代码:

 <?php while($ rowequipment = mysql_fetch_assoc($ sqlequipment)){
echo'< input type =checkboxname =equipment []value ='。$ rowequipment ['equipmentid']。'/> < input type =textname =count []id =count []size =3value =/>'
。$ rowequipment ['description']。 ; br /
}?>

这会创建一个复选框列表,以选择用户想要的设备和文本框需要。



有可能以某种方式将数据存储在php中的关联数组中,以便我不能将其传输到数据库中?例如:

 设备计数
1 2
3 1


谢谢。

>

解决方案

您可以:

  < form method =post> 
<?php
$ row [0] ['equipmentid'] = 1;
$ row [0] ['description'] =test1;
$ row [1] ['equipmentid'] = 2;
$ row [1] ['description'] =test2;

foreach($ row as $ rowequipment)
{
$ e_id = $ rowequipment ['equipmentid'];
echo'< input type =checkboxname =equipment ['。$ e_id。'] [id]value ='$ e_id。'/> ';
echo'< input type =textname =equipment ['。$ e_id。'] [count]id =count'。$ e_id。'size =3value = /> ';
echo rowequipment ['description']。< br />;
}
?>
< input type =submitvalue =aa/>
< / form>

,结果将是:

  array(1){
[equipment] =>
array(2){
[1] =>
array(2){
[id] =>
string(1)1
[count] =>
string(1)4
}
[2] =>
array(2){
[id] =>
string(1)2
[count] =>
string(1)6
}
}
}

现在改变你的foreach,你会有相同的结果。


I have this code here:

<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) {
echo '<input type="checkbox" name="equipment[]" value="'.$rowequipment['equipmentid'].'"/>    <input type="text" name="count[]" id="count[]" size="3" value=""/>'
.$rowequipment['description']."<br />";
}?>

This creates a list of checkboxes to select which equipment the user wants and text boxes to put in the number required.

Is it possible to somehow store the data in an associative array in php so that i cant then transfer this into a database? for example:

equipmentid    count
1              2
3              1

Or is there a different way to do this.

Thanks.

解决方案

You can do:

<form method="post">
<?php
$row[0]['equipmentid'] = 1;
$row[0]['description'] = "test1";
$row[1]['equipmentid'] = 2;
$row[1]['description'] = "test2";

foreach($row as $rowequipment)
{
    $e_id = $rowequipment['equipmentid'];
    echo '<input type="checkbox" name="equipment['.$e_id.'][id]" value="'.$e_id.'"/> ';
    echo '<input type="text" name="equipment['.$e_id.'][count]" id="count'.$e_id.'" size="3" value=""/> ';
    echo rowequipment['description']."<br />";
}
?>
<input type="submit" value="aa"/>
</form>

and the result will be:

array(1) {
  ["equipment"]=>
  array(2) {
    [1]=>
    array(2) {
      ["id"]=>
      string(1) "1"
      ["count"]=>
      string(1) "4"
    }
    [2]=>
    array(2) {
      ["id"]=>
      string(1) "2"
      ["count"]=>
      string(1) "6"
    }
  }
}

now change the foreach with your while and you will have the same result.

这篇关于将数据存储在关联数组php中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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