x
 
Create New Page
 View Previous Changes    ( Last updated by Lisa Wess )

Insert Multiple Categories

Changes are made at your own risk and are not guaranteed to work. Always make backups of the database and files prior to implementing hacks. Make a special backup of the file and/or template you are modifying so that you can roll back quickly.

Hacks are dangerous and can cause your site to stop working. They make later updates to ExpressionEngine more difficult; you should track your hacks for post-update re-implementation. 

Most hacks are unnecessary, please review the Development Documentation for information on expanding ExpressionEngine via Modules, Extensions, and Plugins.

Hacks are never officially supported.

A forum topic recently enquired about the ability to insert multiple categories without having to go through the System control panel inserting them one by one.

So I wrote this script to do the job. Basically, you would create a new template group (I usually have one called “db_functions”), and either insert the following code into the index, or another created template. Once it is pasted in, you just point your browser to the page to run it.

<html>
<
head>

<
title>Create Multiple Categories</title>

<
style type="text/css">
    
html {margin:0 padding:0;}
    body {margin
:0; padding:10px; background:#fff; font:normal 80% Verdana, sans-serif; color:#000;}
    
form input, form textarea {width:100%; border:1px solid #000; font:100% Verdana, sans-serif;}
    
form textarea {height:200px;}
    form input
.submit {width:auto;}
    p
.right {text-align:right;}
</style>

</
head>

<
body>

<?php

    
//globals
    
global $DB;
    
    
//if posted, grab info, validate and update the database
    
if(isset($_POST['submit']))
    
{
        
//post vars
        
$group_name = $_POST['group'];
        
$cats         = $_POST['cats'];
        
        
//check fields aren't blank
        
if(!empty($group_name) && !empty($cats))
        
{
            
//split comma delimited data
            
$cats = split(',', $cats);
            
            
/*
            CREATE CATEGORY GROUP
            */
            //get next id
            
$result = $DB->query("SELECT (MAX(group_id) + 1) as group_id FROM exp_category_groups");
            
$group_id = $result->row['group_id'];
            
            
//create group
            
echo('<p>Creating Category Group ... ');
            
$sql = "INSERT INTO exp_category_groups SET group_id = $group_id, group_name = '$group_name'";
            
$DB->query($sql);
            echo(
'Category Group Created Successfully.</p>');
            
            
/*
            CREATE CATEGORIES
            */
            //get next id
            
$result = $DB->query("SELECT (MAX(cat_id) + 1) as cat_id FROM exp_categories");
            
$cat_id = $result->row['cat_id'];
            
            
//get next sort order
            
$result = $DB->query("SELECT (MAX(cat_order) + 1) as cat_order FROM exp_categories");
            
$cat_order = $result->row['cat_order'];
            
            
//loop through categories, and insert into db
            
foreach($cats as $key=>$value)
            
{
                
echo('<p>Inserting Category ' . $key . ' (' . $value . ') ... ');
                
$sql = "INSERT INTO exp_categories SET cat_id = $cat_id, group_id = $group_id, cat_name = '$value', cat_order = $cat_order";
                
$DB->query($sql);
                echo(
'Category Inserted Successfully.</p>');
                
                
//increase cat_id and cat_order
                
$cat_id++;
                
$cat_order++;
            
}
            
            
echo('<p>Upload Complete.</p>');
        
}
        
else
        
{
            
echo('<p>You must provide a Category Group and at least one Category.</p>');
        
}
    }
    
else
    
{
        
//init output
        
$output = "";
        
        
//build form
        
$output .= ('<form method="post" action="{path={segment_1}/{segment_2}}">' . "\r\n");
        
$output .= ('<p><label for="group">Category Group Name:</label><br /><input type="text" name="group" id="group" maxlength="128" /></p>' . "\r\n");
        
$output .= ('
            <p>
                <label for="cats">Categories (Separate each entry with a comma):</label><br />
                <textarea name="cats" id="cats" rows="" cols=""></textarea>
            </p>'
. "\r\n");
        
$output .= ('<p class="right"><label for="submit"></label><input type="submit" name="submit" id="submit" value="Continue �" class="submit" /></p>' . "\r\n");
        
$output .= ('</form>' . "\r\n");
        
        echo(
$output);
    
}    
    
?>
    
</body>
</
html>

Explaining the above code, the html part should be fairly self explanatory as it is your bulk standard framework.

For the php part, first it sets $DB to be global as required to access the database.

Next, the script checks to see if a form has been submitted. If it hasn’t, a form is displayed that will allow the user to specify the name of a Category Group, and to insert the categories they require, using a comma to separate each category.

When the form is processed, the posted variables are checked to make sure that they aren’t blank.

If the variables aren’t blank, the script then splits the categories and puts them into an array called $cats.

Following that, the category group is inserted into the database.

Next, insert these into the categories table of the database.

The script loops through each category, then exits on completion.

Category:Categories Category:Migration

Categories: