Please, think about this.

I won’t post code often but just think about this.

prepare("insert into `names` (`nm_lastname`, `nm_company`, `nm_i1`, `nm_t1`) values ('-1', :client_name, :sic, :id)");
$cl_nent = $mdb2->prepare("insert into `entityname` (`en_code`, `en_ts`, `en_name`) values ('101', :sale_date, :cl_id)");
$cl_cent = $mdb2->prepare("insert into `entityname` (`en_code`, `en_ts`, `en_entity`, `en_name`) values ('105', :sale_date, :distributor_id, :cl_eid)");
	// Client Contact
$contact = $mdb2->prepare("insert into `names` (`nm_firstname`, `nm_lastname`, `nm_title`) values (:contact_firstname, :contact_lastname, :contact_title)");
$nm_nent = $mdb2->prepare("insert into `entityname` (`en_code`, `en_ts`, `en_name`) values ('101', :sale_date, :nm_id)");
$nm_cent = $mdb2->prepare("insert into `entityname` (`en_code`, `en_ts`, `en_entity`, `en_name`) values ('106', :sale_date, :cl_fid, :nm_eid)");
	// Client Phone
$phone   = $mdb2->prepare("insert into `electronics` (`ele_code`, `ele_num`, `ele_db_autoid`) values ('101', :ele_num_phone, :nm_id)");
	// Client Email
$email   = $mdb2->prepare("insert into `electronics` (`ele_code`, `ele_num`, `ele_db_autoid`) values ('102', :ele_num_email, :nm_id)");
	// Client Address
$address = $mdb2->prepare("insert into `addresses` (`addr_1`, `addr_2`, `addr_city`, `addr_state`, `addr_country`, `addr_zip`, `addr_db_autoid`) values (:addr_1, :addr_2, :addr_city, :addr_state, :addr_country, :addr_zip, :nm_id)");

$file = fopen('clients.csv', "r");
$line = fgetcsv($file);
while($line = fgetcsv($file))
{
	if ($b <= 5)
	{
		list(
			$c_client['id'],
			$c_client['salesperson'],
			$c_client['salesperson_id'],
			$c_client['distributor_id'],
			$c_client['sale_date'],
			$blank,
			$c_client['client_id'],
			$c_client['client_name'],
			$c_client['contact_lastname'],
			$c_client['contact_firstname'],
			$c_client['contact_title'],
			$c_client['addr_1'],
			$c_client['addr_2'],
			$c_client['addr_city'],
			$c_client['addr_state'],
			$c_client['addr_zip'],
			$c_client['addr_country'],
			$c_client['ele_num_phone'],
			$c_client['ele_num_email'],
			$c_client['sic_desc'],
			$c_client['sic']
		) = $line;
		$c_client['contact_lastname']  = ((strlen($c_client['contact_lastname']) > 0)?$c_client['contact_lastname']:"-1");
		$c_client['sale_date']         = date_to_ts($c_client['sale_date']);
		$sic                           = new sic(array("code" => $c_client['sic']));
		print_r_pre($sic->fields);
		
		// Insert Client
		$result = $client->execute(getArray($c_client, array("client_name", "sic", "id")));
		if (PEAR::isError($result))
			echo("CLIENT_NAME: " . $result->getMessage() . "
\n"); else $c_client['cl_id'] = $mdb2->lastInsertID(); // Insert Client 101 $result = $cl_nent->execute(getArray($c_client, array("sale_date", "cl_id"))); if (PEAR::isError($result)) echo("CL_NENT: " . $result->getMessage() . "
\n"); else $c_client['cl_eid'] = $mdb2->lastInsertID(); // Insert Client 105 $result = $cl_cent->execute(getArray($c_client, array("sale_date", "distributor_id", "cl_eid"))); if (PEAR::isError($result)) echo("CL_CENT: " . $result->getMessage() . "
\n"); else $c_client['cl_fid'] = $mdb2->lastInsertID(); // Insert Client Contact if ($c_client['contact_lastname'] == '-1' && $c_client['contact_firstname'] == '') { $c_client['contact_lastname'] = $c_client['client_name']; $c_client['contact_firstname'] = 'Self'; } print_r_pre(getArray($c_client, array("contact_firstname", "contact_lastname", "contact_title"))); $result = $contact->execute(getArray($c_client, array("contact_firstname", "contact_lastname", "contact_title"))); if (PEAR::isError($result)) echo("CONTACT: " . $result->getMessage() . "
\n"); else $c_client['nm_id'] = $mdb2->lastInsertID(); // Insert Client Contact 101 print_r_pre(getArray($c_client, array("sale_date", "nm_id"))); $result = $nm_nent->execute(getArray($c_client, array("sale_date", "nm_id"))); if (PEAR::isError($result)) echo("CONTACT_NENT: " . $result->getMessage() . "
\n"); else $c_client['nm_eid'] = $mdb2->lastInsertID(); // Insert Client Contact 106 print_r_pre(getArray($c_client, array("sale_date", "cl_fid", "nm_eid"))); $result = $nm_cent->execute(getArray($c_client, array("sale_date", "cl_fid", "nm_eid"))); if (PEAR::isError($result)) echo("CONTACT_CENT: " . $result->getMessage() . "
\n"); else $c_client['nm_fid'] = $mdb2->lastInsertID(); // Insert Client Contact Phone if (strlen($c_client['ele_num_phone']) > 1) { $result = $phone->execute(getArray($c_client, array("ele_num_phone", "nm_id"))); if (PEAR::isError($result)) echo("PHONE: " . $result->getMessage() . "
\n"); else $c_client['ph_id'] = $mdb2->lastInsertID(); } // Insert Client Contact Email if (strlen($c_client['ele_num_email']) > 0) { $result = $email->execute(getArray($c_client, array("ele_num_email", "nm_id"))); if (PEAR::isError($result)) echo("EMAIL: " . $result->getMessage() . "
\n"); else $c_client['email_id'] = $mdb2->lastInsertID(); } // Insert Client Contact Address if (strlen($c_client['addr_1']) > 0 || strlen($c_client['addr_2']) > 0 || strlen($c_client['addr_city']) > 0 || strlen($c_client['addr_state']) > 0 || strlen($c_client['addr_country']) > 0 || strlen($c_client['addr_zip']) > 0) { $result = $email->execute(getArray($c_client, array("ele_num_email", "nm_id"))); if (PEAR::isError($result)) echo("ADDRESS: " . $result->getMessage() . "
\n"); else $c_client['addr_id'] = $mdb2->lastInsertID(); } } } function getArray($array, $fields) { foreach ($fields as $k => $v) $newarray[$v] = $array[$v]; return $newarray; } ?>

Then allow your mind to explode.

Join the Conversation

4 Comments

  1. omgnickusoooooocwwwwaaaazy nnndiluffffyewwww

  2. I noticed that this is on the first page of results for lastInsertID() a nice MDB2 function which is kind of hard to track down.  I thought I'd note really quick that lastInsertID is that exact case and is run off the base MDB2 object, not the result returned.  Maybe this will help some people.  =)

    Nick

Leave a comment

Your email address will not be published. Required fields are marked *