your code adds the last column twice.
I made a minor mistake earlier, if you replace the old line I told you to replace with mine, and move my line down to the round() line it should be fine.. however do not run round anymore.
Changing decimal values ?
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
your code as posted:
Notice that you have the basicly same bit of code in the section code block. Notice that after the loop (foreach) that second code block will run.. the data contained in the variables is the last element of the array $data.
Code: Select all
foreach($data as $column)
{
$column = round(mysql_real_escape_string($column),2);
$sql2 .= "'{$column}', ";
}
{
$column = mysql_real_escape_string($column);
$sql2 .= "'{$column}', ";
}This is what I have now, it is importing values, but still rounded to whole numbers with three decimals i.e.
KlasseId LevelId DeelnemerId Tot_percentage Gem_percentage
1 A 2 4.000 0.000
1 A 3 4.000 0.000
It must be that English is not my native language that I don't understand what you're saying..
KlasseId LevelId DeelnemerId Tot_percentage Gem_percentage
1 A 2 4.000 0.000
1 A 3 4.000 0.000
It must be that English is not my native language that I don't understand what you're saying..
Code: Select all
<?php
error_reporting(E_ALL);
mysql_select_db($database_ASN, $ASN);
if (!file_exists('Klassement.csv'))
{
die ("Er is geen Klassement bestand aangetroffen, voer eerst Competitie bijwerken uit vanaf het ASN Beheer menu !");
}
$fp = fopen('Klassement.csv', 'r');
// first line has column names
$data = fgetcsv($fp, 2048, ';');
$columns = array();
mysql_query('DELETE FROM testklassement');
$sql = 'INSERT INTO testklassement (';
$sql .= implode($columns, ', ');
$sql .= ') VALUES (';
// next lines have values
while (($data = fgetcsv($fp, 2048, ';')) !== FALSE)
{
$sql2 = $sql;
foreach($data as $column)
{
$columns[] = preg_replace('#^((?:(?:(?:[0-9]{1,3}\.(?:[0-9]{3}\.)*)(?:[0-9]{3}))|0|[0-9]*))(,[0-9]+)?$#e','str_replace(\'.\',\'\',\'\\1\').str_replace(\',\',\'.\',\'\\2\')',trim($column, '"'));
$sql2 .= "'{$column}', ";
}
$sql2 = rtrim($sql2, ', ');
$sql2 .= ')';
echo 'Executing: ' . $sql2 . '</br>';
mysql_query($sql2) or print(mysql_error() . '<br>');
}
fclose($fp);
?>