PatrickRQ Posted March 8, 2022 Share Posted March 8, 2022 (edited) Hi, I look for a way to reverse array order directly in template and PHP but seems it does not work. I wanted to avoid plugins or direct file modifications for such simple things. Is it actually possible?, example code below. {{foreach $files as $k => $file}} {{$data = $files->data();}} <li class='ipsDataItem'> <div class='ipsDataItem_main'> <h4 class='ipsDataItem_title ipsContained_container'><span class='ipsType_break ipsContained'>{{if $data['record_realname']}}{$data['record_realname']}{{else}}{{$pathBits = explode( '/', \IPS\Http\Url::external( $data['record_location'] )->data[ \IPS\Http\Url::COMPONENT_PATH ] );}}{expression="\count( $pathBits ) ? array_pop( $pathBits ) : $data['record_location']"}{{endif}}</span></h4> {{if $data['record_size']}}<p class='ipsType_reset ipsDataItem_meta'>{filesize="$data['record_size']"}</p>{{endif}} </div> {{if $waitingOn == $k}} <div class='ipsDataItem_generic ipsDataItem_size6 ipsType_warning'> <noscript>{lang="wait_x_seconds_noscript" pluralize="$waitingFor"}</noscript> </div> {{endif}} <div class='ipsDataItem_generic ipsDataItem_size4 ipsType_right'> <span class="ipsHide" data-role="downloadCounterContainer">{lang="download_begins_in"} <span data-role="downloadCounter"></span> {lang="seconds"}</span> <a href='{$fileObject->url('download')->setQueryString( array( 'r' => $k, 'confirm' => 1, 't' => 1, 'version' => isset( \IPS\Request::i()->version ) ? \IPS\Request::i()->version : NULL ) )->csrf()}' class='ipsButton ipsButton_primary ipsButton_small' data-action="download" {{if member.group['idm_wait_period']}}data-wait='true'{{endif}}>{lang="download"}</a> </div> </li> {{endforeach}} What I want is array_reverse($files, true) but then no file displays in downloads. I tried exactly below code: {{$data2 = null;}} {{$data2 = array_reverse($files, true);}} {{foreach $data2 as $k => $file}} {{$data = $data2->data();}} Edited March 8, 2022 by PatrickRQ Link to comment Share on other sites More sharing options...
Stuart Silvester Posted March 8, 2022 Share Posted March 8, 2022 $files isn't an array, but an Iterator. You might be able to do something like {{foreach array_reverse( iterator_to_array( $files ) ) as $k => $file}} I haven't tested and I'm not certain that it'll work, but worth a try. PatrickRQ 1 Link to comment Share on other sites More sharing options...
PatrickRQ Posted March 8, 2022 Author Share Posted March 8, 2022 It works this way but actually fails here {{$data = $files->data();}} as the $files are not reversed in this place. I think I need to redesign entire foreach to work with array. Link to comment Share on other sites More sharing options...
PatrickRQ Posted March 8, 2022 Author Share Posted March 8, 2022 This is result of the change. Missing info of $data Link to comment Share on other sites More sharing options...
IPCommerceFan Posted March 8, 2022 Share Posted March 8, 2022 I wrote this to sort downloads by filename. You should be able to use it to accomplish the reverse sort by changing the database query accordingly: <div class='ipsPad'> <h1 class='ipsType_pageTitle'>{lang="download_your_files"}</h1> <p class='ipsType_reset ipsType_normal ipsType_light'>{lang="download_file_count" pluralize="\count( $files )"}</p> <hr class='ipsHr'> <ul class='ipsDataList ipsDataList_reducedSpacing'> {{$fileRecords = \IPS\Db::i()->select('*', 'downloads_files_records', array(array('record_file_id=?', $fileObject->id),'record_backup=0'),'record_realname ASC');}} {{foreach $fileRecords as $file}} {{$data = $file;}} {{$k = $data['record_id'];}} <li class='ipsDataItem'> <div class='ipsDataItem_main'> <h4 class='ipsDataItem_title ipsContained_container'><span class='ipsType_break ipsContained'>{{if $data['record_realname']}}{$data['record_realname']}{{else}}{{$pathBits = explode( '/', \IPS\Http\Url::external( $data['record_location'] )->data[ \IPS\Http\Url::COMPONENT_PATH ] );}}{expression="\count( $pathBits ) ? array_pop( $pathBits ) : $data['record_location']"}{{endif}}</span></h4> {{if $data['record_size']}}<p class='ipsType_reset ipsDataItem_meta'>{filesize="$data['record_size']"}</p>{{endif}} </div> {{if $waitingOn == $k}} <div class='ipsDataItem_generic ipsDataItem_size6 ipsType_warning'> <noscript>{lang="wait_x_seconds_noscript" pluralize="$waitingFor"}</noscript> </div> {{endif}} <div class='ipsDataItem_generic ipsDataItem_size4 ipsType_right'> <span class="ipsHide" data-role="downloadCounterContainer">{lang="download_begins_in"} <span data-role="downloadCounter"></span> {lang="seconds"}</span> <a href='{$fileObject->url()->setQueryString( array( 'do' => 'download', 'r' => $k, 'confirm' => 1, 't' => 1, 'version' => isset( \IPS\Request::i()->version ) ? \IPS\Request::i()->version : NULL ) )->csrf()}' class='ipsButton ipsButton_primary ipsButton_small' data-action="download" {{if member.group['idm_wait_period']}}data-wait='true'{{endif}}>{lang="download"}</a> </div> </li> {{endforeach}} </ul> </div> Link to comment Share on other sites More sharing options...
Stuart Silvester Posted March 8, 2022 Share Posted March 8, 2022 That wouldn't quite work either, it would include screenshots, links from all versions and also include new files that are pending approval Link to comment Share on other sites More sharing options...
IPCommerceFan Posted March 8, 2022 Share Posted March 8, 2022 Ah, our use case doesn't involve any of that so it wasn't considered. 🤦♂️ Link to comment Share on other sites More sharing options...
PatrickRQ Posted March 8, 2022 Author Share Posted March 8, 2022 That condition make things work {{$fileRecords = \IPS\Db::i()->select('*', 'downloads_files_records', array(array('record_file_id=?', $fileObject->id),'record_backup=0','record_hidden=0', 'record_type="upload"'),'record_id DESC');}} Link to comment Share on other sites More sharing options...
Recommended Posts