how to copy database records of one table section to another table section?

9 posts by 4 authors in: Forums > CMS Builder
Last Post: December 6, 2013   (RSS)

By Codee - November 30, 2013

Hi all, with CMSB, (ver 2.53) how do I copy all the records under one table section into another table section with different name? For example I have 2 table sections: drills (with 150 records in it) and a new table section called advanced_drills (currently with 0 records in it).  I need to copy all the records under drills into advanced_drills (the fields in both sections are identical).

Thanks!

Not trying to hijack this thread but this is something I am also interested in. For me, copying a record that is about to be deleted to another section. If I find out how, I'll post here...

By Daryl - December 2, 2013

Hi,

What I would do is I will write an importer script where I will add new records to another section from the old one and execute it once.

For example:

// load records from 'drills'
list($drillsRecords, $drillsMetaData) = getRecords(array(
  'tableName'   => 'drills',
  'loadUploads' => true,
));
foreach($drillsRecords as $record){
  mysql_insert('advanced_drills ', $record, true);
}

This will work if both tables have identical fields. Hope this helps!

Cheers!

Daryl Maximo
PHP Programmer - interactivetools.com

By Codee - December 2, 2013

Hi Daryl,

Thanks! I am a novice at some of this stuff.  "Where" would I post the code you just wrote and how would I run it?

By Dave - December 2, 2013

Hi equinox, 

Backup first (under Admin > General > Backup) and then you can just create a new the script like this:

<?php
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('C:/wamp/www/sb/CMS Builder/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  // load records from 'drills'
  list($drillsRecords, $drillsMetaData) = getRecords(array(
    'tableName'   => 'drills',
    'loadUploads' => true,
  ));

  foreach($drillsRecords as $record){
    print "Inserting record #{$record['num']}<br/>\n"; 
    mysql_insert('advanced_drills ', $record, true);
  }

  print "Done!"; 
?>

Careful not to refresh it multiple times or it will copy the records twice!

Note: If the script doesn't run just update the "// load viewer library" code with code that was generated for your website.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By Codee - December 2, 2013

I apologize for not being clear on what I'm asking.  Think "dumb it down" first and then my question: "Where does this code go specifically?" and "How do I execute this script?"  Based on the type of code you both wrote out, it seems it would go on a .php page, maybe call it "copyrecords.php" or something?  Or does it go in the header area of a different .php page like drillList.php or drillDetails.php?  Or should this be added in like a plugin?  Does that clarify what I'm asking?  Thanks!

By Dave - December 3, 2013

Hi Terry,

 it seems it would go on a .php page, maybe call it "copyrecords.php"

Yea, that's exactly.  Note that this is pretty advanced stuff though so be sure to backup first.  Also note that this method won't copy uploads.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By Codee - December 6, 2013

Thanks!!