Changing decimal values ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post 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 !
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post 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); 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

need to tweak the inserted line, switching $columns[] to $column
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Finally it works..
Thanks very much feyd for being patient with me..
Post Reply