Result and Sort by Date |
Result and Sort by Date
This is my ControlleR: Code: $data['reg'] = $this->register->registerAcc($schoolID); This is my View Code: Code: <thead style="width:300px;"> But i have a problem , when i show in the page the list will go from Code: Date Function UserName What i wanted is
Code: Date Function UserName Code: public function registerAcc($schoolID){ Any idea ?
Hard to offer help without knowing what the data return from the models looks like. It might also be useful to show the model code.
@incognitorecon,
What type of field is the date field? varchar? datetime? However, I did find this solution... https://stackoverflow.com/questions/1693...ate-format
(09-03-2019, 01:55 AM)incognitorecon Wrote: Any idea ? You are making 3 separate request, that's why the order is not what you expect. I can think of 2 ways to fix this: 1. Combine your 3 select query in a single one with UNION and add an order by for the results returned by all 3 selects. Example: https://stackoverflow.com/questions/4374...nion-mysql 2. Combine the results of your 3 select in a single array, and sort this new array, maybe with usort, depending on the content: https://www.php.net/usort
(09-04-2019, 09:47 AM)includebeer Wrote: 2. Combine the results of your 3 select in a single array, and sort this new array, maybe with usort, depending on the content: https://www.php.net/usort I thought this might work too but it seems to me that the combined array has to sort by keys. A sort by value is - pardon the pun - of no value. (usort sorts on values) If we use dates for the keys we are limited to one date in the array. Maybe a UNION will work. UPDATE It seems a UNION will work. I used the query below in phpMyAdmin and got the desired results. The various "date" columns all use the "date" datatype. Code: SELECT DATE_FORMAT(`registerDate`, '%m-%d-%Y') AS 'Date',`registerStatus` AS 'Function',`registerUserName` AS 'Name' FROM `registerdata` WHERE `schoolID`=5 So that means only one query and resulting dataset, so the view only needs one foreach loop.
(09-04-2019, 10:28 AM)dave friend Wrote:(09-04-2019, 09:47 AM)includebeer Wrote: 2. Combine the results of your 3 select in a single array, and sort this new array, maybe with usort, depending on the content: https://www.php.net/usort Why would you want to sort by key? You want to sort by date, so by value. You don't need to change the structure of the array. Each element is a database row. You reorder the rows based on the date value.
(09-04-2019, 12:42 PM)includebeer Wrote: Why would you want to sort by key? You want to sort by date, so by value. You don't need to change the structure of the array. Each element is a database row. You reorder the rows based on the date value. All the sort functions work on a single array. A database result set is an indexed array containing either array or object items. (Let's call those sub-arrays/objects $rows.) What is needed is a way to sort the $rows within the containing array. As I said, my first inclination was to simply take the date from each $rows and use it as the key in the containing array. But the date values are not unique so that won't work. I kind of remember a way to do this using array_column() and array_multisort(), but the UNION query is easier so I've stopped thinking about it. ![]()
Here is what I had in mind for the usort function.
Lets take 3 simple arrays and combined them with array_merge: PHP Code: $arr1 = array( This gives you this 4th array: PHP Code: Array Now to sort by date, define a compare function and call usort: PHP Code: function cmp($a, $b) This gives you this final array, sorted by date: PHP Code: Array |
Welcome Guest, Not a member yet? Register Sign In |