Sorting HTML table with JSON data in alphabetical order

emmab

So, I'm a PHP rookie that could use some tips when it comes to creating a table and sorting this. I'm not looking for a particular fancy solution but rather just get it to work.

JSON data is imported from API to a variable and I want to print product name and price in a list and statically sort this by product name in alphabetical order, instead of order they appear in array. I have created an HTML table which works fine but I am super stuck when it comes to sorting it. Is it possible to do using perhaps some loops rather than involving JavaScript or similar?

I have the imported and decoded JSON data in $product_data and I'm printing the table like this:

<table id="articles">
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($product_data as $products) { ?>
        <tr>
            <td><?= $products->product_name; ?></td>
            <td><?= $products->price; ?></td>
        </tr>
        <?php } ?>
    </tbody>
</table>

I find plenty of solutions where you can do this sorting dynamically using JS or solutions to sort when connected to a SQL DB, but I just simply want it to show in alphabetical order with no user interaction.

Thanks

Przemysław Niemiec

Just before your foreach loop, you can sort your array using usort function. This function allows you to sort your array, using your own function:

usort( $product_data, fn($a,$b) => strcmp($a->product_name, $b->product_name) );

And by the way: I think it will be good idea to rename $products variable in your forach loop to $product, because it stands for a single object (single product).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JSON Sorting by Alphabetical Order

To display json data in alphabetical order

Sorting by alphabetical order

Sorting files in alphabetical order

sorting based on alphabetical order

Sorting Lists into Alphabetical Order

Sorting Letters in Alphabetical Order

Data Migration Code First Table order is alphabetical

Sort by name in alphabetical order not working in data table

To filter Json data in alphabetical order in angular 6

Sorting JSON/Array in alphabetical order using multiple values in jQuery or PHP

MySQL joining tables and sorting data by count and alphabetical order

Sorting multiple data from swift firebase in alphabetical order in tableview cell

Sorting a Linked List in alphabetical order

Sorting an ArrayList<String[]> in alphabetical order

Sorting based on frequency and alphabetical order

Sorting arraylist in alphabetical order(Persian)

sorting collection in alphabetical order in laravel

Sorting a Linked List into alphabetical order

Java sorting alphabetical order of array

Pivot table in alphabetical order

How to sort json data and display in a tableView as alphabetical order

Trouble with Properly Sorting Array in Alphabetical Order

Sorting by alphabetical order immutable.js

Sorting arraylist in alphabetical order (case insensitive)

Sorting an array of strings in an alphabetical order using pointers

Sorting a dictionary by keys in alphabetical order if their values are equal

Sorting (alphabetical order) to ignore empty cells : dataTables

Sorting characters by alphabetical order within column?