dqphg40600 2011-11-25 10:29
浏览 61
已采纳

将数据存储在关联数组php中

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.

  • 写回答

2条回答 默认 最新

  • douluo1330 2011-11-25 10:40
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计