Home > Enterprise >  How to show SQL results in separate lines using PHP?
How to show SQL results in separate lines using PHP?

Time:06-03

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

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

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:

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?

CodePudding user response:

If you want to render it for HTML you've to use

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

If you want to have a line break in the HTML-source code too you can write

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

CodePudding user response:

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>

So we can re-word the problem more specifically:

I have a string separated by ,. I want it to be separated by <br> instead.

Thinking about other ways of rewording this, you might easily come up with:

I want to replace every , in my string with <br>

Then you look in the PHP manual, and you find a function called str_replace whose summary reads:

Replace all occurrences of the search string with the replacement string

I'll leave the actual PHP code to you; if you can't figure out from there, you should probably give up and become a farmer or something

  • Related