Sort After Import Question

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

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

By In-House-Logic - September 16, 2015 - edited: September 16, 2015

Gracias, Super Greg!

I'll give this a poke and let you know how it super it is. ;)

EDIT: Super Power Status Granted. Worked like a charm. Many thanks!

J.