0

I have 5 different database calls to collect data from users:

$stmt=$db->prepare('SELECT members.email, sites.username, sites.time, sites.id, sites.url, sites.title
                    FROM sites
                    LEFT JOIN members
                    ON members.username=sites.username
                    LEFT JOIN list_friends 
                    ON
                    (CASE
                     WHEN friend1 = :username THEN friend2
                     WHEN friend2 = :username THEN friend1
                    END) = sites.username
                    WHERE sites.username=:username
                    OR (list_friends.friend1 <> :username)
                    OR (list_friends.friend2 <> :username)
                    ORDER BY sites.time ASC');
$stmt->bindParam(':username', $username);
$stmt->execute();
$row2 = $stmt->fetchAll();

Each database call is a unique fetchAll variable: $row, $row1, $row2, etc.

I then create 5 different array sets based on that data.

foreach( $row2 AS $sites_table ) {
    $data[] = array(
        'type' => 'sites',
        'id' => $sites_table["id"],
        'time' => $sites_table["time"],
        'site' => $sites_table["url"],
        'title' => $sites_table["title"],
        'username' => $sites_table["username"],
        'email' => $sites_table["email"]
    );
}

I have a comparing function

function cmp($a, $b)  {
    $ad = new DateTime($a['time']);
    $bd = new DateTime($b['time']);

    if ($ad == $bd) {
        return 0;
    }

    return $ad < $bd ? -1 : 1;
}

And finally I do a sort based on that compare.

if (isset($data) && $data !== NULL) {
    usort($data, "cmp");
    for($i=(count($data)-1)-($feed-1);$i>=(0);$i--){
        if ($i >= 0) {
            // echo out result of arrays
        }
    }
}

My end goal is to have all the data displayed sorted by data['time'] with the latest data['time'] always displayed on top, and then the next latest, and the next so on.

Unfortunately it appears that the sorting is only working per array, per user, instead of all the arrays data combined for all users combined.

so it is displaying output such as

user1 -> $row1 -> 1 minute ago
user2 -> $row5 -> 30 seconds ago
user1 -> $row2 -> 10 seconds ago
user3 -> $row1 -> 4 minutes ago
user1 -> $row1 -> 2 minutes ago
user2 -> $row1 -> 45 seconds ago
user2 -> $row5 -> 55 seconds ago
etc.

So how would I make a sorting algorithm that shows the latest timestamps in order regardless of row or user

user1 -> $row2 -> 10 seconds ago
user2 -> $row5 -> 30 seconds ago
user2 -> $row1 -> 45 seconds ago
user2 -> $row5 -> 55 seconds ago
user1 -> $row1 -> 1 minute ago
user1 -> $row1 -> 2 minutes ago
user3 -> $row1 -> 4 minutes ago
etc.
Bruce
  • 1,039
  • 1
  • 9
  • 31
  • Have you considered merging the arrays into one array before sorting? See https://stackoverflow.com/questions/13170230/php-combine-two-associative-arrays-into-one-array – bcperth Oct 08 '18 at 23:51
  • All the arrays are combined into one $data[] array already. I currency use them by `if ($data[$i]['type'] == 'value') { // echo results }`. – Bruce Oct 15 '18 at 05:02

0 Answers0