Sort After Import Question

3 posts by 2 authors in: Forums > CMS Builder
Last Post: September 16, 2015   (RSS)

Hi there,

I've imported several thousand records into CMSB and this has gone pretty well. However, the new records were imported in the old database order (primary key). They now show up as oldest-first and carry on for many pages. The editor is set to use dragSortorder and this does work. I can also change the sort order in the editor to publishDate DESC and they respond nicely in the list.

But... what we want is to have the imported records sorted once by their publishDate DESC and then revert to dragSortOrder as we start entering new material from now on - but keep that dated order as the starting layout.

In other words, we'd like to temporarily sort by publishDate DESC and then use that to reset the dragSortOrder once to match. Then we can revert to dragSortOrder from then on.

Is there a clever Interactive Tools super-power that can help us do that?

Thanks!

J.

Hey J. 

If you count a script as a superpower, then yes! 

<?php

  header("Content-Type:text/plain");
  
  // load viewer library
  $libraryPath = 'cmsb/lib/viewer_functions.php';
  $dirsToCheck = array('C:/wamp/www/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  $sectionName = 'blog';
  $inOrderOf   = 'createdDate';

  $records = mysql_select($sectionName, "TRUE ORDER BY $inOrderOf DESC");

  $orderByCounter = 10;

  foreach($records as $record){

    echo "Updating record #{$record['num']}\r\n";

    mysql_update($sectionName, $record['num'], null, array('dragSortOrder' => $orderByCounter));

    $orderByCounter += 10;
  }


  echo "Script Complete!\r\n";

You'll need to update the sectionName variable with the DB name for the section you want to update, and the inOrderOf variable with the date field you want to order the items in. So the script will collect all of the records from a section in date order, then cycle through them and update each records dragSortOrder with order the records have been retrieved in. 

Make sure you do a full backup of your database before running this script. 

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com