将PHP代码下载到excel文件中-仅显示查询的第一条记录,不返回所有记录

时间:2018-10-20 14:52:23

标签: php html excel

我有这段代码应该在Microsoft Excel内的每个记录中显示每个记录。 但是,即使我有一个while循环,它也只会将第一个记录放入excel,因此所有记录都将被插入。但是它不起作用,我的代码中是否有任何问题?还是循环不会像这样工作?我以前做过,但没有任何问题,但是由于某种原因,这是行不通的。

<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$filename = "Charge Report";
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: filename=".$filename.".csv");
if($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT
first_name, 
orders.user_id AS 'user_id',
last_name, 
email, 
city,
line1,
line2,
delivery_zip, 
orders.id AS 'orders_id',
charge_status,
subtotal,
charge_date, 
delivery_fee,
tax,
tax_amount,
total,
stripe_customer_id,
discount_code_id,
discount
FROM
    orders
INNER JOIN subscriptions ON orders.subscription_id = subscriptions.id
INNER JOIN users on subscriptions.user_id = users.id
LEFT JOIN addresses on users.id = addresses.user_id
ORDER BY users.id";


$result = mysqli_query($conn,$query);
?>
Client ID,First/Last Name,Email,Address,City,Zipcode,Order ID,Charge Date,Charge Status,Subtotal,Delivery Fee, Delivery Fee Discount,Discount Code, Discount Type,Promo Code Discount Amount,Tax Rate, Tax Amount,Net Charge Amount,Net Total Amount,Total Amount, Number of Meals,Client ID Stripe
<?php 
while ($row = mysqli_fetch_assoc($result)) 
{
$name = $row['first_name']. " " . $row['last_name'];
$address = $row['line1']. " " . $row['line2'];
$subtotal = $row['subtotal'];
$delivery_fee = $row['delivery_fee'];

if($subtotal >= 30000)
{
    $delivery_fee_discount = $delivery_fee + 1000;
}
elseif($subtotal >= 20000)
{
    $delivery_fee_discount = $delivery_fee;
}
elseif($subtotal >= 10000)
{
    $delivery_fee_discount = 1000;
}
else{
    $delivery_fee_discount = 0;
}
$tax_amount = $row['tax_amount'];
$total = $row['total'];
$discount_code_id = $row['discount_code_id'];
$net_total_amount = $total - $tax_amount;
$discount = $row['discount'];

$total_amount = $subtotal + $tax_amount +$delivery_fee;

$query_discount="select * from discount_codes where id = '{$discount_code_id}'";
$result = mysqli_query($conn,$query_discount);
$row_discount = mysqli_fetch_assoc($result);
$discount_name = $row_discount['code'];
$discount_type = $row_discount['discount_type'];


$discount_amount = $discount - $delivery_fee_discount;
if($discount <= 0)
{
    $discount_amount = 0;

}
if($discount_type == 3)
{
    $discount_amount = $row['delivery_fee'];
}

$get_orders= "select SUM(num_meals) as `meal_total` from orders left join deliveries on orders.id = deliveries.order_id where orders.id = '{$row['orders_id']}'";
$r_get_orders = mysqli_query($conn, $get_orders);
$qty = mysqli_fetch_assoc($r_get_orders);

$meal_counter = $qty['meal_total'];
$user_id = $row['user_id'];
$user_id = (explode(" ",$user_id));

?>
    <?=@$user_id[0] ?>,<?=$name ?>,<?=$row['email'] ?>,<?=$address?>,<?=$row['city']?>,<?=$row['delivery_zip']?>,<?=$row['orders_id']?>,<?=$row['charge_date']?>,<?=$row['charge_status']?>,<?=$subtotal?>,<?=$delivery_fee?>,<?=$delivery_fee_discount?>,<?=$discount_name?>,<?=$discount_type?>,<?=$discount_amount?>,<?=$row['tax']?>,<?=$row['tax_amount']?>,<?=$row['total']?>,<?=$net_total_amount?>,<?=$total_amount?>,<?=$meal_counter?>,<?=$row['stripe_customer_id']?>

<?php
}
?>

1 个答案:

答案 0 :(得分:1)

您在第二个查询中覆盖$result 内部 while($row = mysqli_fetch_assoc($result)) {}

$result = mysqli_query($conn,$query_discount);

因此,在此处及其下一行将其重命名为类似$result_discount的内容:

$result_discount = mysqli_query($conn,$query_discount);
$row_discount = mysqli_fetch_assoc($result_discount);