Hello all,
I was wondering if anybody knows a way (a module or an already existing script) to import data into new fields for existing nodes. In other words, I have already several nodes, and what I wanted to do is to create a new field on them and import data from a cvs to them.
Thank you in advance,
Dani
Well...
First you have to have your data in something like a CSV file...
Then you need a script which is going to iterate over each line of the CSV file, see if it can find an existing node to add data to, or else create a new node.
For PHP code iterating over each line of a CSV file, check out the code in http://drupal.org/project/node_import module.
Something like this:
$article_nid = get_nid('title', $article_name, 'article'); if ($article_nid) { $article_node = node_load($article_nid); } else { $article_node = new StdClass(); } $article_node->type = 'article'; $article_node->name = 'Article'; $article_node->format = 3; $article_node->status = 1; $article_node->promote = 0; $article_node->sticky = 0; $log = 'Imported by import-issue ' . date('g:i:s a'); ... // cck fields handled a little differently
$article_node->field_article_publication_issue = array ( 0 => array( 'nid' => $issue_nid, ), ); ... // save node object $article_node = node_submit($article_node); node_save($article_node);
The get_nid function would be something like this (only works for fields like 'title' that are in the {node} table itself) or a simplified version of it:
/** * Return the nid of a node if it exists * * @param $field_name * The field name upon which the query is based. * @param $field_name_string * The string uniquely (in this case) * identifying the node we are searching for. * @param $type * The identifier of the content type. * @return * $nid or false */ function get_nid ($field_name, $field_name_string, $type) { return db_result(db_query("SELECT nid FROM {node} WHERE %s = '%s' AND type = '%s'", $field_name, $field_name_string, $type)); }
Hope that helps...
Victor Kane http://awebfactory.com.ar http://projectflowandtracker.com
On Mon, Dec 28, 2009 at 1:11 PM, Dani Matielo dacamat@gmail.com wrote:
Hello all,
I was wondering if anybody knows a way (a module or an already existing script) to import data into new fields for existing nodes. In other words, I have already several nodes, and what I wanted to do is to create a new field on them and import data from a cvs to them.
Thank you in advance,
Dani
-- Daniela de Carvalho Matielo dacamat.com.br | weblab.tk | lixoeletronico.org twitter: @danimatielo
"The only people for me are the mad ones, the ones who are mad to live, mad to talk, mad to be saved, desirous of everything at the same time, the ones who never yawn or say an uncommon-place thing, but burn, burn, burn like fabulous yellow roman candles." ~ Jack Kerouac
-- [ Drupal support list | http://lists.drupal.org/ ]
Off topic, but Dani, I like your quote ;)
On Tue, Dec 29, 2009 at 12:56 AM, Victor Kane victorkane@gmail.com wrote:
Well...
First you have to have your data in something like a CSV file...
Then you need a script which is going to iterate over each line of the CSV file, see if it can find an existing node to add data to, or else create a new node.
For PHP code iterating over each line of a CSV file, check out the code in http://drupal.org/project/node_import module.
Something like this:
$article_nid = get_nid('title', $article_name, 'article'); if ($article_nid) { $article_node = node_load($article_nid); } else { $article_node = new StdClass(); } $article_node->type = 'article'; $article_node->name = 'Article'; $article_node->format = 3; $article_node->status = 1; $article_node->promote = 0; $article_node->sticky = 0; $log = 'Imported by import-issue ' . date('g:i:s a');... // cck fields handled a little differently
$article_node->field_article_publication_issue = array ( 0 => array( 'nid' => $issue_nid, ), );... // save node object $article_node = node_submit($article_node); node_save($article_node);
The get_nid function would be something like this (only works for fields like 'title' that are in the {node} table itself) or a simplified version of it:
/**
- Return the nid of a node if it exists
- @param $field_name
- The field name upon which the query is based.
- @param $field_name_string
- The string uniquely (in this case)
- identifying the node we are searching for.
- @param $type
- The identifier of the content type.
- @return
- $nid or false
*/ function get_nid ($field_name, $field_name_string, $type) { return db_result(db_query("SELECT nid FROM {node} WHERE %s = '%s' AND type = '%s'", $field_name, $field_name_string, $type)); }
Hope that helps...
Victor Kane http://awebfactory.com.ar http://projectflowandtracker.com
On Mon, Dec 28, 2009 at 1:11 PM, Dani Matielo dacamat@gmail.com wrote:
Hello all,
I was wondering if anybody knows a way (a module or an already existing script) to import data into new fields for existing nodes. In other words, I have already several nodes, and what I wanted to do is to create a new field on them and import data from a cvs to them.
Thank you in advance,
Dani
-- Daniela de Carvalho Matielo dacamat.com.br | weblab.tk | lixoeletronico.org twitter: @danimatielo
"The only people for me are the mad ones, the ones who are mad to live, mad to talk, mad to be saved, desirous of everything at the same time, the ones who never yawn or say an uncommon-place thing, but burn, burn, burn like fabulous yellow roman candles." ~ Jack Kerouac
-- [ Drupal support list | http://lists.drupal.org/ ]
-- [ Drupal support list | http://lists.drupal.org/ ]
Hello, there.
First, happy New Year to everybody. :)
Victor, thank you very much for your reply, I haven't had the chance to try it yet, but I will in the next days and come back here to post the outcomes.
Alexander, just out of curiosity, this was the quote I used in my very first personal website on 1998... :D
best,
Dani
On Tue, Dec 29, 2009 at 12:00 AM, Alexander Arul alexander.arul@gmail.comwrote:
Off topic, but Dani, I like your quote ;)
On Tue, Dec 29, 2009 at 12:56 AM, Victor Kane victorkane@gmail.comwrote:
Well...
First you have to have your data in something like a CSV file...
Then you need a script which is going to iterate over each line of the CSV file, see if it can find an existing node to add data to, or else create a new node.
For PHP code iterating over each line of a CSV file, check out the code in http://drupal.org/project/node_import module.
Something like this:
$article_nid = get_nid('title', $article_name, 'article'); if ($article_nid) { $article_node = node_load($article_nid); } else { $article_node = new StdClass(); } $article_node->type = 'article'; $article_node->name = 'Article'; $article_node->format = 3; $article_node->status = 1; $article_node->promote = 0; $article_node->sticky = 0; $log = 'Imported by import-issue ' . date('g:i:s a');... // cck fields handled a little differently
$article_node->field_article_publication_issue = array ( 0 => array( 'nid' => $issue_nid, ), );... // save node object $article_node = node_submit($article_node); node_save($article_node);
The get_nid function would be something like this (only works for fields like 'title' that are in the {node} table itself) or a simplified version of it:
/**
- Return the nid of a node if it exists
- @param $field_name
- The field name upon which the query is based.
- @param $field_name_string
- The string uniquely (in this case)
- identifying the node we are searching for.
- @param $type
- The identifier of the content type.
- @return
- $nid or false
*/ function get_nid ($field_name, $field_name_string, $type) { return db_result(db_query("SELECT nid FROM {node} WHERE %s = '%s' AND type = '%s'", $field_name, $field_name_string, $type)); }
Hope that helps...
Victor Kane http://awebfactory.com.ar http://projectflowandtracker.com
On Mon, Dec 28, 2009 at 1:11 PM, Dani Matielo dacamat@gmail.com wrote:
Hello all,
I was wondering if anybody knows a way (a module or an already existing script) to import data into new fields for existing nodes. In other words, I have already several nodes, and what I wanted to do is to create a new field on them and import data from a cvs to them.
Thank you in advance,
Dani
-- Daniela de Carvalho Matielo dacamat.com.br | weblab.tk | lixoeletronico.org twitter: @danimatielo
"The only people for me are the mad ones, the ones who are mad to live, mad to talk, mad to be saved, desirous of everything at the same time, the ones who never yawn or say an uncommon-place thing, but burn, burn, burn like fabulous yellow roman candles." ~ Jack Kerouac
-- [ Drupal support list | http://lists.drupal.org/ ]
-- [ Drupal support list | http://lists.drupal.org/ ]
-- [ Drupal support list | http://lists.drupal.org/ ]