Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
How to display data in an object

When outputting data, they are displayed in one line. How to make sure that every record is an object and all objects turn into an array?

<?php
  require_once("db.php");
  $query = $db->query('SELECT * FROM  `dictdb`.`dictwords`');
  while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo "
           id: " . $row['id'] . ",
           engWord: " . $row['engwords'] . ",
           rusWord: " . $row['ruswords'] ."
          ";
  }
?>

test.js

$(document).ready(function () {
    $(".btn-test").on("click", function () {
        $.ajax({
            url: "/src/php/tests.php",
            type: "POST",
            success: function (data) {
              $(".test-word").html(data);
            }
        })
    })

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Because you're inserting plain text into HTML. That's meant to be with <tags> ignoring your whitespace (like line breaks).

Either load some reasonable HTML from PHP

## in PHP
...
echo "<p>
  id: " . $row['id'] . ",<br>
  engWord: " . $row['engwords'] . ",<br>
  rusWord: " . $row['ruswords'] ."<br>
</p>";
...

Or, better, work with JSON

## in PHP
...
$results = [];
while ($row = $query->fetch(PDO::FETCH_ASSOC))
  $results[] = $row;

header('Content-Type: application/json');
echo json_encode($results);
...
## in JS
$.ajax('/src/php/tests.php')
.done(data => { // use .success for older jQuery versions
  let formatedJSON = JSON.stringify(data, null, 2);
  $(".test-word").html(`<pre>${formatedJSON}</pre>`);
});


over 4 years ago · Santiago Trujillo Denunciar

0

Put the desired HTML tags in the data you echo.

echo "<br>
       id: " . $row['id'] . ",<br>
       engWord: " . $row['engwords'] . ",<br>
       rusWord: " . $row['ruswords'] ."<br>
      ";
over 4 years ago · Santiago Trujillo Denunciar

0

Your echo does not produce a JSON object. You would need to do something like:

<?php
require_once("db.php");
  
$query = $db->query('SELECT * FROM  `dictdb`.`dictwords`');

$response = [];

while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    $response[] = [
        'id' => $row['id'],
        'engWord' => $row['engwords'],
        'rusWord' => $row['ruswords'],
    ];
}

echo json_encode($response);

This would produce a JSON object similar to (if you had 2 results returned from DB):

[
    {
        id: 'YourID',
        engWord: 'Your Eng Word',
        rusWord: 'Your Rus Word',
    }, {
        id: 'YourID',
        engWord: 'Your Eng Word',
        rusWord: 'Your Rus Word',
    }, 
]

Rather than try to parse HTML, i'd highly recommend using a JSON response.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda