我想将表单中的数据存储到2D数组中 但似乎有问题将数据插入数组。 如果我要回应$ orderArray [0] [$ count2]它似乎工作 但如果我要回复$ orderArray [1] [$ count2]那么会有错误
$dateArray = array();
$orderArray = array(array());
$amountArray = array(array());
$count = 0;
$count2 = 0;
foreach ($_POST['export'] as $date){
$dateArray[$count] = $date;
include "/storescript/connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM ordera WHERE orderDate = '$date' ORDER BY orderid ASC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$orderArray[$count][$count2] = $row["orderAmount"];
$amountArray[$count][$count2] = $row["itemAmount"];
$count2++;
}
}
$count++;
}
答案 0 :(得分:0)
我会将代码缩减为:
// connect here
include "/storescript/connect_to_mysql.php";
// make date list safe for querying
$dates = join(',', array_map(function($date) {
return sprintf("'%s'", mysql_real_escape_string($date));
}, $_POST['export']);
// run query
$sql = "SELECT * FROM ordera WHERE orderDate IN ($dates) ORDER BY orderid";
$res = mysql_query($sql) or die("Error in query");
// collect results
while ($row = mysql_fetch_array($res)) {
$orders[$date][] = $row['orderAmount'];
$amounts[$date][] = $row['itemAmount'];
}
// do stuff with results
foreach ($orders as $date => $orderAmounts) {
print_r($orderAmounts);
print_r($amounts[$date]);
}