-2

I have below code which gives output as a single line. Please help me to get array value to new row. I have tried using </br> or <br> but no use.

Code:

if($_SERVER['REQUEST_METHOD'] == "POST") {
    $string = "copy \\\\plm\\tt\\data\\";
}

$des=$_POST["tester"];
if($des=="") {
    // if ALL is selected in Dropdown box
    $res=mysqli_query($conn, "SELECT * FROM workflow1");
    while($r=mysqli_fetch_row($res))
    {
        echo "$string$r[4]";
    }
}

Current output:

  copy \\plm\tt\data\event3copy \\plm\tt\data\event5copy \\plm\tt\data\event4

Desired output:

  copy \\plm\tt\data\event3
  copy \\plm\tt\data\event5
  copy \\plm\tt\data\event4
Tony Chiboucas
  • 5,505
  • 1
  • 29
  • 37
SSG
  • 73
  • 1
  • 10
  • 1
    Possible duplicate of [PHP - how to create a newline character?](https://stackoverflow.com/questions/4238433/php-how-to-create-a-newline-character) – Lyrl Dec 29 '17 at 19:32

3 Answers3

2

Newline behavior depends on context

If you're trying to show this in HTML, add <br> tags. If you're trying to show newlines in raw-text, or CLI (console) output, use "\n"

while($r=mysqli_fetch_row($res))
{
    $newline = "\n";
    // $newline = "<br>";
    // $newline = "<br>\n";
    echo $string . $r[4] . $newline;
}

What does "newline" mean?

If you really want to know the basics of the crazy legacy from typewriters that is the source of this, see: https://blog.codinghorror.com/the-great-newline-schism/

  • Newline: start a new line of inline text or content.
  • CR: carriage-return - can mean go to start of this line, or start a new line.
  • LF: line-feed - start a new line.
  • CRLF: combined of the two above.
  • "\n": PHP double-quoted strings will turn this into a CRLF.
  • BR: html "break" - html ignores whitespace (space, CR, LF, tab, etc.), thus the BR tag.
Tony Chiboucas
  • 5,505
  • 1
  • 29
  • 37
  • Thanks Tody, gives output as expected! – SSG Dec 29 '17 at 18:57
  • one more query, at starting of first line only there are 2 spaces. How can I remove them? – SSG Dec 31 '17 at 10:32
  • That means that your code, or the library it uses, is outputting or echoing space characters. You'll either have to find it, echo a newline before the code in my answer, or dive deep into IOStreams, and console control. – Tony Chiboucas Dec 31 '17 at 18:59
  • Just got rid of these spaces by adding new line before `if` condition. Thank you, Tony! – SSG Jan 01 '18 at 09:05
1

your statement should looks like this

 echo $string . $r[4] . "\n";
samehanwar
  • 3,280
  • 2
  • 23
  • 26
0

Add the tag below to get a new line in HTML

echo "$string$r[4]";
?><BR/><?php

https://www.w3schools.com/tags/tag_br.asp

Ctznkane525
  • 7,297
  • 3
  • 16
  • 40