Skip to content

Commit

Permalink
Add import csv page
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas CARPi committed Mar 26, 2014
1 parent 55b68a2 commit f6eea2e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 46 deletions.
3 changes: 3 additions & 0 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
}
?>

<p><a href='import.php'><img src='themes/<?php echo $_SESSION['prefs']['theme'];?>/img/import.png' alt='import' />
Import CSV file into the database</a></p>

<div id='tabs'>
<ul>
<li><a href='#tabs-1'>Main configuration</a></li>
Expand Down
104 changes: 61 additions & 43 deletions import.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,58 +32,72 @@
$inserted = 0;
$column = array();

// open the file
$handle = fopen('strains.csv', 'r');
if ($handle == false) {
die('Could not open the file.');
}
// file upload block
// show select of type
// SQL to get items names
$sql = "SELECT * FROM items_types";
$req = $pdo->prepare($sql);
$req->execute();
echo "<b>The import will start right after you selected the item type, so be careful, and do it once ! ;)</b><br>";
echo "<b>You should make a backup of your database before importing thousands of items !</b><br><br>";
?>
<p style='text-align:justify'>This page will allow you to import a .csv (Excel spreadsheet) file into the database.
Firt you need to open your (.xls/.xlsx) file in Excel or Libreoffice and save it as .csv.
In order to have a good import, the first column should be the title. The rest of the columns will be imported in the body. You can make a tiny import of 3 lines to see if everything works before you import a big file.
<b>You should make a backup of your database before importing thousands of items !</b></p>

echo "Select a type of item to import to :<select onchange=go_url(this.value)><option value=''>--------</option>";
<label for='item_selector'>1. Select a type of item to import to :</label>
<select id='item_selector' onchange='goNext(this.value)'><option value=''>--------</option>
<?php
while ($items_types = $req->fetch()) {
echo "<option value='import.php?go=1&type=".$items_types['id']."' name='type' ";
echo "<option value='".$items_types['id']."' name='type' ";
echo ">".$items_types['name']."</option>";
}
echo "</select>";

if (isset($_GET['type']) && is_pos_int($_GET['type'])) {
$type = $_GET['type'];
}
// loop the lines
while ($data = fgetcsv($handle, 0, ",")) {
$num = count($data);
// get the column names (first line)
if($row == 0) {
for($i=0;$i < $num;$i++) {
$column[] = $data[$i];
}
$row++;
continue;
?>
</select><br>
<div id='import_block'>
<form enctype="multipart/form-data" action="import.php" method="POST">
<label for='uploader'>2. Select a CSV file to import :</label>
<input id='uploader' name="csvfile" type="file" />
<br>
<br>
<div class='center'>
<button type="submit" class='button' value="Upload">Import CSV</button>
</div>
</form>
</div>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// open the file
$handle = fopen($_FILES['csvfile']['tmp_name'], 'r');
if ($handle == false) {
die('Could not open the file.');
}
$row++;

$title = $data[0];
$body = '';
$j = 0;
foreach($data as $line) {
$body .= "<p><b>".$column[$j]." :</b> ".$line.'</p>';
$j++;
// get what type we want
if (isset($_COOKIE['itemType']) && is_pos_int($_COOKIE['itemType'])) {
$type = $_COOKIE['itemType'];
}
// loop the lines
while ($data = fgetcsv($handle, 0, ",")) {
$num = count($data);
// get the column names (first line)
if($row == 0) {
for($i=0;$i < $num;$i++) {
$column[] = $data[$i];
}
$row++;
continue;
}
$row++;

/*
echo '<h3>'.$title.'</h3>';
echo $body;
echo '<hr>';
*/
$title = $data[0];
$body = '';
$j = 0;
foreach($data as $line) {
$body .= "<p><b>".$column[$j]." :</b> ".$line.'</p>';
$j++;
}


if (isset($_GET['go']) && $_GET['go'] == 1) {
// SQL for importing
$sql = "INSERT INTO items(title, date, body, userid, type) VALUES(:title, :date, :body, :userid, :type)";
$req = $pdo->prepare($sql);
$result = $req->execute(array(
Expand All @@ -97,18 +111,22 @@
$inserted++;
}
}
fclose($handle);
$message = $inserted." items imported.";
display_message('info', $message);
}
echo "<br>".$inserted." items imported.";
fclose($handle);

?>
<script>
function go_url(x) {
function goNext(x) {
if(x == '') {
return;
}
location = x;
document.cookie = 'itemType='+x;
$('#import_block').show();
}
$(document).ready(function() {
$('#import_block').hide();
});
</script>
<?php
require_once 'inc/footer.php';
2 changes: 0 additions & 2 deletions inc/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
$total_time = round(($finish - $start), 4);
echo "Page generated in ".$total_time." seconds.<br />";
?>
<section class='align_right'>
</section>
</footer>
<script src="js/jquery.pageslide.min.js"></script>
<?php
Expand Down
2 changes: 1 addition & 1 deletion inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function has_attachement($id)
{
global $pdo;
$sql = "SELECT id FROM uploads
WHERE item_id = :item_id";
WHERE item_id = :item_id AND type = 'items'";
$req = $pdo->prepare($sql);
$req->execute(array(
'item_id' => $id
Expand Down
Binary file added themes/default/img/import.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added themes/l33t/img/import.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f6eea2e

Please sign in to comment.