Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
Why the command echo php print badly lines html?

I am new on the php but I am trying to build a page that produces after having read something from the database the inherent checkboxes, but when these are printed with echo the formatting it is not the desired one producing therefore of the errors for the carriage return. Here is the php

<?php 

$conn = mysqli_connect("localhost", "username", "password", "database_name");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$query="SELECT * FROM ingrediente";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {

   $name = $row["id_ingrediente"];
   $value = $row["immagine"];
   $etichetta = $row["nome"];
   $prezzo = $row["costo_unitario"];
    echo "\n";
    echo "<input type='checkbox' name=$name value='$value' onchange=\"add_ing2($name,'$value','$etichetta',$prezzo,this);\">$etichetta" ;
    echo "\n";
    echo "<input type='number' name='$etichetta' style='border:0px; text-align:right;background:transparent; height:auto;' value=$prezzo readonly> €";
    echo "\n";
    echo "<br><br>";

  }
} else {
echo "0 results";
}

mysqli_close($conn);

?>

Here is the produced html

<input type='checkbox' name=1 value='/images/ingredienti/pomodoro.png' onchange="add_ing2(1,'/images/ingredienti/pomodoro.png','Pomodoro',0.3,this);">Pomodoro
<input type='number' name='Pomodoro' style='border:0px; text-align:right;background:transparent; height:auto;' value=0.3 readonly> €
<br><br>
<input type='checkbox' name=2 value='/images/ingredienti/formaggio.png
' onchange="add_ing2(2,'/images/ingredienti/formaggio.png
','Formaggio',0.2,this);">Formaggio
<input type='number' name='Formaggio' style='border:0px; text-align:right;background:transparent; height:auto;' value=0.2 readonly> €
<br><br>
<input type='checkbox' name=3 value='/images/ingredienti/hamburger.png
' onchange="add_ing2(3,'/images/ingredienti/hamburger.png
','Hamburger',1,this);">Hamburger
<input type='number' name='Hamburger' style='border:0px; text-align:right;background:transparent; height:auto;' value=1 readonly> €
<br><br>

As you can see the correct way to print has been applied only to the first element (Pomodoro (line 1:2 in html code)). Since this causes me more than few problems when then use the checkboxes, as can I resolve?

Thank you so much

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Nothing wrong with PHP - the data you're pulling out of the database is just bad. Some of your image paths have a new-line character at the end.

You should ultimately fix the data (validating user input, adjusting scripts that pull it in automatically, etc.), but you can apply trim() to the values to strip off the extra character(s).

over 4 years ago · Santiago Trujillo Relatório

0

Wrong assumption! Your $value seems too have an line-break at the end. You can see that the line breaks after the image url. So look for an \n at the image value. Tip: $value = trim($value); [update] Do trim() on all used variables. – @JustOnUnderMillions

Looks like you've got bad data with newlines in it. Take a look in the database - I'll bet you'll see them there in the raw data. You can try trim()ing the values, but better to fix the underlying issue. – @ceejayoz

I applied the trim() to all the variables and it works!!! Thx so much JustOnUnderMillion and ceejayox <3

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda