Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!