Domanda

HTML form

<form action="createpdf.php" method="POST">
    <input type="text" name="title[]" />
    <input type="text" name="unit_price[]" />
    <input type="text" name="item_quantity[]" />
    <input type="text" name="total_cost[]" />
        // etc...
</form>

now send me inserted values to createpdf.php

PHP code for TCPDF converting HTML to PDF / createpdf.php

$html = 'something...';

foreach($_POST['title'] as $key => $v1){
    $html = $html.'
        <tr>
            <td>'.$_POST['item_title'][$key].'</td>
            <td>'.$_POST['unit_price'][$key].'</td>
            <td>'.$_POST['item_quantity'][$key].'</td>
            <td>'.$_POST['total_cost'][$key].'</td>
        </tr>
    ';}

$pdf->writeHTML($html, true, false, true, false, '');

Target

Is there anybody, who can tell me how can I use the following function which should format values before POSTing data to table cells? I need this pattern x xxx.dd and just don't know how... Thanks a lot

function format_price($number,$decPlaces,$decSep,$thouSep){

    //$number - number for format
    //$decPlaces - number of decimal places
    //$decSep - separator for decimals
    //$thouSep - separator for thousands

    //first remove all white spaces
    $number=preg_replace('/\s+/', '',$number);
    //split string into array
    $numberArr = str_split($number);
    //reverse array and not preserve key, keys will help to find decimal place
    $numberArrRev=array_reverse($numberArr);

    //find first occurrence of non number character, that will be a decimal place
    //store $key into variable $decPointIsHere 
    foreach ($numberArrRev as $key => $value) {
        if(!is_numeric($value)){
            if($decPointIsHere==""){
                $decPointIsHere=$key;
            }            
        }
    }

    //decimal comma or whatever it is replace with dot
    //$decPointIsHere is the key of the element that will contain decimal separator dot 
    if($decPointIsHere!=""){
        $numberArrRev[$decPointIsHere]=".";
    }

    //again check through array for non numerical characters but skipping allready processed keys
    //if is not number remove from array

    foreach ($numberArrRev as $key => $value) {
        if(!is_numeric($value) && $key>$decPointIsHere){
            unset($numberArrRev[$key]);            
        }
    }

    //reverse back, at the start reversed array $numberArrRev to $numberArr
    $numberArr=array_reverse($numberArrRev);    

    //create string from array
    $numberClean=implode("",$numberArr);

    // apply php number_format function
    return number_format($numberClean,$decPlaces,$decSep,$thouSep);

}
È stato utile?

Soluzione

try this:

   <td>'.number_format($_POST['unit_price'][$key], 2, ".", " ").'</td>

Altri suggerimenti

From your description, I'd think this should be it:

<td>' . format_price($_POST['unit_price'][$key],2,'.','') . '</td>

The comments tell you how to do it:

format_price($_POST['unit_price'][$key], 2, ".", " ");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top