-1

I'm trying to output a SELECT with comma or even space but I can't design the database structure to use commas och space.

Database:

Cash Varchar 255 Decimals 0 and Default 50000 (I want it to be 50 000 or 50,000)

PHP:

$id = $_SESSION['user_id'];
$get = mysql_query('SELECT * FROM `users` WHERE `id` = '.$id.'')
<?php echo $get_row['cash']; ?>
M.Ali
  • 67,945
  • 13
  • 101
  • 127
Samuel Masih
  • 54
  • 1
  • 8

2 Answers2

2

You don't want to do that in your database. That's where you should just store the number (unformatted). Formatting is for the display script: UI, view, HTML, output etc.

In PHP, do something like this:

$value_from_database = 50000; // use $get_row['cash']
// output in view:
echo '$' . number_format($value_from_database, 2, ',', '.'); // $50,000.00 etc
echo number_format($value_from_database, 0, '', ' '); // 50 000 etc

Manual: http://php.net/number_format

scrowler
  • 24,273
  • 9
  • 60
  • 92
  • The thing is the cash is going to be changing all the time, I just want it to be like 50 000, and when it changes it goes like 54 329 or even 2345. 1 10 100 1000 10 000 100 000 1 000 000 1 000 000 000, How do i output it with space? – Samuel Masih Feb 12 '14 at 01:05
  • Yep, you store the unformatted numbers in the database and add the spaces, commas etc when you want to display it. – scrowler Feb 12 '14 at 01:06
2

I recommend you to change the cash field to decimal(10,2) and use number_format instead of using varchar it's not right format for money/currency value.

//  You want spaces instead of comma and dot
number_format($value, 2, ' ', ' '); // 100000.00 becomes 100 000 00
Sammitch
  • 30,782
  • 7
  • 50
  • 77
The Alpha
  • 143,660
  • 29
  • 287
  • 307