©2006-2017 IAAI Software

Contact Us Privacy Policy

Bulk insert at subcategories

Submitted by stevebi on Mon, 2016-01-25 15:09.

Hello David,

Is there any possibility of bulk insert of subcategories to a Category at Categories Hierarchy?

Cheers

Steve

Submitted by support on Mon, 2016-01-25 15:48.

Hi Steve,

A "Bulk-Add" page with a textarea for entry of multiple sub-categories (one per line) is straight forward to implement. First edit admin/categories_hierarchy.php and look for the following code at line 118:

    admin_tool("New","JavaScript:ch_add_onClick(".$node["id"].")",TRUE,TRUE);

...and REPLACE with:

    admin_tool("New","JavaScript:ch_add_onClick(".$node["id"].")",TRUE,TRUE);
    admin_tool("New-Bulk","categories_hierarchy_newbulk.php?id=".$node["id"],TRUE,TRUE);

Then create the new script admin/categories_hierarchy_newbulk.php as below:

<?php
  
require("../includes/common.php");
  
$admin_checkPassword TRUE;
  require(
"../includes/admin.php");
  require(
"../includes/widget.php");
  function 
getCategoryPath($id)
  {
    global 
$config_databaseTablePrefix;
    
$categories = array();
    do {
      
$sql "SELECT name,parent FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id = '".$id."'";
      
database_querySelect($sql,$rows);
      
array_unshift($categories,$rows[0]["name"]);
    } while(
$id $rows[0]["parent"]);
    return 
implode(" / ",$categories);
  }
  
$id $_GET["id"];
  
$sql "SELECT * FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id='".database_safe($id)."'";
  
database_querySelect($sql,$rows);
  
$category $rows[0];
  
$submit = (isset($_POST["submit"])?$_POST["submit"]:"");
  if (
$submit == "Save")
  {
    if (
$_POST["subcategories"])
    {
      
$subcategories explode("\n",$_POST["subcategories"]);
      foreach(
$subcategories as $subcategory)
      {
        
$subcategory trim($subcategory);
        if (
$subcategory)
        {
          
$sql "SELECT id FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE name='".database_safe($subcategory)."' AND parent='".database_safe($id)."'";
          if (!
database_querySelect($sql,$rows))
          {
            
$sql "INSERT INTO `".$config_databaseTablePrefix."categories_hierarchy` SET name='".database_safe($subcategory)."',parent='".database_safe($id)."'";
            
database_queryModify($sql,$result);
          }
        }
      }
    }
    
header("Location: categories_hierarchy.php");
    exit();
  }
  require(
"admin_header.php");
  print 
"<h2>".translate("Category Hierarchy Mapping")."</h2>";
  print 
"<h3>".translate("New Subcategories")." (".getCategoryPath($category["id"]).")</h3>";
  print 
"<div class='row'>";
  print 
"<div class='small-6 columns'>";
  
widget_formBegin();
  
widget_textArea("Subcategories","subcategories",FALSE,"",200,12);
  
widget_formButtons(array("Save"=>TRUE),"categories_hierarchy.php");
  
widget_formEnd();
  print 
"</div>";
  print 
"</div>";
  require(
"admin_footer.php");
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stevebi on Mon, 2016-01-25 19:32.

Ohhhhhhhhhhhhhhhh!!!

PriceTapestry really rocks!!

Thank you very much David!!!

Steve