簡體   English   中英

如何從數據庫查詢中捆綁 output 以便將其寫入文件

[英]How can I bundle output from a database query so I can write it to a file

我有這段代碼從 API 獲取記錄並將其顯示在頁面上。 代碼有效並且記錄正確顯示。

    $allrecords = array();
    for ($i = 0; $i <= 3; $i++) {
        $record = getData("https://api.site.com/v2/offers?page_size=100&page_number=" . $i);
        $allrecords[] = $record['offers'];
    }
    $date = date('d/m/Y');
    $items = $allrecords[0];
//TABLE WITH ALL THE PRODUCTS
    echo "<html><head><style>td{padding:5px}th {padding: 5px;background: #035;color: #fff;}</style></head>
    <body>
    <br/>
    <h3 class='pageDate'>" . $date . "</h3>
    <table id='reportsTable' style='width:80%;margin:20px auto'><thead>
    <tr id='reportRow'>
    <th class='image'></th>
    <th class='title'>Title</th>
    <th class='small'>Price</th>
    <th class='small'>CPT</th>
    <th class='small'>JHB</th>
    </tr>
    </thead>";
    foreach ($items as $key => $row) {
        foreach ($row['stock'] as $val) {
            foreach ($val['warehouse'] as $value) {
                if ($value == 1) {
                    $cpt = $val['quantity_available'];
                }
            }
        };
        foreach ($row['stock'] as $key => $val) {
            foreach ($val['warehouse'] as $key => $value) {
                if ($value == 3) {
                    $jhb = $val['quantity_available'];
                }
            }
        };
        echo
        "<tr>
    <td class='num centerCol'><img src='placeholder.png'/></td>
    <td class='productTitle'>" . $row['title'] . "</td>
    <td class='small centerCol' style='text-align:center'>" . 'R' . $row['selling_price'] . "</td>
    <td class='small centerCol' style='text-align:center'>" . $cpt . "</td>
    <td class='small centerCol' style='text-align:center'>" . $jhb . "</td>
    </tr>";
    }
    echo "</table></body></html>";

系統上有不同的用戶,每個用戶都有自己的 API 密鑰。 我有一個每天晚上運行的 cron 作業,它需要運行一個文件,該文件將為每個用戶生成報告。 如何捆綁此 output 表數據並將其寫入 html 文件? 換句話說,我不想顯示它我想將所有這些表數據與變量一起放入一個可以寫入文件的變量中。

我已經開始編寫生成報告 function 但我一直無法生成 html 文件:

function generateReports($con)
{
    $query = "select username, apikey from users";
    $result = mysqli_query($con, $query);
    while ($row = mysqli_fetch_assoc($result)) {
        $user = $row['username'];
        $key = $row['apikey'];
        $date = date('dm');
        $filename = $user . '/' . $date . '.html';
        $allrecords = array();
        for ($i = 0; $i <= 3; $i++) {
            $record = getData("https://seller-api.takealot.com/v2/offers?page_size=100&page_number=" . $i, $key);
            $allrecords[] = $record['offers'];
        }
        $items = $allrecords[0];
    }
}

不知何故我的問題不明白這給了我一個錯誤:

        $content = "
        <html><head><style>td{padding:5px}th {padding: 5px;background: #035;color: #fff;}</style></head>
        <body>
        <br/>
        <h3 class='pageDate'>" . $date . "</h3>
        <table id='reportsTable' style='width:80%;margin:20px auto'><thead>
        <tr id='reportRow'>
        <th class='image'></th>
        <th class='title'>Title</th>
        <th class='small'>Price</th>
        <th class='small'>CPT</th>
        <th class='small'>JHB</th>
        </tr>
        </thead>"
        foreach ($items as $key => $row) {
            foreach ($row['stock_at_takealot'] as $val) {
                foreach ($val['warehouse'] as $value) {
                    if ($value == 1) {
                        $cpt = $val['quantity_available'];
                    }
                }
            };
            foreach ($row['stock_at_takealot'] as $key => $val) {
                foreach ($val['warehouse'] as $key => $value) {
                    if ($value == 3) {
                        $jhb = $val['quantity_available'];
                    }
                }
            };
        "<tr>
        <td class='num centerCol'><img src='placeholder.png'/></td>
        <td class='productTitle'>" . $row['title'] . "</td>
        <td class='small centerCol' style='text-align:center'>" . 'R' . $row['selling_price'] . "</td>
        <td class='small centerCol' style='text-align:center'>" . $cpt . "</td>
        <td class='small centerCol' style='text-align:center'>" . $jhb . "</td>
        </tr>;
        }
        </table></body></html>
        ";

不是專家,我不知道如何將 foreach 語句包含在我要寫入文件的單個字符串中。

您需要的是在編程中稱為文件處理的東西。 如果您還沒有使用過它,這里有一個教程向您展示它是如何完成的。 您可以將包含 html 結構的變量放入file_put_contents()中。 這是file_put_contents()的完整文檔。

順便說一下,這是一個使用 PHP 處理文件的簡單示例:

<?php
    $file = 'path/where/you/need/file/index.html';
    $content = "<h1>Hello World!</h1>";

    // Write the contents in to the file
    file_put_contents($file, $content);
?>

編輯

通過進一步了解您的要求,我意識到您無法回顯 php 代碼(即:foreach 塊)。 為此,您可以簡單地執行其他人在評論中提到的操作,使用完整的 html 標記將所有行回顯到該變量中,而不是打印foreach()循環,然后將整個標記放入文件中。 快樂編碼!

要將數據放入文件中,您首先需要在字符串中包含所有必要的數據。 一旦你有了它,你就可以將字符串寫入文件。

因此,與其回顯您的數據,不如將其分配給一個字符串變量:

//create a string with an initial value.
$content = "
    <html><head><style>td{padding:5px}th {padding: 5px;background: #035;color: #fff;}</style></head>
    <body>
    <br/>
    <h3 class='pageDate'>" . $date . "</h3>
    <table id='reportsTable' style='width:80%;margin:20px auto'><thead>
    <tr id='reportRow'>
    <th class='image'></th>
    <th class='title'>Title</th>
    <th class='small'>Price</th>
    <th class='small'>CPT</th>
    <th class='small'>JHB</th>
    </tr>
    </thead>";

foreach ($items as $key => $row) {
    foreach ($row['stock_at_takealot'] as $val) {
        foreach ($val['warehouse'] as $value) {
            if ($value == 1) {
                $cpt = $val['quantity_available'];
            }
        }
    };

    foreach ($row['stock_at_takealot'] as $key => $val) {
        foreach ($val['warehouse'] as $key => $value) {
            if ($value == 3) {
                $jhb = $val['quantity_available'];
            }
        }
    };

    //carry on adding to the string as needed
    $content .= "<tr>
    <td class='num centerCol'><img src='placeholder.png'/></td>
    <td class='productTitle'>" . $row['title'] . "</td>
    <td class='small centerCol' style='text-align:center'>" . 'R' . $row['selling_price'] . "</td>
    <td class='small centerCol' style='text-align:center'>" . $cpt . "</td>
    <td class='small centerCol' style='text-align:center'>" . $jhb . "</td>
    </tr>";
}

$content .= "</table></body></html>";

//now write the finished string to a file
file_put_contents("text.txt", $content);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM