Reputation: 708
i'm doing a plugin for my wordpress site. in that i need to import contacts in csv file in "ID,Name,Mobile,Email,Age,Gender" format thus it will enter into the db automatically. the code i used is:
<?php global $wpdb;
$mainurl = get_option('siteurl')."/wp-admin/admin.php?page=add_admin_menu_import_contact";
if($_POST['importtrue'] == "true" ) {
if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['import_contact']['name'];
$uploaded_file_type = $_FILES['import_contact']['type'];
$allowed_file_types = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel','text/plain');
//$path= EMAIL_PLUGIN_DIR.'/import_contact/';
if(in_array($uploaded_file_type, $allowed_file_types)) {
// Options array for the wp_handle_upload function. 'test_upload' => false
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload($_FILES['import_contact'], $upload_overrides);
$csv_titles = array( 'eemail_patient_id' => array('ID', 'id'),
'eemail_name_sub' => array('name', 'NAME','Name'),
'eemail_mobile_sub' => array('Mobile', 'phone','Phone','MOBILE'),
'eemail_email_sub' => array('email', 'emailid','EMAIL','Email'),
'eemail_age_sub' => array('Age', 'Old','AGE'),
'eemail_gender_sub' => array('Gender', 'gender','GENDER') );
if ( $movefile ) {
echo "<span style='color:green'>File is valid, and was successfully uploaded.</span>\n";
// var_dump( $movefile);
print_r($csv_titles); echo '<br/>';
if (($handle = fopen($movefile["file"], "r")) !== FALSE)
{
$header = fgetcsv($handle, 1000, ",");
$dbkey = array();
foreach($header as $key) {
echo $key;
foreach ($csv_titles as $keys => $vals)
{
echo '<br/>'.$keys;
echo '<br/>';
print_r($vals);
if (in_array ($key,$vals))
{
$dbkey[]=$keys;
}
else
{
continue;
}
}
}
// echo '<br/>Dbkey: '; var_dump($dbkey);
if(! empty($dbkey)) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$i = 0;
$values = array();
foreach($dbkey as $key) {
if (!empty($data[$i])) {
$values[$key] = $data[$i];
$i++;
}
}
$keys = "`" . implode("`, `", array_keys($values)) . "`";
$values = "'" . implode("', '", $values) . "'";
$sql = "INSERT INTO ".WP_eemail_TABLE_SUB." ({$keys}) VALUES ({$values})";
$wpdb->get_results($sql);
}
$usql="update ".WP_eemail_TABLE_SUB." set patient_date= CURDATE() where patient_date='0000-00-00' ";
$wpdb->get_results($usql);
}
else { echo "<span style='color:red'>But provide valid titles for CSV in first row to import contacts </span>";}
fclose($handle);
}
// unlink($movefile["file"]);
}
} else
{
echo "Not an allowed type!\n";
}
}
?>
<script language="JavaScript" src="<?php echo emailnews_plugin_url('inc/setting.js'); ?>"></script>
<form name="form_importcontact" enctype="multipart/form-data" accept-charset="utf-8" method="post" action="admin.php?page=add_admin_menu_import_contact" onsubmit="return importcontact_submit()" >
<table width="100%">
<tr>
<td align="left" valign="middle" width="10%">Select CSV File:</td>
<td align="left" valign="middle"><input name="import_contact" id="import_contact" type="file" /></td>
</tr>
<tr>
<td height="35" colspan="2" align="left" valign="bottom">
<table width="100%">
<tr>
<td width="50%" align="left">
<input name="publish" lang="publish" class="button-primary" value="Import Contacts" type="submit" />
<input name="publish" lang="publish" class="button-primary" onclick="_cancel_import()" value="Cancel" type="button" />
</td>
<td width="50%" align="right"> </td>
</tr> <input type="hidden" id="importtrue" name="importtrue" />
</table></td>
</tr>
</table>
</form>
</div>
the output i always getting is:
File is valid,and was successfully uploaded.But provide valid titles for CSV in first row to import contacts
when i output the arrays :
$csv_titles: Array ( [eemail_patient_id] => Array ( [0] => ID [1] => id ) [eemail_name_sub] => Array ( [0] => name [1] => NAME [2] => Name ) [eemail_mobile_sub] => Array ( [0] => Mobile [1] => phone [2] => Phone [3] => MOBILE ) [eemail_email_sub] => Array ( [0] => email [1] => emailid [2] => EMAIL [3] => Email ) [eemail_age_sub] => Array ( [0] => Age [1] => Old [2] => AGE ) [eemail_gender_sub] => Array ( [0] => Gender [1] => gender [2] => GENDER ) )
$key: ID Name Mobile Email Age Gender
output of echo 'keys'.$keys
and print_r($vals):
keys: eemail_patient_id
Array ( [0] => ID [1] => id )
keys: eemail_name_sub
Array ( [0] => name [1] => NAME [2] => Name )
keys: eemail_mobile_sub
Array ( [0] => Mobile [1] => phone [2] => Phone [3] => MOBILE )
keys: eemail_email_sub
Array ( [0] => email [1] => emailid [2] => EMAIL [3] => Email )
keys: eemail_age_sub
Array ( [0] => Age [1] => Old [2] => AGE )
keys: eemail_gender_sub
Array ( [0] => Gender [1] => gender [2] => GENDER )
i figured out that the second in_array
if condition is not executing thus the #dbkey is empty. but i can't find out the reason why in_array is false always. logically it seems to be correct for me. please help me to find the reason?
Thanks in advance!
Upvotes: 1
Views: 604
Reputation: 2444
when you execute "echo $key;" you got "ID Name Mobile Email Age Gender".It' a string.So in_array ($key,$vals) never return true .Because $vals doesnot match with "ID Name Mobile Email Age Gender" string.Thats your actual mistake.please explode $key and the use in_array() function :)
Upvotes: 1