繁体   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