Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

List by first name and not ID WordPress

  • SOLVED

Hi
This is not WP related but it's PHP so I hope someone here can help me :)

I've got a page that lists the staff. Each staff member (teacher) has an ID, but I want the list to be sorted alphabetically by the name.

On the current page at the bottom, you can see that the teacher Ann Kathrine should have been at the top of the site. The current site is here: http://madsrosendahl.dk/jammerbugt/teachers.php

The code is here https://pastebin.com/GwTh9exg and below: (I've removed the profile images to make the code easier to read)

NOTE: You can't test the code because there's an authorization key that I ofc can't share here.


<table>
<?php function executeRESTCall($method, $url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
$header = array();
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: THIS-IS-HIDDEN';
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($curl);
}
$baseUrl = 'https://api.speedadmin.dk/v1/';
$teachersJSONString = executeRESTCall('GET', $baseUrl . 'teachers');
// $getMads = executeRESTCall('GET', 'https://api.speedadmin.dk/v1/teachers/496');
// Convert JSON string to Object
$teachers = json_decode($teachersJSONString);
// Use print_r($teachers); to printing all teachers, then you can see what values you have in there
// Loop through Object
foreach($teachers as $key => $value) {
$external_link = "https://www.jammerbugtkulturskole.dk/images/personale/Teacher_". strval($value->TeacherId) . ".jpg";
// define a variable for the image url with the teacher ID
if (true) {
echo '<tr>';
echo '<td><strong>' . ($value->Name . "\n\n") . ($value->Surname) . '</strong>';
echo '<ul>';
// There might be a better way to do this, but this works :)
// This lists the 'listemedfag' for each teacher
//
$listemedfag = $value->AvailableClasses;
for ($i = 0; $i < count($listemedfag); $i++)
echo '<li>' . $listemedfag[$i] . '</li> ' . "\n";
echo '</ul>';
echo '<td>Email: <a href="mailto:' . $value->Email . '">' . ($value->Email . '</a>' . "\n\n");
echo '<br>Tlf. ' . ' ' .($value->Mobile . "\n\n");
echo '</td></tr>';
}
}
?>
</table>

Answers (2)

2020-03-17

John Cotton answers:

You probably want to use usort.


$teachers = json_decode($teachersJSONString);
usort ($teachers, function ($a, $b) {
return strcmp( strtolower($a->name), strtolower($b->name) ); // ->name or whatever the property name actually is
});

// $teachers is now sorted alphabetically.

2020-03-17

Reigel Gallarde answers:

something like this, after your json_decode...

usort($teachers, function($a, $b) { //Sort the array using a user defined function
return strtolower($a->Name) > strtolower($b->Name) ? -1 : 1; //Compare the Name or use Surname or concatenate both.. depends on your requirement
});

print_r($teachers);


Reigel Gallarde comments:

more readings:

https://stackoverflow.com/questions/23779412/sort-json-object-in-php-by-a-key-value