Welcome Guest, Not a member yet? Register   Sign In
use string helper with currency
#1

[eluser]Michael Nielsen[/eluser]
I'm building a site for my company and I want to be able to show my customers a preview of products with the specs and price ect.

I have built a basic mock up but when I add a new product to the store I want to be able to just add the price without the $ sign. Is there some way to do this in the controller before it's entered into the db? Kinda like removing the slashes only adding text.
#2

[eluser]TheFuzzy0ne[/eluser]
I don't understand the problem. At what point does the currency symbol get added?
#3

[eluser]jrtashjian[/eluser]
Use str_replace to strip the dollar sign ($) from the string.

Example:
Code:
$price = "$192.00";

$new_price = str_replace('$', '', $price);

// $new_price would be 192.00
#4

[eluser]Michael Nielsen[/eluser]
sorry that I didn't make my problem clear. I want to add the $ sign to the string. so if I entered 192.00 it would be entered into the dv at $192.00

I want to add the $ sign.
#5

[eluser]jrtashjian[/eluser]
Oh, ok. Then all you need to do is concatenate a string, like so:
Code:
$price = "192.00";

$new_price = "$" . $price;
// $new_price is now $192.00
#6

[eluser]Michael Nielsen[/eluser]
thanks, working fine Smile
#7

[eluser]xwero[/eluser]
check out money_format
#8

[eluser]Michael Nielsen[/eluser]
Ok but now I've submitted my form but I can't get it working just right.

Code:
function add()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');
        $data['randomValue'] = random_string('alnum', 9);

        $this->form_validation->set_rules('productName', 'Product Name', 'required');
        $this->form_validation->set_rules('productPrice', 'Price', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('product_add', $data);
        }
        else
        {
            $price = '$'.$_POST['productPrice'];
            $this->db->insert('product', $_POST);
            echo 'Complete';
            echo $price;
        }
    }

If you look at the last line of the function I echo the price which comes out with the $ sign however it's been accepted into the db because it wasn't in the original $_POST.

Any solutions?
#9

[eluser]darkhouse[/eluser]
There are a few issues here.

First, I don't think you should be using the $_POST array as the 2nd parameter of the insert method. I think you should create a new array so that it only has the values you want.

Next, I think don't think you should be storing the $ in the database with the price data. I think the price field should be set to float, or double in the database, and then you add the $ anywhere you display the price. This solves another issue you might run into down the road. What if you need to make your site display different currencies. Many currencies do not use the $ symbol, nor are they in the same format like $1,234.56. If you store the actual numerical value, you can then use that to display in any currency (after you convert it of course) and not worry about the $ symbol.

Last, I don't know if this is common practice for you, but you should really be using better validation rules. Now, this looks like it's the backend where you're inserting products, but just doing 'required' isn't enough. For the product name, you might want to check a max length, and xss_clean. For the price, you'll want to check numeric.

That's my $0.02
#10

[eluser]Michael Nielsen[/eluser]
[quote author="darkhouse" date="1235763582"]There are a few issues here.

First, I don't think you should be using the $_POST array as the 2nd parameter of the insert method. I think you should create a new array so that it only has the values you want.

Next, I think don't think you should be storing the $ in the database with the price data. I think the price field should be set to float, or double in the database, and then you add the $ anywhere you display the price. This solves another issue you might run into down the road. What if you need to make your site display different currencies. Many currencies do not use the $ symbol, nor are they in the same format like $1,234.56. If you store the actual numerical value, you can then use that to display in any currency (after you convert it of course) and not worry about the $ symbol.

Last, I don't know if this is common practice for you, but you should really be using better validation rules. Now, this looks like it's the backend where you're inserting products, but just doing 'required' isn't enough. For the product name, you might want to check a max length, and xss_clean. For the price, you'll want to check numeric.

That's my $0.02[/quote]

Thanks DarkHouse. I understand that the validation rules are very minimal and the reason behind that is that I'm just building the base of the app right now.

As I'm not a seasoned developer would you care to explain (examples) the above methods. Particularly the database.

Also this site is only for New Zealand residence. My goal was to be able to enter a numeric value in the form then before it's entered into the database by the controller have it add $ sign so I don't have to waste time on every entry by manually typing the $ sign.

Could you also further explain putting the data from the form into an arry instead of $_POST




Theme © iAndrew 2016 - Forum software by © MyBB