php-general Digest 7 May 2007 11:12:26 -0000 Issue 4777

Topics (messages 254487 through 254494):

Re: Removing commas from number
        254487 by: Tijnema !
        254488 by: Paul Novitski
        254490 by: Todd Cary
        254491 by: Todd Cary
        254492 by: Paul Novitski

losing session variable
        254489 by: Alain Roger
        254493 by: itoctopus

PHP 5.2.2 - Undefined Function mysql_connect()
        254494 by: Jason Paschal

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
On 5/6/07, Todd Cary <[EMAIL PROTECTED]> wrote:
Thanks to the suggestions, I use number_format($my_number, 2) to format the
number in an edit field.  Now I need to reenter it into MySQL.  How should I use
preg_replace to remove the commas?

This removes the commas *and* the decimal point:

preg_replace('/\D/', '', $str)

In reviewing patterns, I cannot find the purpose of the "/" character in the 
above.

Many thanks....

Todd

the / in above is the start and end token. for every preg_* function,
this is used.

You could replace it by any other character as long as it is the same
for start and end.

so above could also be
preg_replace('%\D%','',$str);

I use % nearly allways, because i like it, and it isn't used a lot. /
might be used in paths and can get confusing when \ also used.

Tijnema

--- End Message ---
--- Begin Message ---
At 5/6/2007 08:33 AM, Todd Cary wrote:
Thanks to the suggestions, I use number_format($my_number, 2) to format the number in an edit field. Now I need to reenter it into MySQL. How should I use preg_replace to remove the commas?

This removes the commas *and* the decimal point:

preg_replace('/\D/', '', $str)


This strikes me as such an odd thing to do. If the MySQL table field is defined as float, I don't see the need to remove the decimal point.

If you do need to store the digits without the punctuation, you could also multiply by 100 and store it as an integer: intval($my_number * 100) which seems more efficient than to format as a string and then reformat without the punctuation.

If I have two uses for a number -- say, formatted with commas and other dressing for display and formatted more severely for SQL -- I'd retain the number in a variable as its pure value and convert it for each use, rather than converting it for one use and then converting that for the next use. The software's more robust because a glitch in a formatting operation isn't going to affect the final result. Also, in many arithmetic circumstances where division is involved, the true value of the results are accurate to more than two decimal places. While these might be rounded to the nearest cent for display purposes, you'll want to add the true values to get the true total. One common example is a column of percentages that should add to 100%.

Regards,

Paul
__________________________

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com
--- End Message ---
--- Begin Message ---
Paul -

You make a good point.  What is your suggestion for the following sequence:

$number = $row->AMOUNT; // Get the double from MySQL

To display the number in the HTML edit field, I do

$display_number = number_format($number, 2, '.', '');

The user may enter a character that will not be accepted by MySQL, so I do

$mysql_number = preg_replace('/[^0-9^\.]/', '', $display_number);

Thank you for your suggestions....

Todd

Paul Novitski wrote:
At 5/6/2007 08:33 AM, Todd Cary wrote:
Thanks to the suggestions, I use number_format($my_number, 2) to format the number in an edit field. Now I need to reenter it into MySQL. How should I use preg_replace to remove the commas?

This removes the commas *and* the decimal point:

preg_replace('/\D/', '', $str)


This strikes me as such an odd thing to do. If the MySQL table field is defined as float, I don't see the need to remove the decimal point.

If you do need to store the digits without the punctuation, you could also multiply by 100 and store it as an integer: intval($my_number * 100) which seems more efficient than to format as a string and then reformat without the punctuation.

If I have two uses for a number -- say, formatted with commas and other dressing for display and formatted more severely for SQL -- I'd retain the number in a variable as its pure value and convert it for each use, rather than converting it for one use and then converting that for the next use. The software's more robust because a glitch in a formatting operation isn't going to affect the final result. Also, in many arithmetic circumstances where division is involved, the true value of the results are accurate to more than two decimal places. While these might be rounded to the nearest cent for display purposes, you'll want to add the true values to get the true total. One common example is a column of percentages that should add to 100%.

Regards,

Paul
__________________________

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com

--- End Message ---
--- Begin Message ---
Paul -

You make a good point.  What is your suggestion for the following sequence:

$number = $row->AMOUNT; // Get the double from MySQL

To display the number in the HTML edit field, I do

$display_number = number_format($number, 2, '.', '');

The user may enter a character that will not be accepted by MySQL, so I do

$mysql_number = preg_replace('/[^0-9^\.]/', '', $display_number);

Thank you for your suggestions....

Todd

Paul Novitski wrote:
At 5/6/2007 08:33 AM, Todd Cary wrote:
Thanks to the suggestions, I use number_format($my_number, 2) to format the number in an edit field. Now I need to reenter it into MySQL. How should I use preg_replace to remove the commas?

This removes the commas *and* the decimal point:

preg_replace('/\D/', '', $str)


This strikes me as such an odd thing to do. If the MySQL table field is defined as float, I don't see the need to remove the decimal point.

If you do need to store the digits without the punctuation, you could also multiply by 100 and store it as an integer: intval($my_number * 100) which seems more efficient than to format as a string and then reformat without the punctuation.

If I have two uses for a number -- say, formatted with commas and other dressing for display and formatted more severely for SQL -- I'd retain the number in a variable as its pure value and convert it for each use, rather than converting it for one use and then converting that for the next use. The software's more robust because a glitch in a formatting operation isn't going to affect the final result. Also, in many arithmetic circumstances where division is involved, the true value of the results are accurate to more than two decimal places. While these might be rounded to the nearest cent for display purposes, you'll want to add the true values to get the true total. One common example is a column of percentages that should add to 100%.

Regards,

Paul
__________________________

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com

--- End Message ---
--- Begin Message ---
At 5/6/2007 09:39 AM, Todd Cary wrote:
You make a good point.  What is your suggestion for the following sequence:

$number = $row->AMOUNT; // Get the double from MySQL

To display the number in the HTML edit field, I do

$display_number = number_format($number, 2, '.', '');

The user may enter a character that will not be accepted by MySQL, so I do

$mysql_number = preg_replace('/[^0-9^\.]/', '', $display_number);


Ah. I had earlier assumed that you were supplying the same numeric value to two output destinations -- display and SQL. Instead, you're taking a single value from SQL to input and another single value from input to SQL. Even if you understand that these are the same number in the context of the application, they could as easily be totally separate because the two operations are disconnected from one another:

        1) [SQL] --> [transform 1] --> [input]
        2) submit form
        3) [input] --> [transform 2] --> [SQL]

Transform 1 converts from the pure float value to a formatted string, for which number_format() works fine. (You mentioned that these are dollar amounts, but I wouldn't bother including the currency symbol in with the input text, rather more like:

        Enter price: $[__0.00]

where [___] is the input field.)

Transform 2 converts whatever the user has entered into a valid numeric to pass to SQL. For many applications, I don't think a good input validation routine would simply delete any non-numeric character from the input. A user could erroneously type oh for zero, el for one, or hit the shift key while typing a digit. Better, I think, to preserve the input if it isn't valid and ask the user to reconsider. Their own correction of their input might be significantly different from a simple reduction.

A regular expression pattern to check for valid currency input might be:

        [+-($]*\d{1,3}(,\d{3})*(\.\d*){0,1}\){0,1}

[+-($]*         zero or more: plus, minus, open-paren, or currency symbol
\d{1,3}         one to three: numeric digits
(,\d{3})*       zero or more: comma-digit-digit-digit groups
(\.\d*){0,1}    zero or one: decimal point followed by any number of digits
\){0,1}         zero or one: close-paren

Any string failing to match this pattern could warrant an error message.

This example is of course dollar-oriented; you may wish to make your logic apply equally to foreign currencies. Note that different cultures have very different ways of expressing numbers -- comma for the decimal point and period for the thousands separator, different numbers of digits between separators, and different characters mixed with the digits to indicate scale.

Once you accept the input, then you could delete all the characters that aren't digits or period. Keep that decimal point, it's too significant to lose.

Regards,

Paul
__________________________

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com
--- End Message ---
--- Begin Message ---
Hi,

I have the following code :

   session_start();
    $_SESSION['username']=$myusername;
    $_SESSION['Localization'] = $lang;
    header("Location:
https://".$_SERVER['SERVER_NAME'].$path_adm_files."/modules/welcome/welcome.php");


if i check before header command what is the value of $_SESSION['username'],
i get the right value.
however, if i place on the first line of welcome.php file the following code
:

echo "<br>1. Before session";
session_start();
echo"<br>Username : ".$_SESSION['username'];
echo "<br>2. After session";


i get empty value for this session variable.
How is it possible ?

in my first code piece, if i use localhost instead of
$_SERVER['SERVER_NAME'], it creates a session file in my temporary folder as
it should be....
I'm confused now :-(

thanks a lot for your help.
--
Alain
------------------------------------
Windows XP SP2
PostgreSQL 8.1.4
Apache 2.2.4
PHP 5.2.1

--- End Message ---
--- Begin Message ---
session_write_close();

-- 
itoctopus - http://www.itoctopus.com
""Alain Roger"" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hi,
>
> I have the following code :
>
>    session_start();
>>     $_SESSION['username']=$myusername;
>>     $_SESSION['Localization'] = $lang;
>>     header("Location:
>> https://".$_SERVER['SERVER_NAME'].$path_adm_files."/modules/welcome/welcome.php");
>>
>
> if i check before header command what is the value of 
> $_SESSION['username'],
> i get the right value.
> however, if i place on the first line of welcome.php file the following 
> code
> :
>
> echo "<br>1. Before session";
>> session_start();
>> echo"<br>Username : ".$_SESSION['username'];
>> echo "<br>2. After session";
>>
>
> i get empty value for this session variable.
> How is it possible ?
>
> in my first code piece, if i use localhost instead of
> $_SERVER['SERVER_NAME'], it creates a session file in my temporary folder 
> as
> it should be....
> I'm confused now :-(
>
> thanks a lot for your help.
> -- 
> Alain
> ------------------------------------
> Windows XP SP2
> PostgreSQL 8.1.4
> Apache 2.2.4
> PHP 5.2.1
> 

--- End Message ---
--- Begin Message ---
I use this setup locally for testing/playing.  I recently installed the
latest and greatest of the AMP collection.

Apache 2.2.4
PHP 5.2.2
MySQL 5.0.37

Apache and PHP work fine together, but I wanted to hook it up to a local
test database, so  I installed MySQL.

In php.ini, i have:

; Directory in which the loadable extensions (modules) reside.
extension_dir = "C:\PHP"

i made sure that i moved the dll to that folder, just like i did with the gd
dll.

And then i uncommented this line:

extension=php_mysql.dll

I restarted Apache, and ran a simple test script:

<?php
ini_set("display_errors","1");
$cn = mysql_connect("localhost","root","password");
?>

and I get the error:

*Fatal error*: Call to undefined function mysql_connect()

any ideas?

thanks in advance,
jason

--- End Message ---

Reply via email to