Recent Video
Categories
HowTo Automate Adding Categories
Posted on 2008-01-29
Disclaimer:
This code snippet is provided as an example.
There is no warranty that this code will do anything more than fill up space on your web server. Use at your own risk.
Question: How can I automate creating categories using a .csv file?
Answer: You can add categories in a relatively automatic fashion using some custom code.
NOTE: This code is very rudimentary. There is no built in error checking. Proceed at your own risk.
<?php
global $DB, $REGX;
/* Set variables */
$site_id = 1;
$group_id = 4;
$parent_id = 5;
$table1 = "exp_categories";
$table2 = "exp_category_field_data";
$row = 1;
$handle = fopen("http://www.example.com/edit/states.txt", "r");
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
echo "Title: $data[0]<br />\n";
$url_title = $REGX->create_url_title($data[0]);
$description = $data[0];
echo "URL Title: $url_title<br />\n";
echo "Description: $description<br />\n";
$data1 = array(
'site_id' => $site_id,
'group_id' => $group_id,
'parent_id' => $parent_id,
'cat_name' => $data[0],
'cat_url_title' => $url_title,
'cat_description' => $description);
$DB->query($DB->insert_string($table1, $data1));
$cat_id = $DB->insert_id;
echo "cat_id: $cat_id<br />\n";
$data2 = array (
'cat_id' => $cat_id,
'group_id' => $group_id
);
$DB->query($DB->insert_string($table2, $data2));
$row++;
}
fclose($handle);
?>


