Utilizing PHP to iterate over a JSON array and generate HTML content

I am utilizing bootstrap datatables to present data retrieved from an API in a tabular format. Below is the sample data:

{
  "response_status": {
    "status_code": 0,
    "status_message": [
      {
        "reg_number": "123",
        "company": "Company A",
        "office": "Orlando",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      },
      {
        "reg_number": "552",
        "company": "Company B",
        "office": "Miami",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      },
      {
       "reg_number": "154",
        "company": "Company C",
        "office": "San Francisco",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      }
    ]
  }
}

While trying to display the data using bootstrap datatables, I aim for the resulting HTML output to resemble this structure:

<table> <thead> <tr> <th>Reg No</th> <th>Company</th> <th>Office</th> <th>Phone Number</th> <th>Postal Address</th> <th>Postal Code</th> </tr> </thead> <tbody> <tr> <td>123</td> <td>Company A</td> <td>San Francisco</td> <td>28122312345</td> <td>000</td> <td>000</td> </tr> <tr> <td>552</td> <td>Company B</td> <td>Miami</td> <td>28122312345</td> <td>000</td> <td>000</td> </tr> <tr> <td>154</td> <td>Company C</td> <td>San Francisco</td> <td>28122312345</td> <td>000</td> <td>000</td> </tr> </tbody> <table>

Currently, I have been experimenting with incorporating PHP code into this setup:

<table>
<thead>
   <tr>
       <th>Reg No</th>
       <th>Company</th>
       <th>Office</th>
       <th>Phone Number</th>
       <th>Postal Address</th>
       <th>Postal Code</th>
   </tr>
</thead>
<tbody>
    <?php foreach($data->response_status->status_message as $message) : ?>
    <tr>
       <td><?php echo $message->reg_number; ?></td>
       <td><?php echo $message->company; ?></td>
       <td><?php echo $message->office; ?></td>
       <td><?php echo $message->phone_number; ?></td>
       <td><?php echo $message->postal_address; ?></td>
       <td><?php echo $message->postal_code; ?></td>
   </tr>
   <?php endforeach; ?>
</tbody>
<table>

However, this approach has not yielded the desired outcome. Is there an alternative method that could be employed to achieve the desired result?

Answer №1

It appears that everything is running smoothly, as pointed out by @Scopey. The only thing that seems to be missing here is the json_decode function.

    $json = file_get_contents('path/to/your/JSONfile.json');
    $data= json_decode($json);

If you have already implemented this code snippet, then there should be no issues present.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to use jQuery to fill a dropdown menu with options from a JSON object?

Looking to populate a dropdown box #dropdown with values from a json object stored in a JavaScript string variable. How can I access each element as value/label pairs and insert them into the dropdown? The structure of the json string is as follows: [{"n ...

Disable checkboxes upon page initialization

I am working with a form that includes checkboxes. Whenever the page loads, clicking on the checkboxes automatically checks them. However, I am looking for a solution where the checkboxes are disabled or not clickable during the page load process. Once th ...

I am experiencing an issue with my d3 force directed graph where the links are not

I am relatively new to d3 and have limited experience with web frontend development. In my current web application project, I am attempting to create a force directed graph. Despite spending several hours trying to make it work, I have been unable to displ ...

`Incorporating multiselect functionality to dynamically modify database entries``

I am currently working on a project to create a multi-select list that allows users to add and remove items based on two columns. The left column contains the full list of items, while the right column displays the selected items. Upon submission, all newl ...

What is the best way to retrieve information from an Array within a JSON Object?

I currently have a Json object called FriendJson, which contains an array field named friends. This is the Json Object: [ { "id": 4, "updated": "2023-01-07T22:06:23.929206Z", "created": "2023-01-0 ...

Transform an Array into a String using Angular

Is there a more efficient way to extract all the state names from the array (testArray) and convert them to strings ('Arizona','Alaska','Florida','Hawaii','Gujarat','Goa','Punjab'...)? ...

Remove non-ASCII whitespaces in PHP

My challenge involves trimming unicode whitespaces, like those found in certain characters. I managed to achieve this using a solution I found on Stack Overflow. However, the issue with this method is that it only trims the unicode whitespaces located at t ...

Stop unwanted spam submissions on my form

I am working on creating a send to a friend form that is spam-resistant. After some research, I found the following helpful sources: Preventing Email Header Injection Spoofed Form Submissions Is the code below sufficient for preventing spam? It remove ...

Guiding user to their destination after signing in

Having trouble showing different headers based on user login status? Users can log in using login.php, but you're struggling to display header.php when a user is not logged in or logged in. You want the page to show profile form content when they are ...

Logging into a MySQL database on an Android device

Hey there everyone, I'm currently working on implementing a basic login system in Android that connects to an online MySQL database. Here is the approach I've taken so far: MainActivity: protected String doInBackground(String... args) { s ...

Utilizing jQuery for AJAX requests triggered by mouse movement

I have a project where I am creating an image with gdimage, consisting of 40000 5x5 blocks that link to different user profiles. My goal is to implement a feature where hovering over one of these blocks triggers AJAX to fetch the corresponding user profile ...

Experiencing prolonged delays with PHP/AJAX login process

Recently, I took over the maintenance of a website that requires users to log in. However, I've noticed that the login process is quite slow, especially when there are around 4000 registered users. The current login system utilizes a combination of A ...

Dealing with undefined Ajax values in PHP

Every time I call the function, I receive an undefined value and it's baffling me as to what could be causing this issue. You are logged in as undefined, Error undefined Ajax script: function Submit() { console.log('asd') $.ajax({ ...

Struggling to retrieve data from a MongoDB database in JSON format via an API call? If you're working with JavaScript, Node.js, Express, and MongoDB, we can help you

Below is how I establish the connection to the database: > // Connecting MongoDB using MongoClient > > const MongoClient = require('mongodb').MongoClient > > const url = 'url is used' > > const dbName = 'vir ...

What techniques can I utilize to trim down this SQL query?

SELECT lesson_id_1, lesson_id_2, lesson_id_3, lesson_id_4, lesson_id_5, lesson_id_6, lesson_id_7, lesson_id_8, lesson_id_9, lesson_id_10 FROM hub_attendance WHERE student_id='351' AND ...

Altering information with the use of history.pushState throughout time

I've created a template that I want to load using the jQuery .load() function. However, during testing, I discovered that it's not loading at all. Here is the code I'm trying to use for loading: function open() { history.p ...

Extract quotation marks from a JSON object's value

I am currently utilizing the Woocommerce API within node js to transfer product information from a mssql server database to my woocommerce database. The dataset retrieved through my ms sql query contains an images field. To import images using the Woocom ...

What causes the sudden change in value of this array?

I can't seem to figure out what's going on with this question. It might be something silly, but I haven't been able to find any clues. Here is the array in question: const superVillains = [ { value: '1', label: 'Thanos&apo ...

The request to the upstream server timed out (110: Connection timed out) when connecting to PHP-FPM via NGINX

Our web server is currently running on nginx with php5-fpm apc setup. Recently, we encountered upstream connection timeout errors and slowdowns during page rendering. A temporary fix was implemented by restarting php5-fpm, but the root cause remains uniden ...

Issues with uploading files

I am trying to implement a functionality where users can upload video, audio, images, PDFs, PowerPoint presentations, and Word files, and then have them saved into a directory. However, when I try to upload the files using my code, there are no errors but ...