1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| <div id="contentnormal">
<?
$classe = "listing";
$classe2 = "listingbis";
?>
<table border="0" cellspacing="0" cellpadding="0" align="left" width="100%" bgcolor="#999999">
<tr bgcolor="#999999">
<form action="<? echo $_SERVER['PHP_SELF'];?>?act=import" method="post" enctype="multipart/form-data">
<td width="150">
<input type="hidden" name="upl_csv" value="upl_csv">
<input type="hidden" name="idproduct" value="<? echo $idproduct; ?>">
<input type="file" name="photo" size="15">
</td>
<td width="30">
<input id="formgeneral" type="image" src="<? echo $illu;?>validate_mgrey.png" border="0" name="Rechercher" alt="Search" width="25" height="25">
</td>
<td>
Importation pour Injection de fichier de type "EXPORT EXCEL au format CSV en ; " d’après model pré établi !
</td>
</form>
</tr>
</table>
<?php
### Function concept ###
# parameter 'file name' / CSV oriented
# parameter 'table name'
# Action : field type analyse
# Action : 1st line process for field name
# Action : other lines value corrective for specific type
# Action : insert
# Action : satelite actions
#/## Function concept ###
### VARIABLES ###
$sep_first_table = 1; # 1st table ends x field(s) before the end.
#/## VARIABLES ###
switch ($_POST['upl_csv']) {
case "upl_csv": # Upload an original
if ($_FILES['photo']['name'] != '') {
$csvfolder = $doc.'csv/';
$finalfile = $csvfolder.'csv-1.csv';
$_POST['namefile'] = $_FILES['photo']['name'];
#$namefile = $_FILES['photo']['name'];
move_uploaded_file($_FILES['photo']['tmp_name'] , $finalfile); #for historic issues later
chmod ($finalfile, 0777);
}
}
$f = $finalfile;
if(file_exists($f)) {
ini_set('auto_detect_line_endings', true);
$MyFile = file($f); // place the file in an array
$nb = count($MyFile); // count the number of lines or values
echo '<table class="'.$classe.'" border="1" width="100%" cellspacing="1" cellpadding="1" align="center">';
echo '<tr>
<th class="'.$classe.'" width="30">#</th>
<th class="'.$classe.'">Title</th>
<th class="'.$classe.'">Id</th>
</tr>';
$count_line = 0; // to handle 1st line
foreach ($MyFile AS $MyLine) // we handle the array of the file, line by line
{
if ($count_line == '0') {
$line_to_handle = $MyLine;
$MyLine = htmlentities($MyLine);
$explo_field_name = explode(";", $line_to_handle); // 1 XLS value = 1 array value = 1 field_name
$count_field_name = count($explo_field_name); // count the number of Fields
$field_for_insert = $count_field_name - $sep_first_table; // To manage the value addition later => X - Other_Table_fields
$field_for_stock = $count_field_name - $sep_first_table + 1; // To manage the stock addition later
$sql_insert = '';
$turn_field = 0;
foreach ($explo_field_name AS $MyField) { // we handle the array of the 1st line : Field_name
if ($turn_field < $field_for_insert) {
if ($sql_insert != '') { $sql_insert .= ", "; }// for separator between values
$sql_insert .= "`".$MyField."`";
$turn_field++; // for field counter
}
}
$sql_insert_field = 'INSERT INTO `ztestimportproduct` ('.$sql_insert.', `agentmodif`, `datemodif`) VALUES (';
} elseif ($count_line == '1') {
# SUB TITLE line in XLS FILE # To be ignored
} else {
$sql_insert_values = '';
# Other line than 1st and 2nd => INSERT into MySQL
$MyLine = str_replace('"', '', $MyLine);
$MyLine = str_replace("'", "", $MyLine);
$line_to_handle = $MyLine;
$explo_field_value = explode(";", $line_to_handle); // 1 XLS value = 1 array value = 1 field_value
$turn_value = 0;
while ($turn_value < $field_for_insert) # Al fields from 1st table
{
switch ($MyField[$turn_value]) {
case "price_sell": // case = number
case "price_sell_wvat": // case = number
$explo_field_value[$turn_value] = fnbrbk($explo_field_value[$turn_value]); // translate French number in Sql number 'coma into dot'
break;
default:
}
if ($sql_insert_values != '') { $sql_insert_values .= ", "; }// for separator between values
$sql_insert_values .= "'".(str_replace("'", "’", $explo_field_value[$turn_value]))."'";
$turn_value++;
if ($turn_value == '3') { $tourtable++; echo '<tr><td class="'.$classe.'">'.$tourtable.'</td><td class="'.$classe.'">'.(str_replace("'", "’", $explo_field_value[$turn_value])).'</td>'; }
}
$thistime = strftime("%Y-%m-%d %T");
$sql_insert_values .= ", '".$_SESSION['idagent']."', '".$thistime."');";
$sql_insert_final = $sql_insert_field.' '.$sql_insert_values;
# echo '<br>'.$sql_insert_final.'<br>';
$product = new db();
$product->inline("$sql_insert_final");
$idproduct = mysql_insert_id() ;
echo '<td class="'.$classe.'">'.$idproduct.'</td></tr>';
### STOCK
$_POST['idproduct'] = $idproduct;
$_POST['datein'] = $today;
$_POST['type'] = 'stockin';
$_POST['note'] = 'import XLS';
$_POST['quantity'] = $explo_field_value[$turn_value];
if (($_POST['quantity'] >= 1) AND ($_POST['note'] != '')) {
### ADD A STOCK
$ajout = new db('ztestimportstock', 'idstock');
$ajout->AJOUTE(array('idproduct', 'datein', 'quantity', 'type', 'note'));
### ADD A STOCK
}
#/## STOCK
}
$count_line++;
}
echo '</table>';
} else {
echo 'No file';
}
?>
</div> |
Partager