First I just have to say this module totally rocks! Just reporting a small problem with the CSV import. It seems that the import doesn't respect quoted text eg you want to import "5,000". The program currently will break that up into two columns storing 5 and 000.

Not sure if quotes need to be set aside as the way to enclose text (or maybe you select what to use like you would in excel) but this would need to allow your data to have commas, double quotes, single quotes, etc.

Comments

pobster’s picture

Hello!

Sorry wasn't ignoring you (I fixed the NOT NULL issue after you first posted - thanks for spotting that!) the reason I hadn't replied is that I know absolutely nothing at all about CSV... I only implemented it at the request of someone on here and I've never actually used it myself. Anyways, I will update the code if I can - but at present I can't find any coding examples anywhere to suggest the best way to go about handling this function? If you know anything about programming then please help!! :o) I *will* try my best to look into doing it, but as of yet I've not found anything useful... Most examples I've found whilst googling do exactly what I've done in my version (just using commas?) Dunno I'm afraid :o( Need help!

Pobster

pobster’s picture

Assigned: Unassigned » pobster
rbrooks00’s picture

I'll see if I can find an example or two but in the meantime what about checking out the user import: http://drupal.org/node/31940 or the node import: http://drupal.org/project/node_import

Both of those projects have been around for a while and I'd assume they probably deal with this scenario but I haven't used them myself.

pobster’s picture

Okay! Been experimenting... And I've come up with this:

/**
 * Submit function for tablemanager_csv form
 */
function tablemanager_csv_submit($form_id, $form) {
  global $user;
  $whole = trim($form['csvfile']);
  // haven't got a clue what the code for a newline is...
  $temp = explode('
', $whole);
  foreach ($temp as $a) {
    if (!$header) {
      if ($form['header'] == 1) {
        $header = serialize(explode(',', check_plain($a)));
      }
      else {
        $count = count(explode(',', $a));
        $header = array_fill(0, $count, NULL);
        $header = serialize($header);
      }
      // insert header into database
      db_query("INSERT INTO {tablemanager} (tid, uid, name, header) VALUES ('', %d, '', '%s')", $user->uid, $header);
      // update name to show tid
      $sql = db_fetch_object(db_query('SELECT tm.tid
                                       FROM {tablemanager} tm
                                       ORDER BY tm.tid DESC'));
      $tid = $sql->tid;
      db_query("UPDATE {tablemanager} SET name='%s' WHERE tid=%d", $tid, $tid);
      if ($form['header'] == 1) {
        continue;
      }
    }
    $temp = check_markup($a, $form['format']);
    if (substr(trim($temp), 0, 3) == "<p>" && trim(substr(trim($temp), -4, 4) == "</p>")) {
      $temp = substr(trim($temp), 3, -4);
    }
    // experiment
    $temp = explode(',', $temp);
    foreach ($temp as $no => $a) {
      if (substr($a, 0, 1) == '"') {
        array_splice($temp, $no, 2, substr($temp[$no].','.$temp[($no+1)], 1, -1));
      }
    }
    $row = serialize($temp);
    // insert rows into database
    db_query("INSERT INTO {tablemanager_data} (tid, uid, data) VALUES (%d, %d, '%s')", $tid, $user->uid, $row);
  } // end foreach
  variable_set('tablemanager_table', $tid);
  drupal_set_message(t('CSV table has been created.'));
  watchdog('tablemanager', t("tablemanager: added table '%table' from CSV file.", array('%table' => $tid)), WATCHDOG_NOTICE, l('view', 'tablemanager/' . $tid));
  drupal_goto($form['backpage']);
  return;
} // tablemanager_csv_submit

Now the new bit of code starts at // experiment... It currently only supports ONE comma inside of quoted text - so you can import say:

name,age
paul,30
"sheba, dog",9
sally,24

For a table import of the occupants of my house ;o)

Now obviously this isn't ideal as there will be situations where you'll need more than one lot of commas, BUT (!!!!) in doing this it's given me an idea!!! :o) I'm going to attempt to explode each line by " rather than by , - then I should be able to manipulate whats left easier... Note - should be ;o) It's not next on my list of things to do, but given this idea I don't think I'll have any problem doing it.

Pobster

pobster’s picture

Status: Active » Fixed

Ohhhhh okay :o) I thought it'd be easy so I just went ahead and fixed it ;o)

It *should* work, I tested it a little - but feedback would be very much appreciated.

Thanks

Pobster

pobster’s picture

Status: Fixed » Closed (fixed)