By dadofgage on
I am working on importing a list of business data into my drupal site.
According to the developer (who provided this code), all I needed to do is to load my data into a temp table in the database which I have done.
From there, run this code and the data would be imported.
When I run this, I don't get any error, but nothing seems to have happened.
Any suggestions as to why?
function import_business() {
$result = db_query("SELECT * FROM {aaaaa} LIMIT 0,10");
while($item = db_fetch_object($result)) {
//store information for each business in temp file
$category = trim($item->category);
$last_name = trim($item->last_name);
$first_name = trim($item->first_name);
$business = trim($item->business);
$address1 = trim($item->address1);
$address2 = trim($item->address2);
$city = trim($item->city);
$state = trim($item->state);
$zip = trim($item->zip);
$phone = trim($item->phone);
$phone = trim($item->phone);
$phone = ereg_replace( '[^0-9]+', '', $phone );
$phone = _convert_phone_to_array($phone);
//insert data
$node = (object)$node;
$node->type = 'business'; //required
$node->uid = 1; //required
$node->name = administrator; //required
$node->taxonomy[1] = $category;
$node->title = $business;
$node->body = '';
$node->street1 = $address1;
$node->street2 = $address2;
$node->firstname = $first_name;
$node->lastname = $last_name;
$node->city = $city;
$node->state = $state;
$node->zip = $zip;
$node->phone1 = $phone;
node_save($node);
}
}
According to him, the node_save will do everything that needs to be done.
Is this correct? Is there something that is missing?
Comments
You might ...
You might want to actually call the function, and not merely define it.
Add import_business().
I have done that (sorry I
I have done that (sorry I wasn't more specific).
From the continued research I have been doing, only the last one is getting placed in the database so that tells me that all of them are being done as an update over each other and so I am only seeing the final of the 10 being visible.
It appears that adding $node->is_new = TRUE; (or some sort of line like this) is what is needed, but this is currently not working.
We are getting closer.
Sneaky mistake, and often
Sneaky mistake, and often hard to track down.
Quick fix: put
unset($node->nid);afternode_save()and before the brackets.Explanation:
The code reuses the $node variable. The first node is saved as a new node, but the node_save() function modifies the $node variable, adding nid. All subsequent calls to node_save() already have the nid defined, so the function updates that first node with the new information. That is why it looks like only the last node is inserted.
- Corey
Thanks for the fix!
That worked now.
I really appreciate the help.