How to create a drupal node programmatically
Many a times we want to create Drupal node programatically when there is a big data in excel format or in some other format and we want it to be added in our Drupal site through cck form cause its time consuming to do that data entry manually.
If you are finding way to do that data entry programatically then here is the solution.
Suppose you want to create nodes for your content type having fields – book number, book name, author name and publication year.
$node = new stdClass(); //Create instance of class stdClass which will create node for you.
$node->type = 'library'; //Name of the content type
$node->field_book_no[value] = 22;//Book number
$node->field_book_name[value] = "Drupal"; //Book name
$node->field_book_author[value]='Sachin'; //Author name
$node->field_year[value]='2011'; //Publication year
$node->uid = 1; // user id, 1 is created by admin
$node->status = 1;//1 is published, 0 is unpublished
$node->promote = 0;//1 is promote to home page, 0 is not to promote on home page
node_save($node); // Save this node
If you are having data in excel format then you can read that excel and and have above code in for/while loop to create nodes for all books.
I have done this by reading excel data and created thousands of nodes for my content type. I have code to read excel file and create these nodes but its not included in this post, you can find excel reader class on Google.
Please feel free to login using google or facebook and create a forum topic to get your query answered in few hours here http://www.learn-drupal.in/forums/forum/drupal-forum-post-installation