文本文件结构(表) [英] Textfile Structure (tables)

查看:165
本文介绍了文本文件结构(表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道最近在php中编写查询并将其输出到文本文件对我有很多帮助.我终于掌握并学习了如何实际做(谢谢大家!).下面的代码显示了我所拥有的:

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:

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

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

我不一定需要边框或至少是一种易于阅读的格式.此输出文件将由Google产品用于我工作的公司(我相信他们需要一种易于阅读的格式,或者只是一种以表格形式设置的格式.我可以使用CSV,但我所了解的很少那我就用PHP.再次感谢.

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.

推荐答案

好的,让我们看看如何做到这一点.我将只专注于相关的标题打印和显示格式化的结果.因此,在这种情况下,我们将使用 fprintf 进行打印一些花式格式的文本到我们正在讨论的文件中.所以这基本上是如何做到的:

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

首先,我们需要为这些字段设置一些宽度,以便所有内容都能以一种不错的方式显示出来.我们将每个宽度设置为10:

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

%-10s的作用是告诉我们,我们想将一个字符串格式化为10个字符的宽度,如果长度不够,请使用空格填充.您可以将10调整为任意宽度,以获得最佳效果.结果出来像这样:

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);
?>

这篇关于文本文件结构(表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆