文本文件结构(表)
好吧,我知道最近我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CSV 对于
fputcsv
来说是微不足道的:CSV is trivial with
fputcsv
:好的,让我们看看如何做到这一点。我将重点关注相关的标题打印和显示格式化结果。那么,在这种情况下,我们将使用 fprintf 来将一些奇特的格式化文本打印到我们的相关文件中。基本上,这就是如何做到这一点:
首先,我们需要为这些字段设置一些宽度,以便所有内容都以良好的方式显示。我们将为每个字符设置 10 的宽度:
这个
%-10s
的作用是告诉我们,我们想要一个宽度为 10 个字符的字符串,如果长度不是,则使用空格进行填充。足够的。您可以将 10 调整为您想要的任何宽度,以获得最佳结果。结果是这样的:接下来我们打印出我们的分隔符,我只是稍微调整了长度,直到结果相同:
然后我们循环并打印出我们的值:
这会给我们这样的结果
:就是这样!以下是完整代码供参考:
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:
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:
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:Next we print out our divider, which I just tweaked the length a bit until it came out the same:
Then we loop through and print out our values:
Which will give us something like this:
And that's it! Here is the full code for reference: