简体   繁体   English

如何使用 PHP 在单独的行中显示 SQL 结果?

[英]How to show SQL results in separate lines using PHP?

So, I have this little search engine Im working on, where I can look for error messages, and as a result, I get links to known solutions, and other information It's something I use for my work as tech support engineer I spend most of my days just googling things and looking into different internal locations for solutions to known issues I wanted to build a one stop shop where I can enter an error code, and I get all related information to it因此,我正在开发这个小型搜索引擎,我可以在其中查找错误消息,因此,我可以获得指向已知解决方案和其他信息的链接我的日子只是在谷歌上搜索东西并寻找不同的内部位置以解决已知问题 我想建立一个一站式商店,我可以在其中输入错误代码,并获得所有相关信息

For this I created a database, which has 4 columns: Error Name: the error code I'm looking for KBs: knowledge base articles related to that error Other Links: links to non-company sites with solutions to the same error Cases: internal case numbers为此,我创建了一个数据库,它有 4 列: 错误名称:我正在寻找的错误代码 KB:与该错误相关的知识库文章 其他链接:指向具有相同错误解决方案的非公司网站的链接 案例:内部案例编号

So, if I search for Error1, I get that information, and it's fine if I just have one link for each one of them, but usually, I have multiple sources for any named error因此,如果我搜索 Error1,我会得到该信息,如果我对它们中的每一个只有一个链接,那很好,但通常,对于任何命名错误,我都有多个来源

So, on the database, for example I have the column Cases, populated with several case numbers (separated by a comma) And the php I have right now, is showing me the results as:因此,在数据库中,例如,我有列 Cases,其中填充了几个案例编号(用逗号分隔)而我现在拥有的 php 向我显示了结果:

123,234,345,456,567

And I would like to display that as:我想将其显示为:

123
234
345
456
567

The code I have for this is:我为此提供的代码是:

$sql = "SELECT errorname, kbs, otherinfo, cases FROM issues WHERE id = $id";

<?php echo $row['cases']; ?>

Any suggestions?有什么建议么?

One of the most important skills in programming is breaking the problem down .编程中最重要的技能之一就是分解问题

All the details about the database, and why you're building it, are completely irrelevant to the current problem you're trying to solve, which can be written as:关于数据库的所有细节,以及你为什么要构建它,与你试图解决的当前问题完全无关,可以写成:

I have a list of items in a comma-separated string.我有一个逗号分隔字符串中的项目列表。 I want to display each item on a separate line.我想在单独的行上显示每个项目。

What is relevant is where you're viewing the output;相关的您在哪里查看输出; I'm going to guess you're outputting HTML, so all you really need to know is that a line break in HTML is spelled <br>我猜你正在输出 HTML,所以你真正需要知道的是HTML 中的换行符拼写为<br>

So we can re-word the problem more specifically:所以我们可以更具体地重新表述这个问题:

I have a string separated by , .我有一个由,分隔的字符串。 I want it to be separated by <br> instead.我希望它被<br>分隔。

Thinking about other ways of rewording this, you might easily come up with:考虑一下其他的改写方式,您可能很容易想到:

I want to replace every , in my string with <br>我想用<br>替换我的字符串中的每个,

Then you look in the PHP manual, and you find a function called str_replace whose summary reads:然后您查看 PHP 手册,您会发现一个名为str_replace的函数,其摘要如下:

Replace all occurrences of the search string with the replacement string用替换字符串替换所有出现的搜索字符串

I'll leave the actual PHP code to you;我将把实际的 PHP 代码留给你; if you can't figure out from there, you should probably give up and become a farmer or something 😉如果你不能从那里弄清楚,你可能应该放弃并成为一名农民或其他东西😉


There are of course other variations of the problem, such as:当然,问题还有其他变体,例如:

For each item, I want to output an item in a bulleted list对于每个项目,我想在项目符号列表中输出一个项目

Which leads you to:这会导致您:

If you want to render it for HTML you've to use如果你想为HTML渲染它,你必须使用

<?php echo $row['cases'] . '<br>'; ?>

If you want to have a line break in the HTML-source code too you can write如果您也想在 HTML 源代码中换行,您可以编写

<?php echo $row['cases'] . "<br>\n"; ?>

If you want to render it for Text files you've to use this:如果你想为文本文件渲染它,你必须使用这个:

<?php echo $row['cases'] . "\n"; ?>

For other file formats it might have to be noted differently but you've to define which file-format you use then.对于其他文件格式,可能需要以不同的方式注明,但您必须定义您使用的文件格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM