Jump to content

Help with a php loop


phelix

Recommended Posts

Ok, I have a .csv file. the first key in the array is the name of the tables. the rest of the info is the data that needs to be put into my mysql database. Here is an example of what my array looks like.

 

 

Array

(

[0] => Array

(

[0] => RECORD_TYPE

[1] => BORROWER_FIRST_NAME

[2] => BORROWER_MIDDLE_NAME

[3] => BORROWER_LAST_NAME

[4] => BORROWER_SUFFIX_NAME

[5] => BORROWER_COMPANY_NAME

[6] => LENDER

[7] => LENDER_ADDRESS

[8] => LENDER_CITY

[9] => LENDER_STATE

[10] => LENDER_ZIP

[11] => LENDER_PHONE_NO

[12] => TRUSTEE

[13] => TRUSTEE_ADDRESS

[14] => TRUSTEE_CITY

[15] => TRUSTEE_STATE

[16] => TRUSTEE_ZIP

[17] => TRUSTEE_PHONE_NO

[18] => PROPERTY_DESCRIPTION

[19] => CASE_NO

[20] => LOAN_NO

[21] => LOAN_DOC_NO

[22] => FORECLOSURE_DOC_NO

[23] => FORECLOSURE_BOOK

[24] => FORECLOSURE_PAGE

[25] => PARCEL_ID_NO

[26] => TRUSTEE_SALE_NUMBER

[27] => SITUS_ADDRESS

[28] => SITUS_UNIT_NO

[29] => SITUS_CITY

[30] => SITUS_COUNTY

[31] => SITUS_STATE

[32] => SITUS_ZIP

[33] => APPRAISED_VALUE

[34] => MINIMUM_BID

[35] => JUDGMENT_AMOUNT

[36] => LOAN_AMOUNT

[37] => LOAN_BALANCE

[38] => DATE_OF_LOAN

[39] => INT_RATE

[40] => PAYMENT

[41] => MATURITY

[42] => AUCTION_DATE

[43] => AUCTION_TIME

[44] => AUCTION_ADDRESS

[45] => AUCTION_CITY

[46] => LEGAL_DESCRIPTION

[47] => RECORDING_DATE

)

 

[1] => Array

(

[0] => T

[1] => KATHLEEN

[2] =>

[3] => PHILLIPS

[4] =>

[5] =>

[6] => NATIONSTAR MORTGAGE, LLC

[7] => JUST LAW, INC

[8] =>

[9] =>

[10] =>

[11] =>

[12] =>

[13] =>

[14] =>

[15] =>

[16] =>

[17] =>

[18] => COMM

[19] =>

[20] =>

[21] =>

[22] =>

[23] =>

[24] =>

[25] => S1134120670

[26] =>

[27] => 11101 W. AMITY RD

[28] =>

[29] => BOISE

[30] => BOISE

[31] => ID

[32] => 83709

[33] =>

[34] =>

[35] =>

[36] =>

[37] => $91,325.12

[38] =>

[39] => 7.88%

[40] =>

[41] =>

[42] => 9/10/2010

[43] => 11:00 AM

[44] => 9465 W. EMERALD ST., STE. 260

[45] => BOISE

[46] => COMMENCING AT THE NORTH QUARTER CORNER OF SECTION 34, TOWNSHIP 3 NORTH, RANGE 1 EAST

[47] => 5/10/2010

)

 

[2] => Array

(

[0] => T

[1] => ARNEL

[2] => T

[3] => BERMUDO

[4] =>

[5] =>

[6] => NATIONSTAR MORTGAGE LLC

[7] => JUST LAW, INC

[8] =>

[9] =>

[10] =>

[11] =>

[12] =>

[13] =>

[14] =>

[15] =>

[16] =>

[17] =>

[18] => RSFR

[19] =>

[20] =>

[21] =>

[22] =>

[23] =>

[24] =>

[25] =>

[26] =>

[27] => 5718 N. MILLDAM PL

[28] =>

[29] => GARDEN CITY

[30] => BOISE

[31] => ID

[32] => 83714

[33] =>

[34] =>

[35] =>

[36] =>

[37] => $109,781.84

[38] =>

[39] => 6.88%

[40] =>

[41] =>

[42] => 9/3/2010

[43] => 11:00 AM

[44] => 9465 W. EMERALD ST., STE. 260

[45] => BOISE

[46] => LOT 12 IN BLOCK 3 OF MILLSTREAM NO. 1 SUBDIVISION, ACCORDING TO THE PLAT THEREOF

[47] => 5/10/2010

)

 

 

Here is the code that I am trying. Obviously its not working properly.

 

$tablenames = $data2DArray[0];

unset($data2DArray[0]);

 

$i=0;

foreach ($tablenames as $tablename){

foreach($data2DArray as $datakey => $data){

echo "{$tablename}={$data[$i]}<br>";

}

$i++;

}

 

 

--------------------------------------

 

Basically I need this to turn into a usable mysql insert query. Need to be able to

 

insert into mysql_table set RECORD_TYPE='T', BORROWER_FIRST_NAME='KATHLEEN' etc

Link to comment
Share on other sites

Those are actually column names not tables names.  Not tested, but this should be easier (note the different insert syntax):

$columns = "`" . implode("`,`", $data2DArray[0]) . "`";
unset($data2DArray[0]);

foreach($data2DArray as $data) {
  $values = array_map('mysql_real_escape_string', $data);
  $values = "'" . implode("','", $values) . "'";
  $query  = "INSERT INTO table_name ($columns) VALUES ($values)";
  // run query
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.