文本文件结构(表)

发布于 2024-11-08 19:36:33 字数 473 浏览 0 评论 0原文

好吧,我知道最近我在 php 中编写查询并将其输出到文本文件方面得到了很多帮助。我终于掌握并学会了如何真正做到这一点(谢谢大家!)。下面的代码显示了我所拥有的:

现在我知道表结构(th/tr/td)在文本文件中不起作用。我想知道是否有人知道如何将其变成传统的“列”视图,例如:

id   ||   title   ||   keyword
================================
2    ||   bob     ||   jones
2    ||   bob     ||   jones
2    ||   bob     ||   jones
2    ||   bob     ||   jones

我不一定需要边框或任何东西,至少需要一种易于阅读的格式。这个输出的文件将由我工作的公司的谷歌产品使用(我相信他们需要一种易于阅读的格式,或者只是以表格形式设置的东西。我可以做一个 CSV,但我对然后我再次感谢。

Alright, I know I have had a lot of help on here recently with writing queries in php and outputting them to a text file. I have finally grasped and learned how to actually do that much (thanks guys!). The code below show what I do have:

Now I know the table structure (th/tr/td) does not work in a text file. I was wondering if anyone knew how to get this into a traditional "column" view like:

id   ||   title   ||   keyword
================================
2    ||   bob     ||   jones
2    ||   bob     ||   jones
2    ||   bob     ||   jones
2    ||   bob     ||   jones

I dont necessarily need borders or anything just at least an easy to read format. This outputted file will be used by google products for the company I work for (and they need an easy to read format I believe, or just something that is set up in a table form. I could do a CSV, but I know less about that then I do php. Thanks again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2024-11-15 19:36:33

CSV 对于 fputcsv 来说是微不足道的:

$fh = fopen('output.csv', 'w') or die("can't open file");

// output header and first row
$row = mysql_fetch_assoc($result)
fputcsv($fh, array_keys($row));
fputcsv($fh, $row);

// output the remaining rows
while ($row = mysql_fetch_assoc($result)) {
    fputcsv($fh, $row);
}

fclose($fh);

CSV is trivial with fputcsv:

$fh = fopen('output.csv', 'w') or die("can't open file");

// output header and first row
$row = mysql_fetch_assoc($result)
fputcsv($fh, array_keys($row));
fputcsv($fh, $row);

// output the remaining rows
while ($row = mysql_fetch_assoc($result)) {
    fputcsv($fh, $row);
}

fclose($fh);
被翻牌 2024-11-15 19:36:33

好的,让我们看看如何做到这一点。我将重点关注相关的标题打印和显示格式化结果。那么,在这种情况下,我们将使用 fprintf 来将一些奇特的格式化文本打印到我们的相关文件中。基本上,这就是如何做到这一点:

id   ||   title   ||   keyword

首先,我们需要为这些字段设置一些宽度,以便所有内容都以良好的方式显示。我们将为每个字符设置 10 的宽度:

fprintf($fh, "%-10s || %-10s || %-10s\n", "id", "title", "keyword");

这个 %-10s 的作用是告诉我们,我们想要一个宽度为 10 个字符的字符串,如果长度不是,则使用空格进行填充。足够的。您可以将 10 调整为您想要的任何宽度,以获得最佳结果。结果是这样的:

id         || title      || keyword

接下来我们打印出我们的分隔符,我只是稍微调整了长度,直到结果相同:

fprintf($fh, "===================================\n");

然后我们循环并打印出我们的值:

while ($row = mysql_fetch_assoc($result)) {
  fwrite($fh, "%-10s || %-10s || %-10s\n" $row['card_id'], $row['title'],  $row['description']);
}

这会给我们这样的结果

id         || title      || keyword   
===================================
2          || bob        || jones     
2          || bob        || jones     
2          || bob        || jones     
2          || bob        || jones   

:就是这样!以下是完整代码供参考:

<?php
// Make a MySQL Connection
 mysql_connect("mysql4.host.net", "user", "pass") or die(mysql_error()); 
 mysql_select_db("amyadele_test") or die(mysql_error()); 

// Query the database for data
$query = "SELECT card_id,title,description FROM cards";
$result = mysql_query($query);

// Open file for writing
$myFile = "test.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

// Loop through returned data and write (append) directly to file
fprintf($fh, "%-10s || %-10s || %-10s\n", "id", "title", "keyword");
fprintf($fh, "===================================\n");
while ($row = mysql_fetch_assoc($result)) {
  fprintf($fh, "%-10s || %-10s || %-10s\n", $row['card_id'], $row['title'], $row['description']);
}

// Close out the file
fclose($fh);
?>

Okay, let's see how we can do this. I'm just going to focus on the relevant header printing and displaying formatted results. So then, in this case we're going to use fprintf to print some fancy formatted text to our file in question here. So here's basically how will do this:

id   ||   title   ||   keyword

First off we need to make some widths for these fields so everything shows up in a nice fashion. We'll set a width of 10 for each:

fprintf($fh, "%-10s || %-10s || %-10s\n", "id", "title", "keyword");

What this %-10s does is tell us that we want a string formatted with a width of 10 characters, with spaces used for padding if the length is not enough. You can adjust the 10 to be whatever width you'd like in order to get the best result. The result comes out to something like this:

id         || title      || keyword

Next we print out our divider, which I just tweaked the length a bit until it came out the same:

fprintf($fh, "===================================\n");

Then we loop through and print out our values:

while ($row = mysql_fetch_assoc($result)) {
  fwrite($fh, "%-10s || %-10s || %-10s\n" $row['card_id'], $row['title'],  $row['description']);
}

Which will give us something like this:

id         || title      || keyword   
===================================
2          || bob        || jones     
2          || bob        || jones     
2          || bob        || jones     
2          || bob        || jones   

And that's it! Here is the full code for reference:

<?php
// Make a MySQL Connection
 mysql_connect("mysql4.host.net", "user", "pass") or die(mysql_error()); 
 mysql_select_db("amyadele_test") or die(mysql_error()); 

// Query the database for data
$query = "SELECT card_id,title,description FROM cards";
$result = mysql_query($query);

// Open file for writing
$myFile = "test.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

// Loop through returned data and write (append) directly to file
fprintf($fh, "%-10s || %-10s || %-10s\n", "id", "title", "keyword");
fprintf($fh, "===================================\n");
while ($row = mysql_fetch_assoc($result)) {
  fprintf($fh, "%-10s || %-10s || %-10s\n", $row['card_id'], $row['title'], $row['description']);
}

// Close out the file
fclose($fh);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文