用于下载csv文件的PHP if-else条件

时间:2014-06-02 05:06:02

标签: php csv if-statement shopify

这是我的代码:

if (isset($t)) {
    $orders               = $sc->call('GET', 'admin/orders.json', array(
        'published_status' => 'published'
    ));
    $myarray              = array();
    $list[]               = array(
 'Name',
        'Email');
    $list                 = array_filter($list);
if ($lineitemcount == 0) {
                array_push($list, array(
                    $order['name'],
                    $order['email']));
$order  = $_GET;
    $list[] = array_to_csv_download($myarray, // this array is going to be the second row
        "numbers.csv");
    $list   = array_filter($list);
    array_to_csv_download($list);
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=file.csv");
    header("Pragma: no-cache");
}
function array_to_csv_download($list)
{
    $output = fopen("php://output", "w");
    foreach ($list as $row) {
        fputcsv($output, $row);
        //print_r($row); die;
    }
    fclose($output);

现在我的情况是,当点击csv应用程序时,它会直接显示下载链接,无论是否有订单。

我只是感到困惑,上面的代码是否正确?

如何解决这个问题?

有人可以帮助我吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

正如评论已经解释的那样,你将$there_are_orders设置为if语句之前的空字符串,因此你总是会得到“没有订单”。

首先需要确定商店中是否有订单,并相应地设置$there_are_orders。请参阅Shopify API documentation for how to get a count of all the orders

  

统计所有订单

GET /admin/orders/count.json

HTTP/1.1 200 OK

{  
    "count": 1  
}

修改

一个例子:

$there_are_orders = $sc->call('GET', 'admin/orders/count.json');

if($there_are_orders) {
    echo '<a href="index-oauth29.php">Download</a>';
} else {
   echo 'There are no orders';
}

答案 1 :(得分:0)

在catalog-generator.php中使用此代码,它可能适合您

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="Stylesheet" type="text/css" />
<title>Catalog</title>
</head>
<body>
<div class="page">
<h1>CSV Generator</h1>
<?php

require_once 'lib/shopify.php';
$t="bac2486aa2b31aa5aed1fdd62e77a4ae";
$sc = new ShopifyClient("mine-329.myshopify.com/", $t, API_KEY, SECRET);
 $orders = $sc->call('GET', 'admin/orders.json', array('published_status'=>'published'));

$there_are_orders=count($orders);//count($orders if get you the count of orders)
if($there_are_orders!=0){
    echo '<a href="index-oauth29.php">Download</a>';
} else {
   echo 'There is no orders';
}
?>
</div>
</body>
</html>