0

Hello so I'm writing a php code that allows me to stock the result of sql query in a php array and then Iam conerting the array into a string using the function implode and I am trying to put each row of the table in a new line but when i echo the string I keep having all my results in the same line.

$data=array();
$req=$bdd->prepare("SELECT * FROM tableciqual WHERE (CONVERT(`alim_nom` USING utf8)) LIKE  ?");
$req->execute(array("%$nom%"));
while($resultat=$req->fetch()){
    $data[] =$resultat['alim_nom'];
}
$d=implode("\r\n",$data);
echo $d;
3
  • 1
    Have you surrounded the output with <pre> and </pre> tags, or put the output somewhere where the browser will respect the newline? Commented Sep 21, 2020 at 12:03
  • 1
    implode("<br>",$data); ? Commented Sep 21, 2020 at 12:04
  • the solution with <br> works when I echo my result on a web page but I am trying to echo the result on dialogflow and the response contains the <br> instead of putting each result in a new line. Commented Sep 21, 2020 at 15:19

1 Answer 1

1

You should use <br/> tag inside the implode function, instead of line-terminators in case of a web-browser. Markup languages like HTML/XML do not process line-terminators by default. For example,

  • A newline '\n'
  • A carriage-return '\r'
  • A next-line '\u0085'
  • A line-separator '\u2028'
  • A paragraph-separator '\u2029

are not processed as they should be (by default in a web browser). Instead the browser treats them as "whitespaces".

If you want to use them, then you can either

  • Wrap the variable/output with a <pre> tag, which will explicitly tell the browser to also do non-HTML formatting (like in this case for line-terminators).
  • Use the nl2br() function, which will convert line-terminators to <br/>

Ideally you would want to replace line-terminators with the line-break <br/> tag.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.