简体   繁体   中英

How can I convert a string to a float from a textbox CodeIgniter?

this my method post code

  <form method="post" action='<?php echo site_url('project/save')?>'>
    <input name="number" type="text" maxlength="4"> 

my insert query code in controller

$data=array('number'=>$this->input->post('number')

    $this->db->insert('tb_data',$data);

view code on

    <?php
    $i=1;
        foreach ($row as $r) {
            echo "<tr>

                <td>$r->number</td>

                  </tr>";
                  $i++;
        }
    ?>
  • Create an column named number in your table with float type

  • Create an model with an insert function, after that, in Controller:

     <?php $this->load->model('Modelname_model'); $data=array('number'=>$this->input->post('number') $this->modelname_model->set($data); 

In your frontend, use number_format :

 <?php
//EXAMPLES
// english notation (default)
//$english_format_number = number_format($number);
// 1,235

// French notation
//$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56


    $i=1;
        foreach ($row as $r) {
            echo "<tr>

                <td>" . number_format($r->number, 2, ',', ' ') . "</td>

                  </tr>";
                  $i++;
        }
    ?>

You can use

number_format();

To disply number in float format in your view.

In your view page change

$foo = $r->number;
echo number_format((float)$foo, 2, '.', ''); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM