ถามเรื่องการเปลี่ยน format number จาก 1000 ให้ เป็น 1K ยังไงดีค่ะ

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: ถามเรื่องการเปลี่ยน format number จาก 1000 ให้ เป็น 1K ยังไงดีค่ะ

Re: ถามเรื่องการเปลี่ยน format number จาก 1000 ให้ เป็น 1K ยังไงดีค่ะ

โดย supattra_su » 28/08/2018 6:18 pm

มันสามารถแปลงเป็น javascript ได้ไหมค่ะ เพราะที่หนูทำอยู่ มันเป็นแบบ javascript อยู่ค่ะ

Re: ถามเรื่องการเปลี่ยน format number จาก 1000 ให้ เป็น 1K ยังไงดีค่ะ

โดย tsukasaz » 28/08/2018 1:10 pm

ในเนตมีคนเขียนเป็น function ไว้เยอะอยู่ครับ

ตัวอย่าง https://stackoverflow.com/questions/854 ... d-of-1-000

โค้ด: เลือกทั้งหมด

function restyle_text($input){
    $input = number_format($input);
    $input_count = substr_count($input, ',');
    if($input_count != '0'){
        if($input_count == '1'){
            return substr($input, 0, -4).'k';
        } else if($input_count == '2'){
            return substr($input, 0, -8).'mil';
        } else if($input_count == '3'){
            return substr($input, 0,  -12).'bil';
        } else {
            return;
        }
    } else {
        return $input;
    }
} 

ถามเรื่องการเปลี่ยน format number จาก 1000 ให้ เป็น 1K ยังไงดีค่ะ

โดย supattra_su » 28/08/2018 12:02 pm

ถามเรื่องการเปลี่ยน format number จาก 1000 ให้ เป็น 1K ยังไงดีค่ะ
22
22
5555.png (8.37 KiB) Viewed 1009 times
หน้า Controller

โค้ด: เลือกทั้งหมด

<?php
	class cTpstsalHD extends CI_Controller{

		public function __construct(){
			parent::__construct();
		}
		public function index(){

            $this->load->view('common/index');		
        }
        
    function fetchHD(){

         $output = '';
         $query = '';

         $this->load->model('mTpstsalHD');
         
         if($this->input->post('query')){
          $query = $this->input->post('query');
         }
         
            $data = $this->mTpstsalHD->fetch_data($query);
         
         $result = '';
         if(is_array($data) == 1){
            foreach($data as $key => $row){
                $result = $row->TPSTSalHD;
            }
         }    
          echo $result;
         }
     }
?>
    

โค้ด: เลือกทั้งหมด

<script>
        $(document).ready(function(){
            load_data_hd();
        });

    function load_data_hd(query){
        $.ajax({
            url:"tpstsalHD",
            method:"POST",
            data:{query:query},
            success:function(data){
            $('#odvResultVal').html(data);
            $('#odvResultText').html(accounting.formatMoney(data,''));


        }
    });
}
</script>
หน้า index

โค้ด: เลือกทั้งหมด

<div class="col-md-6 text-center">
จำนวนบิล
<div class="xCNDashboardBody">
	<div class="xCNCircle" id="odvCountBill"></div>
        	<div  style="display: none;" id="odvCountBillVal"></div>
	</div>
    </div>
 <div class="col-md-6 text-center">
 ยอดขายรวม
 <div class="xCNDashboardBody">
 <div class="xCNCircle" id="odvResultText"></div>
 <div style="display: none;" id="odvResultVal"></div>
 </div>
 </div>

ข้างบน