Three are two file needed for import excel into database
//first is import.php
$data = array();
function add_person( $salutation, $first_name, $last_name, $email, $prefered_lang )
{
global $data;
$data []= array(
'salutation' => $salutation,
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'prefered_lang' => $prefered_lang
);
}
if($_POST['submit'] == 'Import')
{
if ( $_FILES['file']['tmp_name'] )
{
$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
$rows = $dom->getElementsByTagName( 'Row' );
$first_row = true;
foreach ($rows as $row)
{
if ( !$first_row )
{
$salutation = "";
$first_name = "";
$last_name = "";
$email = "";
$prefered_lang = "";
$index = 1;
$cells = $row->getElementsByTagName( 'Cell' );
foreach( $cells as $cell )
{
$ind = $cell->getAttribute( 'Index' );
if ( $ind != null ) $index = $ind;
if ( $index == 1 ) $salutation = $cell->nodeValue;
if ( $index == 2 ) $first_name = $cell->nodeValue;
if ( $index == 3 ) $last_name = $cell->nodeValue;
if ( $index == 4 ) $email = $cell->nodeValue;
if ( $index == 5 ) $prefered_lang = $cell->nodeValue;
$index += 1;
}
add_person( $salutation, $first_name, $last_name, $email, $prefered_lang );
}
$first_row = false;
}
}
}
if(!empty($data))
{
$user_id = $id;
$add_date = date('Y-m-d');
$status = 0;
$email_status = 2;
$leader_id = 0;
foreach( $data as $row ) {
$insert_participant="INSERT INTO participant (`user_id`,`salutation`,`first_name`,`last_name`,`email`,`prefered_lang`,`add_date`,`status`,`email_status`,`leader_id`)
VALUES(
'".$user_id."',
'".$row['salutation']."',
'".$row['first_name']."',
'".$row['last_name']."',
'".$row['email']."',
'".$row['prefered_lang']."',
'".date('Y-m-d')."',0,2,0)";
mysql_query($insert_participant);
}
}
?>
//second file my_form.php
form enctype="multipart/form-data" action="" method="post"
input type="hidden" name="MAX_FILE_SIZE" value="2000000"
Import Leaders:
input type="file" name="file"
input type="submit" name="submit" value="Import"
/form
No comments:
Post a Comment