Forums

How to select 7 latest record from a table

lf14web 13 Jun, 2018
In a table with thousands records, I need to select the latest 7 items order by ASC.
In the table option I found the order by, but extract the first 7 records with ASC or the latest 7 with DESC but with inverse order.

Regards
Emanuele
lf14web 14 Jun, 2018
Hi Bob!
thank you for your reply.

Yes with sql code I know how to do that. But I don't know ho I can replicate that with Read Data or if there are a best way in chronoform6 to obtain this result.

Do you have other suggest?
Thanks

Emanuele
GreyHead 14 Jun, 2018
Hi Emanuele,

Please check the Writing conditions section at page 24 of the CFv6 manual - I think that you can write a custom query if needed.

Bob
lf14web 14 Jun, 2018
I think that a cusom query its a good way, but I don't now how to insert in chronoforms6.
Please could you to give me an example?
Thanks!
lf14web 15 Jun, 2018
Bob,
please could you to give me an example how I can create a custom query?

Thanks a lot!
Emanuele
healyhatman 15 Jun, 2018
Just do read data, sorted by created date, then use a PHP action together the 7 items you want from the array
lf14web 16 Jun, 2018
Hi healyhatman,since the table contains a lot of thousand of record, maybe the best solution is get the seven records with DESC order by and use the array_reverse functions.

But I do not understand how I can do that.
I've read the manual, but in general I have many difficulties to manage the array data with chronoforms 6, with cf5 is more simple.

Thanks
healyhatman 16 Jun, 2018
Then use the paging option and set the record count to 7
lf14web 16 Jun, 2018
Yes I do that but I've the latest record how first in the array.

11359
11358
11357
...

I need ro reverse the array to have the record in right order

...
11357
11358
11359

[read_data4] => Array
        (
            [log] => Array
                (
                    [0] => SELECT COUNT(`extralaghi`.`aid`) AS `extralaghi.count` FROM `u53uf_chronoforms_data_rilievi-idrometrici-laghi` AS `extralaghi`;
                    [1] => SELECT `extralaghi`.`aid` AS `extralaghi.aid`, `extralaghi`.`user_id` AS `extralaghi.user_id`, `extralaghi`.`created` AS `extralaghi.created`, `extralaghi`.`modified` AS `extralaghi.modified`, `extralaghi`.`data` AS `extralaghi.data`, `extralaghi`.`lago` AS `extralaghi.lago`, `extralaghi`.`idro` AS `extralaghi.idro`, `extralaghi`.`note` AS `extralaghi.note` FROM `u53uf_chronoforms_data_rilievi-idrometrici-laghi` AS `extralaghi` ORDER BY `extralaghi.aid` DESC LIMIT 10;
                )

            [var] => Array
                (
                    [0] => Array
                        (
                            [extralaghi] => Array
                                (
                                    [aid] => 11359
                                    [user_id] => 0
                                    [created] => 2018-06-15 15:25:43
                                    [data] => 15/06/2018
                                    [lago] => Lago di Como
                                    [idro] => 200
                                    [note] => nota qualsiasi "dqtq"
                                )

                        )

                    [1] => Array
                        (
                            [extralaghi] => Array
                                (
                                    [aid] => 11358
                                    [user_id] => 0
                                    [created] => 2018-06-14 22:37:00
                                    [data] => 15/06/2018
                                    [lago] => Lago di Iseo
                                    [idro] => 52
                                    [note] => dsfads sadf adsf asdf
                                )

                        )

                    [2] => Array
                        (
                            [extralaghi] => Array
                                (
                                    [aid] => 11357
                                    [user_id] => 0
                                    [created] => 2018-06-15 14:55:24
                                    [data] => 15/06/2018
                                    [lago] => Lago di Como
                                    [idro] => 100
                                    [note] => dfas dsfads  dsfa sd
                                )

                        )

                    [3] => Array
                        (
                            [extralaghi] => Array
                                (
                                    [aid] => 11356
                                    [user_id] => 0
                                    [created] => 2018-06-14 22:25:29
                                    [data] => 14/06/2018
                                    [lago] => Lago di Como
                                    [idro] => 24
                                    [note] => dsafsd dsfasd asdf  3
                                )

                        )

                    [4] => Array
                        (
                            [extralaghi] => Array
                                (
                                    [aid] => 11355
                                    [user_id] => 0
                                    [created] => 2018-06-14 22:37:15
                                    [data] => 14/06/2018
                                    [lago] => Lago di Iseo
                                    [idro] => 22
                                    [note] => dfsa
                                )

                        )

                    [5] => Array
                        (
                            [extralaghi] => Array
                                (
                                    [aid] => 11354
                                    [user_id] => 0
                                    [created] => 2018-06-14 22:37:25
                                    [data] => 13/06/2018
                                    [lago] => Lago di Iseo
                                    [idro] => 180
                                    [note] => sdafsd sdafae dsfa
                                )

                        )

                    [6] => Array
                        (
                            [extralaghi] => Array
                                (
                                    [aid] => 11353
                                    [user_id] => 0
                                    [created] => 2018-06-14 22:37:38
                                    [data] => 13/06/2018
                                    [lago] => Lago di Como
                                    [idro] => 200
                                    [note] => dsafsdfsafds
                                )

                        )

                    [7] => Array
                        (
                            [extralaghi] => Array
                                (
                                    [aid] => 11352
                                    [user_id] => 0
                                    [created] => 2018-06-14 22:37:45
                                    [data] => 12/06/2018
                                    [lago] => Lago di Como
                                    [idro] => 200
                                    [note] => ddsfsdf
                                )

                        )

                    [8] => Array
                        (
                            [extralaghi] => Array
                                (
                                    [aid] => 11351
                                    [user_id] => 0
                                    [created] => 2018-06-14 22:09:14
                                    [data] => 14/06/2018
                                    [lago] => Lago di Como
                                    [idro] => 200
                                    [note] => dfsa
                                )

                        )

                    [9] => Array
                        (
                            [extralaghi] => Array
                                (
                                    [aid] => 11350
                                    [user_id] => 0
                                    [created] => 2018-06-14 21:59:34
                                    [data] => 
                                    [lago] => 
                                    [idro] => 
                                    [note] => 
                                )

                        )

                )

        )
healyhatman 16 Jun, 2018
Get the value array in PHP with $this->get("read_data4", "")
lf14web 18 Jun, 2018
Ok Thank You!
Work fine with this code into PHP module

$revextra = $this->get("read_data4", "");
return array_reverse($revextra);

Emanuele
This topic is locked and no more replies can be posted.