Last active
August 28, 2015 14:17
-
-
Save mayeenulislam/03864e63b073fc81694b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Template Name: CSV | |
* | |
* Page template file on Twenty Fifteen theme | |
*/ | |
get_header(); | |
?> | |
<div id="primary" class="content-area"> | |
<main id="main" class="site-main" role="main"> | |
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> | |
<header class="entry-header"> | |
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?> | |
</header><!-- .entry-header --> | |
<div class="entry-content"> | |
<?php | |
if( isset($_POST['csv_submit'] ) ) { | |
if( $_FILES['csv']['size'] > 0 ) { | |
// get the csv file | |
$file = $_FILES['csv']['tmp_name']; | |
// read the file | |
$handle = fopen( $file, 'r' ); | |
// a simple counter | |
$find_header = 0; | |
while( $data = fgetcsv( $handle ) ) { | |
// update counter | |
$find_header++; | |
// skipping the header line from CSV | |
if( $find_header > 1 ) { | |
global $current_user; | |
$user_id = $current_user->ID; | |
//as post type data (CPT: 'employees') | |
$post_id = wp_insert_post( array( | |
'post_status' => 'publish', | |
'post_type' => 'employees', //assumed we've a CPT | |
'post_author' => $user_id, | |
'post_title' => $data[1] //name field | |
) ); | |
//as custom field | |
if( !is_wp_error( $post_id ) ) { | |
update_post_meta( $post_id, 'emp_id', $data[0] ); //id | |
update_post_meta( $post_id, 'emp_desig', $data[2] ); //designation | |
update_post_meta( $post_id, 'emp_age', $data[3] ); //age | |
} | |
} | |
} | |
} | |
} | |
?> | |
<form method="post" enctype="multipart/form-data"> | |
<p>Choose a <code>.csv</code> file and hit the <kbd>submit</kbd> button to upload:</p> | |
<input type="file" name="csv"><br> | |
<input type="submit" name="csv_submit" value="Submit"> | |
</form> | |
</div><!-- .entry-content --> | |
</article><!-- #post --> | |
</main><!-- .site-main --> | |
</div><!-- .content-area --> | |
<?php get_sidebar(); ?> | |
<?php get_footer(); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Connecting to database | |
$connect = mysql_connect( "localhost", "root", "" ); //db_host, db_username, db_password | |
mysql_select_db( "csv_db", $connect ); //select the database, i.e. db name is 'csv_db' | |
if( isset($_POST['csv_submit'] ) ) { | |
if( $_FILES['csv']['size'] > 0 ) { | |
// get the csv file | |
$file = $_FILES['csv']['tmp_name']; | |
// read the file | |
$handle = fopen( $file, 'r' ); | |
// a simple counter | |
$find_header = 0; | |
while( $data = fgetcsv( $handle ) ) { | |
// update counter | |
$find_header++; | |
// skipping the header line from CSV | |
if( $find_header > 1 ) { | |
$sql = mysql_query( "INSERT INTO csv_table ( | |
id, | |
name, | |
designation, | |
age | |
) VALUES ( | |
'".$data[0] ."', | |
'".$data[1] ."', | |
'".$data[2] ."', | |
'".$data[3] ."' | |
) | |
"); | |
} | |
} | |
} | |
} | |
?> | |
<form method="post" enctype="multipart/form-data"> | |
<p>Choose a <code>.csv</code> file and hit the <kbd>submit</kbd> button to upload:</p> | |
<input type="file" name="csv"><br> | |
<input type="submit" name="csv_submit" value="Submit"> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if( isset($_POST['csv_submit'] ) ) { | |
if( $_FILES['csv']['size'] > 0 ) { | |
//get the csv file | |
$file = $_FILES['csv']['tmp_name']; | |
// read the file | |
$handle = fopen( $file, 'r' ); | |
while( $data = fgetcsv( $handle ) ) { | |
echo $data[0], ' | ', $data[1], ' | ', $data[2], ' | ', $data[3], '<hr>'; | |
} | |
} | |
} | |
?> | |
<form method="post" enctype="multipart/form-data"> | |
<p>Choose a <code>.csv</code> file and hit the <kbd>submit</kbd> button to upload:</p> | |
<input type="file" name="csv"><br> | |
<input type="submit" name="csv_submit" value="Submit"> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment