Removing comma on currency string PHP - php

I use this jquery plugin to manage currency input from autoNumber .
From the input, I got the string like this : 30,103,437.60
Now in POST, I have like this :
Array
(
[0] => Array
(
[ID_INVOICE] => 83
[DESCRIPTION] => REPAIR
[QUANTITY] => 1
[PRICE] => 1,523,000.50
)
)
Please see in [PRICE] . In database, I set PRICE FIELD to DECIMAL(10,2). So, I got 1.00 in my database. I think because the comma on POST->PRICE = 1,523,000.50.
My question is, how can I save this on database with right format ?
I am using PHP.

Your actual number should always be separate from the formatting so that you can show it to different users using the locale appropriate for formatting. Use number_format, money_format etc. in php and use the appropriate locale to go to and fro from number to formatted number and back. Even if you are using a jQuery plugin for formatting, I don't understand why it has to make its way to the database as is.

Related

List all combinations of option strings while following order of supplied array and rules of specific options - php

I am working on a project where a company sells a multitude of valves with a vast array of variations and need to build a site map with links to every valve data as the current navigation does not use conventional links but codes built via a user interacting with the many options on the page. I have been able to generate everything up until the options which begin to get very tricky because of the vast array of combinations of these options there are and the rules that they follow.
I am attempting to build every option string combinations with the rules you can find below.
Given the array of option codes (at the end of the post) I must generate a list of all possible combinations but they must follow the order that they are presented and follow the rules given by the row_mod. For example when the option_code of O is in the combinations the option_code of ON and OP are not allowed to be in the combination.
The only row_mod info that needs to be followed currently when building these strings is the "naWithOp##" as that refers to another option that is not allowed to be with it.
Some of the possible combinations that would need to come out of the script would be:
OTKZ
OTK
OT
OTOSZ
OTOS
O
OL
OLKZ
OLK
I know this isn't even close to the exhaustive list of how many are actually possible but I can't figure out how to generate all the possible combinations.
Array
(
[0] => Array
(
[option_code] => O
[row_mod] => allowAll naWithOpON naWithOpOP
)
[1] => Array
(
[option_code] => ON
[row_mod] => allowAll naWithOpO naWithOpOP
)
[2] => Array
(
[option_code] => OP
[row_mod] => allowAll naWithOpO naWithOpON
)
[3] => Array
(
[option_code] => T
[row_mod] => allowAll naWithOpL
)
[4] => Array
(
[option_code] => L
[row_mod] => allowAll naWithOpT naWithCoilX
)
[5] => Array
(
[option_code] => K
[row_mod] => allow2 allow3 allow3P naWithOpOS
)
[6] => Array
(
[option_code] => OS
[row_mod] => allow2 allow3 allow3P naWithOpK
)
[7] => Array
(
[option_code] => Z
[row_mod] => allowAll naWithPreB
)
)
Basically I must start on the first option code and iterate down the list starting with the first given option 'O' then go to the next option 'ON' and check whether its valid and if it is then add it to the current combination. And so on and so forth building allowed combinations and storing them to an array.
User Interface example:
I created a jsfiddle that is interactive in the way that the options work together. Found here
I'm not sure what approach I should be taking but I have tried using recursive algorithms to no avail.
You have eight different options, which appear to be usable either zero or one times. Thus "not used" makes this nine. So you have
9 possibilities for the first option,
8 possibilities for the second (since the first cannot be reused),
7 possibilities for the third (since the first two cannot be reused)
...
Your basic number of options then, not counting the extra rules for option compatibility, is 9!, or 362880 combinations.
You could cycle through all possible combinations, and then write a function to test whether the combination is valid, given the rules for each type. Only list items for which the function returns true, and reject the others. It might be a bit slow, but it'll work.
To generate these combinations, I would visualise the sequence of options as a 9-digit number in base 9 (i.e. you have 9 things and they can each take one of 9 values, subject to limitations I note below). I think I am right in saying this is 99 (38742048910) different combinations, if there was no rule to disallow an option from being used twice.
Thus, just loop through these numbers in base 9, reject any numbers that have any duplicate digits, and of the ones that get through, convert it to an options string, and reject any that don't pass the per-options rule function. The list of items you are left with will be the full set of permitted combinations.

PHP array element starting with "<" weird behavior

I was trying to create some syntax for my application that uses $operator.$columnField as elements of an array for SELECT WHERE clause - something like selecting all ids less than 41 would have been
$parameters['where'] = array('<id'),
$parameters['fields'] = array(':id' => '41')
Then I would have parsed all ['where']s in order to determine the operator from the field itself. The main idea here is not if my way is a good way, given the fact that I can do it in a lot of different approaches. I am interested in the fact that it seems '<' plays some specific role if at the beginning of an array element of type string.
I noticed that there were some errors, so I started testing. Now can anyone tell me why
print_r(array('alfa', '<beta', 'gamma'));
echoes
Array (
[0] => alfa
[1] => gamma
)
Thanks in advance.
Later Edit: If the '<' character is followed by a space, the same does not apply any longer. It simply outputs
Array (
[0] => alfa
[1] => < beta
[2] => gamma
)
It actually works.. The < tag is intrepreted by the browser and it is hiding it from you.
Click Ctrl+U to view the source. You will see this..
Array
(
[0] => alfa
[1] => <beta
[2] => gamma
)
Well , if you want it for display purposes.. Do like this..
<?php
print_r(array_map('htmlentities',array('alfa', '<beta', 'gamma')));

SQL return "variant Object" how to convert to regular data

im a web developer but i dont know much about sql, now i can connect to the server no problem but it returns
variant Object
but in the db that info has data values. how can i convert it?
[0] => Array
(
[0] => .0000
[1] => 70042
[2] => POLLERA LERGA C/CUERO EN CINTO
[3] => 1
[4] => 28
[5] => variant Object
[6] => INV02
[7] =>
[8] =>
[9] => variant Object
[10] => variant Object
[11] => variant Object
)
by the way I'm doing the connection with ADODB.Connection
i had to put
(string)
and it worked.
From the manual:
The VARIANT is COM's equivalent of the PHP zval; it is a structure
that can contain a value with a range of different possible types. The
VARIANT class provided by the COM extension allows you to have more
control over the way that PHP passes values to and from COM.
And:
PHP 5 takes a much simpler approach to handling VARIANTs; when
returning a value or fetching a variant property, the variant is
converted to a PHP value only when there is a direct mapping between
the types that would not result in a loss of information. In all other
cases, the result is returned as an instance of the VARIANT class. You
can force PHP to convert or evaluate the variant as a PHP native type
by using a casting operator explicitly, or implicitly casting to a
string by printing it. You may use the wide range of variant functions
to perform arithmetic operations on variants without forcing a
conversion or risking a loss of data.
You didn't post code but this means that you can normally just print it:
echo $result;
Refer to this for help - i used it as a way of processing variants in php. How do you read from a multidimensional variant array returned from a COM object in PHP?
I've wrestled with this issue for a while and found a solution which worked for me. So let say wee need to return a records set of four values per row from the MSSQL database through ADODB. It all boils down where you cast the datatype! If you cast the datatype before copying the (variant) values into the variable of your choice (mostly some array) it will work.
Example:
$buffer = array();
$sql = "SELECT col1, col2, col3, col4 FROM tbl_mydata WHERE col1 > 100";
$rec = OpenRecordset($sql); // connect to db, make your own!
while (!$rec->EOF){
$buffer[] = array((string)$rec->fields[0], (string)$rec->fields[1], (string)$rec->fields[2], (string)$rec->fields[3]);
$rec->MoveNext();
}
closeRecordset($rec);
for the ADOdb docs see here the manual(http://adodb.org/dokuwiki/doku.php?id=v5:userguide:userguide_index)
Now your $buffer array is ready with string type values.
foreach($buffer as ..... blabla...

Turn X arrays into a single array

I want to change the format of this array to match another one. One was pulled from a database, and the other was made using space delimted user input and then explode();
So here are the arrays I want to change, this is from the database (mysql_fetch_array). It grabs the single field from all rows.
Array
(
[name] => daniel
)
Array
(
[name] => alex
)
Array
(
[name] => richard
)
And here is what I want it to look like. This was the output of the user submitted values, space delimted and using PHPs explode() function.
Array
(
[0] => daniel
[1] => alex
[2] => david
)
What I want to be able to do is have these in the same format so that I can compare the two. The end goal is to be able to compare the two arrays (user input and database results), and create a final array containing only values that the user has inputted that the database doesn't already contain.
So using the data above this would be my final array:
Array
(
[0] => david
)
I would really appreciate help with the first bit, and if anyone else has a better way to achieve the end goal that would be a great extra bonus! (I get the feeling it might be easier to do this with SQL queries but I'm really confusing myself with these arrays)
Thanks!
array_diff($user_array, $database_array);
You can construct the $database_array like this:
$database_array = array();
//assuming each db record has only one value
//while there are still results in the db, do:
$database_array[] = reset($fetched_record);
See array_diff.

Array call from key not working correctly

I'm trying to call to a specific part of an array with the key # and it's not working. I can output the array and see it...
Array
(
[6] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
[7] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
[8] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
)
This array is $emailDB. I can call to the array manually with $emailDB[7] and it works, but if my call is dynamic like this it won't work...
<?php
$value = 7;
print_r($emailDB[$value]);
?>
I've never had an issue like this with an array so it's very odd. What really sucks is I'm under deadline with a form not working on a client's site...joy.
We tried this with no luck...
<?php
$value = 7;
print_r($emailDB[intval($value)]);
?>
I thought intval() would assist but it did not.
You're post implies a bug in php itself, which I highly doubt. What's more likely is that what you posted doesn't properly represent the code you're running.
Why don't try this. Make a brand new empty php file. Hardcode the array keys and values and assign them to the $emailDB variable, and then try
$value = 7;
print_r($emailDB[$value]);
You will see you don't have the problem that you claim. You have now started the debugging process, and now you can look at the working, and non working code to compare the difference.
Well, you are echoing an Array, which I assume is printing "Array" onto your screen. If you want to echo the actual contents of the array, you need to use print_r($array) or echo print_r($array, true). You can also try putting the value in quotes, like $emailDB["{$value}"] to see if that works, I sometimes have troubles with integers not going into things properly.
I agree with you all. It had to have been something whacky with how we were pulling in the data somehow. It was a tab-separated file we were exploding. I just re-wrote the whole thing entirely and imported the data into MySQL and all was well.
In hindsight, I have a sneaking suspicion it was a trim() command that was needed and likely nothing more. Dang it...too late, but I learned something about checking over the code for those types of things.

Categories