doulong2782 2016-09-16 16:06
浏览 69
已采纳

php数组中有多个变量

In a form within a sqlsrv_fetch_array I have

<select class="styled-select" name="StockArray['.$row1['ProductID'].']" required><option selected></option><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option></select>

and on the page after submitting that form I have this foreach to construct an insert.

$InsertDate = date("Y-m-d H:i:s");

foreach ($_POST['StockArray'] as $ProductID => $Qty) {$InsertList .= "('".$ProductID."', '".$Qty."', '".$InsertDate."', '1'),";}
$InsertList = rtrim($InsertList, ',');

What I would like to pass is an extra variable within $_POST['StockArray'] called $row1['LastQty'] so I can add an additional column in my insert. I don't think that is technically possible for an array to have more than just one value per key but I'm here to see if there is a workaround for that for what I'm trying to do.

  • 写回答

1条回答 默认 最新

  • doufan1363 2016-09-16 16:50
    关注

    You can by encoding/decoding the variables. So, for example if your productID is 4 and you want to encode a LocationID (let say 28) then you can name the variable like this: P4L28, so in php:

    $sel = '<select class="styled-select" name="' . "P{$row1['ProductID']}L{$row1['LocationID']}" // and so on...
    

    Then you will need to look into the $_POST for each combination:

    foreach ($_POST as $code => $qty) {
        echo "$code => $qty <br>" ;    
    }
    

    If you just need to output the lastQty in the form then you can just print it as plain HTML, because you won't need it back in the $_POST.

    <select ...><p>Last quantity: <?php echo $row1['lastQty'] ; ?></p>
    
    
        . 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么