Read a txt file by php and mysql and viewed by each user - PART 2

Asked

Viewed 94 times

0

Everything was going fine until I came across a situation I couldn’t resolve so far. I couldn’t adjust the total values of the file.

The example below shows how it is. And should look like this (R$ 335,43). I tried to insert some codes to change the position of the comma and the dot, but I was unsuccessful.

Some codes you put were like this:

echo "Valor: R$ ".number_format($data[5], 0,',','.')

but it didn’t work. Below is the whole script.

ID: 000001 Mothers: 06 Year: 2015 Order: 001 Entity: UNIMED ENTERPRISE (OLD PLAN) Value: R$ 33.543

<?php
ini_set('display_errors',0);
error_reporting(0);
?>
<?php 
$conecta = mysql_connect("localhost","root","");
 mysql_select_db("meu_banco_dados",$conecta);
 $sql = "INSERT INTO extratos (usuario,senha,mes,ano,ordem,ref,total,cod) values";
 $dados = mysql_query("select * from minha_tabela"); 

$arquivo = fopen("meu_arquivo.txt", "r");
if ($arquivo) {
    $total = 0;
    while (($line = fgets($arquivo)) !== false) {

        $data = explode("|",$line);

        echo "ID: ".$data[0]."</br>";
        echo "Mês: ".$data[1]."</br>";
        echo "Ano: ".$data[2]."</br>";
        echo "Ordem: ".$data[3]."</br>";
        echo "Entidade: ".$data[4]."</br>";
        echo "Valor: R$ ".number_format($data[5], 0,',','.')."</br>";
        echo "Cod: ".$data[6]."</br>";
    }

    echo '</br>Total: R$' . number_format($total, 0,',','.');

    fclose($arquivo);
} else {
} 

?>
  • The problem is only to format the value as money?

  • 1

    what is the file value and how do you want it to look? 33,543 is equal to 33,543

  • The amount has to be R $ 335,43.

1 answer

0

Try it this way: Barter:

echo "Valor: R$ ".number_format($data[5], 0,',','.')."</br>";

For:

echo "Valor: R$ ".number_format(str_replace(",", ".", $data[5]),2,',','.')."</br>";
  • It didn’t work. Until I replaced the ,2 with the "0", but the position of the comma still hasn’t changed. ID: 000001 Mãs: 06 Year: 2015 Order: 001 Entity: UNIMED EMPRESARIAL (PL ANTIGO) Value: R$ 33.543 The right is to stay R $ 335,43

  • how is she in the archive? so '33543'?

  • Yes. With several zeros and then the value. " 00000033543".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.