简体   繁体   English

如何从数据库查询中捆绑 output 以便将其写入文件

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

I have this code that gets records from an API and displays it on a page.我有这段代码从 API 获取记录并将其显示在页面上。 The code works and the records are displayed correctly.代码有效并且记录正确显示。

    $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>";

There are different users on the system and each has his/her own API key.系统上有不同的用户,每个用户都有自己的 API 密钥。 I have a cron job running every night that needs to run a file that will generate a report for each user.我有一个每天晚上运行的 cron 作业,它需要运行一个文件,该文件将为每个用户生成报告。 How can I bundle this output table data and write it to html files pls?如何捆绑此 output 表数据并将其写入 html 文件? In other words I don't want to display it I want to put all this table data with the variables into a single variable that I can write to a file.换句话说,我不想显示它我想将所有这些表数据与变量一起放入一个可以写入文件的变量中。

I have started writing the generate report function but am stuck at generating the html file:我已经开始编写生成报告 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];
    }
}

Somehow my question is not understood This gives me an error:不知何故我的问题不明白这给了我一个错误:

        $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>
        ";

Not an expert and the foreach statements is what I don't know how to include in the single string I want to write to a file.不是专家,我不知道如何将 foreach 语句包含在我要写入文件的单个字符串中。

What you need is something called file handling in programming.您需要的是在编程中称为文件处理的东西。 If you haven't worked with it, here is a tutorial to show you how its done.如果您还没有使用过它,这里有一个教程向您展示它是如何完成的。 You can take your variable containing the html structure and put it in the file_put_contents() .您可以将包含 html 结构的变量放入file_put_contents()中。 Here is the complete documentation for file_put_contents() .这是file_put_contents()的完整文档。

By the way this is a simple example of file handling using PHP:顺便说一下,这是一个使用 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);
?>

Edit :编辑

By understanding your requirement further, I came to realise you're having trouble echo-ing the php code (ie: foreach block).通过进一步了解您的要求,我意识到您无法回显 php 代码(即:foreach 块)。 For this, you can simply do what others have mentioned in the comments, echo all the rows into that variable with complete html markup instead of printing a foreach() loop and then put that whole markup into a file.为此,您可以简单地执行其他人在评论中提到的操作,使用完整的 html 标记将所有行回显到该变量中,而不是打印foreach()循环,然后将整个标记放入文件中。 Happy coding!快乐编码!

To get the data into a file you first need to have all the necessary data in a string.要将数据放入文件中,您首先需要在字符串中包含所有必要的数据。 Once you have that, you can write the string to a file.一旦你有了它,你就可以将字符串写入文件。

So instead of echoing your data, just assign it to a string variable instead:因此,与其回显您的数据,不如将其分配给一个字符串变量:

//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.

相关问题 我如何将字符串从AS3发送到PHP文件,以便可以用来执行数据库查询 - How can i send a String from AS3 to a PHP file so it can be use to do a database query 如何为该数据库设计编写MySQL查询? - How can I write a MySQL query for this database design? 如果我要从数据库中查询,我如何将数组中的数据 foreach 到 xmls 标签以便将消息发送到多个数字 - How can i foreach data from my array to an xmls tags so as to send message to multiple numbers if i would query from database PHP:设置权限以便我可以写文件? - PHP: Setting permissions so I can write a file? 如何安排此代码,以便可以从数据库中为每个表行获取不同的条目 - How can I arrange this code so I can get different entry from database for each table row 如何对“ object_id”数组进行排序,以便可以从数据库中获取相关值? - How can I sort an 'object_id' array so that I can get the relational values from the database? 如何从数据库中获取图像以输出到图库滑块? - How can I get images from the database to be output into a gallery slider? 如何从数据库输出多个结果(如果有)? - How can I output multiple results from a database (if there are any)? 如何在Laravel中原始编写查询? - How can I write the query as raw in Laravel? 如何使用 Eloquent 编写此查询 - How can I write this query with Eloquent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM