Jump to content

[SOLVED] comma screwing up calculations over 1,000


jakebur01

Recommended Posts

My script is working good only it screws up the numbers over 1,000. Should I not be removing the comma before I multiply?

    $targetChars=array('"', '$', ',', '*');

     $dealercheck=$row["$dealer"];
   $dealercheck=str_replace($targetChars, "", $dealercheck);
      $dealercheck = number_format($dealercheck, 2, '.', '');
  
        $distributorcheck=$row["$distributor"];
   $distributorcheck=str_replace($targetChars, "", $distributorcheck);
      $distributorcheck = number_format($distributorcheck, 2, '.', '');
  
    $listprice=$row["$list"];
   $listprice=str_replace($targetChars, "", $listprice);
   $listprice = number_format($listprice, 2, '.', '');
   
    $distributor1=$row["$distributor"];
   $distributor1=str_replace($targetChars, "", $distributor1);
   $cost=$distributor1*.95;
      $cost = number_format($cost, 2, '.', '');
  
   $level1=$row["$dealer"];
   $level1=str_replace($targetChars, "", $level1);
      $level1 = number_format($level1, 2, '.', '');
  
    $level2=$row["$dealer"];
   $level2=str_replace($targetChars, "", $level2);
   $level2=$level2*.96;
      $level2 = number_format($level2, 2, '.', '');
  
    $level3=$row["$dealer"];
   $level3=str_replace($targetChars, "", $level3);
   $level3=$level3*.92;
      $level3 = number_format($level3, 2, '.', '');
  
   $level4=$row["$dealer"];
   $level4=str_replace($targetChars, "", $level4);
   $level4=$level4*.9;
      $level4 = number_format($level4, 2, '.', '');
  
    $level5=$row["$dealer"];
   $level5=str_replace($targetChars, "", $level5);
   $level5=$level5*.88;
      $level5 = number_format($level5, 2, '.', '');
  
      $level6=$row["$dealer"];
   $level6=str_replace($targetChars, "", $level6);
   $level6=$level6*.85;
      $level6 = number_format($level6, 2, '.', '');
  

 

 

Ohh, I see. When I looked at the mysql table. It was using the comma on the price $10,000.00 as a delimiter when processing the text file. Ha...

 

How can I get around this?

<?php
if($filetype==CSV)
{
$del=",";
}
else
{
$del="\t";
}




//Parse the columns by a specified deliminator
//$del = ","; //could be "   " or ";" or anything else you want
//$content = "Hi, Hello, Dog, Cat"; //for testing
$line = explode("\n", $content);

$cols = explode($del, $line[0]);

$sql = "CREATE TABLE `".$table_name."`(";
$i=1;
foreach($cols as $col){
   $sql .= "col".$i++." varchar(256) NULL default '0',";
   }
$sql  = rtrim($sql, ",");//Get rid of that last "," in the statment
$sql .= ");";
//Go ahead and execute however you want. Proble "mysql_query($sql) or die(mysql_error()."<br>".$sql);"
$_SESSION['table_name'] = $table_name; //Setting the table name into a session variable

$db = mysql_connect('connect info') or die(mysql_error()); 
mysql_select_db('database') or die(mysql_error()); 
mysql_query($sql) or die(mysql_error()."<br>".$sql);

$sambo="ALTER TABLE `".$table_name."` ENGINE=myisam";

mysql_query($sambo) or die(mysql_error()."<br>".$sambo);
  
echo "<br /><br />";
$source_file = "C:/Inetpub/Websites//mysite.com/$target";
$query = "load data infile \"$source_file\" INTO TABLE $table_name FIELDS TERMINATED BY ','";
// $query="LOAD DATA INFILE \"$target\" INTO TABLE $table_name FIELDS TERMINATED BY $del TERMINATED BY \n"; 
$result = mysql_db_query(database, $query, $db)
or die(mysql_error()); 
?>

 

 

i think it is better option not to write the prices with commas in the first place in the file which u r loading..ofcourse u should have control of that script..

 

if u want to do it here, u will have to loop through all rows using for loop, format the price properly and use INSERT...so u can no more use LOAD FILE

 

 

this is again as per my knowledge..there can be better ways.

If I were you, I would create another column in this table (type DECIMAL). Then do a one time operation of moving all these values to proper format (probably possible with simple UPDATE query). Then drop the current column.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.