Page 2 of 2

Posted: Fri Aug 05, 2005 10:35 am
by feyd
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.

Posted: Sat Aug 06, 2005 3:48 am
by pookie62
Hi feyd,
I'm not sure what you mean exactly..
How should the script be now ?
I played around with it but cant't get it right..sorry to be such a pain..
Thanks for your time and support !

Posted: Sat Aug 06, 2005 8:10 am
by feyd
your code as posted:

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}', ";
  }
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.

Posted: Sat Aug 06, 2005 9:06 am
by pookie62
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..

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); 
?>

Posted: Sat Aug 06, 2005 10:26 am
by feyd
need to tweak the inserted line, switching $columns[] to $column

Posted: Sat Aug 06, 2005 12:49 pm
by pookie62
Finally it works..
Thanks very much feyd for being patient with me..