foreach sort results from database
NickName:Tatata Ask DateTime:2018-02-16T21:11:24

foreach sort results from database

I have 9 blocks, and I want sort it by last news added. I have code:

$projects = Projects::find(['conditions' => 'active = 1', 'order' => 'id DESC']);
    $itemsps = [];
    foreach($projects as $project) {
        if(!$project->{'link_' . $lang. ''}) continue;
        $itemsp['title_md'] = $project->title_md;
        $itemsp['title_ru'] = $project->title_ru;
        $itemsp['link'] = 'http://'.$_SERVER['SERVER_NAME']. '/' . $lang . '/' . $project->{'link_' . $lang. ''};
        $category = Categories::findFirst('alias = ' . "'$project->link_md'" . ' OR alias_ru = ' . "'$project->link_ru'");
        $lastCat = NewsCategories::find('categories_id = ' . $category->id)->getLast();

        if($lastCat === false) continue;

        $lastImage = 'uploads/' . $lastCat['news_id'] . '.jpg';

        $itemsp['image'] = $lastImage;

        $itemsps[] = $itemsp;
    }

How I can order by last records of:

$lastCat = NewsCategories::find('categories_id = ' . $category->id)->getLast();

Results in my array:

itemsps

Copyright Notice:Content Author:「Tatata」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48827545/foreach-sort-results-from-database

More about “foreach sort results from database” related questions

foreach sort results from database

I have 9 blocks, and I want sort it by last news added. I have code: $projects = Projects::find(['conditions' => 'active = 1', 'order' => 'id DESC']); $itemsps = []; foreac

Show Detail

Trying to sort fetch results by keys taken from the results

How do I create an array of SQL fetched results and sort by keys that are taken from the results themselves? For example, here is how the results from the database look like: $results = array( ...

Show Detail

Sort the results in a foreach

if I have the following values ​​in a table: table: user id rating 1 5 2 4 1 3 2 2 obviously doing SELECT * FROM USER I pull out all the values ​​of the user table, but if I wanted to cal

Show Detail

Sort foreach by function inside loop

I am making my first ever PHP app and I am flummoxed at the final hurdle. I am looping my results and need to sort the results of a function inside a foreach loop in PHP. If anyone has any insigh...

Show Detail

Perl: foreach and sort

I'm writing a little program that I need to sort the results in. Basically, I have a foreach loop that gets its data from a subroutine. Here's the code: foreach $host (get_mirror_list()) { ...

Show Detail

sort json results in foreach loop

I am running a foreach loop to display json results, when certain conditions are met, and would like to sort them by the name field. I am trying usort(), but can't seem to figure it out. JSON: { ...

Show Detail

Sort foreach results by html form input order

I have an html form that posts the content of an <input> field. This field accepts one or more values. If more than one value, then the user just separates with a blank space. Question: How c...

Show Detail

Sort database results by the polymorphic relationship in Laravel

I have an issue for sorting results by polymorphic relation in Laravel . Suppose we have two table like this : users table : - integer id - string name meta table : - integer id - string k...

Show Detail

How to sort database results?

I need to retrieve items from database and show them on browser in a specific order. I have two forEach loops, first one loops through products of factory object and second one loops through diff...

Show Detail

Sort iterations from within foreach loop

I have an array with user names: $a = array('user1','user2'); I want to use them to output the users' information in a list, but I need it sorted by their name, which I pull from the database wit...

Show Detail