[英]Issue with php 2D array
i would like to store data from a form into 2D array but it seems to have problem inserting data into the array. 我想将数据从表单存储到2D数组中,但是将数据插入到数组中似乎有问题。 if i were to echo $orderArray[0][$count2] it seems to work but if i were to echo $orderArray[1][$count2] there will be error 如果我要回显$ 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++;
}
I would reduce the code to this: 我将代码简化为:
// 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]);
}
Also, please learn about PDO
or mysqli
; 另外,请了解PDO
或mysqli
; the old mysql_
functions are deprecated. 旧的mysql_
函数已被弃用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.