php-general Digest 23 Jan 2010 09:53:21 -0000 Issue 6553

Topics (messages 301363 through 301375):

Re: Formatting Decimals Further Help
        301363 by: Rick Dwyer
        301365 by: Nathan Rixham
        301366 by: Rick Dwyer
        301367 by: Nathan Rixham
        301368 by: Rick Dwyer

Re: Formatting Decimals
        301364 by: Nathan Rixham

Re: Cookies & sessions
        301369 by: clancy_1.cybec.com.au
        301371 by: Michael A. Peters

Weird Array Issue...
        301370 by: Don Wieland
        301374 by: Jochem Maas

Subversion Ubuntu client
        301372 by: Skip Evans
        301373 by: shiplu

Re: 304 Not Modified header not working within a class
        301375 by: Camilo Sperberg

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 Jan 22, 2010, at 4:24 PM, tedd wrote:

Hello List.

In an earlier post, I received help with a custom function to round decimals off (the custom function provided by Adam Richardson is below).

However in my MySQL db, when I have values with only 1 decimal point, I need the value PHP returns to display as 2. For example, 3.8 needs to display as 3.80.

My line of code that calls the custom function looks like this:

$my_price = round_to_half_cent(number_format($my_price, 3, '.', ','));

When the value of $my_price is 3.81, it returns 3.81. However, when the value of $my_price is 3.8 that is what it returns.

How can I force the formatting of my_price to always contain either 2 or 3 decimal points (3 if the original number contains 3 or more decimal points to begin with)


Thanks,

--Rick


Rick:

Okay so 3.8 is stored in the database and not as 3.80 -- but that's not a problem. What you have is a display problem so use one of the many PHP functions to display numbers and don't worry about how it's stored.


Hi Ted.

This is exactly what I am trying to do, some of the values in the DB are going to have 3 decimals, some 2 and some 1.

On my page I pull the value from the db with:

$my_price = $row['my_price'];

This returns 3.80... even though my db has it as 3.8. No problem. But once I run the variable $my_price through the following line of code, it is truncating the 0:

$my_price = round_to_half_cent(number_format($my_price, 3, '.', ','));

Again, the above call to the custom function works fine for 2 and 3 decimal points.... just not 1 decimal point. Because I call this function over many pages, I would prefer if possible to fix it at the custom function level rather than recode each page. But I will do whatever is necessary. I afraid with my current understanding of PHP I am not able to successfully modify the custom function to tack on a 0 when the decimal place is empty. Will keep trying and post if I am successful

Thanks,

--Rick




I'm afraid with my current understanding of PHP, I am not able to come up with the logic to

 --Rick



--- End Message ---
--- Begin Message ---
Rick Dwyer wrote:
> 
> On Jan 22, 2010, at 4:24 PM, tedd wrote:
> 
>>> Hello List.
>>>
>>> In an earlier post, I received help with a custom function to round
>>> decimals off (the custom function provided by Adam Richardson is below).
>>>
>>> However in my MySQL db, when I have values with only 1 decimal point,
>>> I need the value PHP returns to display as 2.  For example, 3.8 needs
>>> to display as 3.80.
>>>
>>> My line of code that calls the custom function looks like this:
>>>
>>> $my_price = round_to_half_cent(number_format($my_price, 3, '.', ','));
>>>

your doing the number format before the rounding.. here's a version of
the function that should fit the bill:

function round_to_half_cent( $value )
{
        $value *= 100;
        if( $value == (int)$value || $value < ((int)$value)+0.3 ) {
                return number_format( (int)$value/100 , 2);
        } else if($value > ((int)$value)+0.6) {
                return number_format( (int)++$value/100 , 2);
        }
        return number_format( 0.005+(int)$value/100 , 3);
}


echo round_to_half_cent( 12.1 );       // 12.10
echo round_to_half_cent( 12.103 );     // 12.105
echo round_to_half_cent( 12.107 );     // 12.11
echo round_to_half_cent( 123456.789 ); // 123,456.79



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

On Jan 22, 2010, at 7:30 PM, Nathan Rixham wrote:


Thanks Nathan I'll give it a shot.

 --Rick




your doing the number format before the rounding.. here's a version of
the function that should fit the bill:

function round_to_half_cent( $value )
{
        $value *= 100;
        if( $value == (int)$value || $value < ((int)$value)+0.3 ) {
                return number_format( (int)$value/100 , 2);
        } else if($value > ((int)$value)+0.6) {
                return number_format( (int)++$value/100 , 2);
        }
        return number_format( 0.005+(int)$value/100 , 3);
}


echo round_to_half_cent( 12.1 );       // 12.10
echo round_to_half_cent( 12.103 );     // 12.105
echo round_to_half_cent( 12.107 );     // 12.11
echo round_to_half_cent( 123456.789 ); // 123,456.79








--- End Message ---
--- Begin Message ---
Rick Dwyer wrote:
> On Jan 22, 2010, at 7:30 PM, Nathan Rixham wrote:
> Thanks Nathan I'll give it a shot.
> 

np - here's a more condensed version:

function round_to_half_cent( $value )
{
        $value = ($value*100) + 0.3;
        $out = number_format( floor($value)/100 , 2 );
        return $out . ($value > .5+(int)$value ? '5' : '');
}

>>
>> echo round_to_half_cent( 12.1 );       // 12.10
>> echo round_to_half_cent( 12.103 );     // 12.105
>> echo round_to_half_cent( 12.107 );     // 12.11
>> echo round_to_half_cent( 123456.789 ); // 123,456.79
>>

--- End Message ---
--- Begin Message ---
Thank you Nathan,
This worked quite well.
 --Rick

On Jan 22, 2010, at 8:10 PM, Nathan Rixham wrote:

Rick Dwyer wrote:
On Jan 22, 2010, at 7:30 PM, Nathan Rixham wrote:
Thanks Nathan I'll give it a shot.


np - here's a more condensed version:

function round_to_half_cent( $value )
{
        $value = ($value*100) + 0.3;
        $out = number_format( floor($value)/100 , 2 );
        return $out . ($value > .5+(int)$value ? '5' : '');
}


echo round_to_half_cent( 12.1 );       // 12.10
echo round_to_half_cent( 12.103 );     // 12.105
echo round_to_half_cent( 12.107 );     // 12.11
echo round_to_half_cent( 123456.789 ); // 123,456.79


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php







--- End Message ---
--- Begin Message ---
Paul M Foster wrote:
> On Mon, Jan 11, 2010 at 04:56:03PM -0500, tedd wrote:
> 
>> --Rick:
>>
>> The above described rounding algorithm introduces more bias than
>> simply using PHP's round() function, which always rounds down. IMO,
>> modifying rounding is not worth the effort.
>>
>> The "best" rounding algorithm is to look at the last digit and do this:
>>
>> 0 -- no rounding needed.
>> 1-4 round down.
>> 6-9 round up.
>>
>> In the case of 5, then look to the number that precedes it -- if it
>> is even, then round up and if it is odd, then round down -- or vise
>> versa, it doesn't make any difference as long as you are consistent.
>>
>> Here are some examples:
>>
>> 122.4  <-- round down (122)
>> 122.6 <-- round up (123)
>> 122.5 <-- round up (123)
>>
>> 123.4  <-- round down (123)
>> 123.6 <-- round up (124)
>> 123.5 <-- round down (123)
>>
>> There are people who claim that there's no difference, or are at odds
>> with this method, but they simply have not investigated the problem
>> sufficiently to see the bias that rounding up/down causes. However,
>> that difference is very insignificant and can only be seen after tens
>> of thousands iterations.  PHP's rounding function is quite sufficient.
> 
> This is called (among other things) "banker's rounding". But PHP's
> round() function won't do banker's rounding, as far as I know.
> 
> Paul
> 

floor( $val + 0.5 );

--- End Message ---
--- Begin Message ---
On Thu, 21 Jan 2010 22:00:30 +0000, [email protected] (Ashley Sheridan) 
wrote:

>On Fri, 2010-01-22 at 08:58 +1100, [email protected] wrote:
>
>> On Thu, 21 Jan 2010 08:54:44 -0500, [email protected] (tedd) wrote:
>> 
>> >At 12:15 PM +1100 1/21/10, [email protected] wrote:
>> >>On Wed, 20 Jan 2010 20:05:42 -0200, [email protected] (Bruno Fajardo) 
>> >>wrote:
>> >>
>> >>  >Well, I hope this information is helpful.
>> >>
>> >>Yes, thanks to everyone who contributed.  I now have a better 
>> >>understanding of what
>> >>cookies are, and have turned on output buffering, enabling me to put 
>> >>the handler where I
>> >>want, and still be able to debug it.
>> >>
>> >>Clancy
>> >
>> >One last thing.
>> >
>> >I use sessions for the storage of variables I need between pages, but 
>> >I use cookies to leave data on the user's computer in case they come 
>> >back to my site and want to pick up where they left off.
>> >
>> >Both operations store variables, but are for different purposes.
>> 
>> Yes; I'm doing that too.  I am setting up a private website, and using 
>> cookies to control
>> access to it.
>> 
>> Clancy
>> 
>
>
>Don't use cookies, use sessions for this. Information stored in cookies
>is susceptible to being read by pretty much anyone, hence the scare of
>using cookies that people get. Cookies in themselves are not the
>problem, but using them for anything you want to keep safe, like login
>details, etc, is a bad idea. Generally, a session ID is stored in the
>cookie, which gives nothing away to anyone trying to read it.

Thank you all for your comments.

My reasoning in using a cookie for user recognition, rather than relying on the 
session
ID, was that with a cookie I could ensure that the connection effectively 
lasted for some
specified period, whereas the session ID lifetime seems to be somewhat short and
ill-defined.  In this way I can be sure that the user will not be logged out 
unexpectedly.
The actual value of the cookie I use is an MD5 hash of some user information 
with an
additional random component, so that it would be extremely difficult to extract 
anything
useful from it.  It could equally be a random number, as it is verified by 
matching with a
value stored on the server.  I am also considering changing it every so often 
(every
hour?) while the user is logged in, so that an old value would be useless to a 
hacker.

At present I am using a normal text window for the user to log in, and I 
suspect that this
is by far the weakest link in the system.  The website is relatively obscure, 
and there is
nothing particularly valuable on it, but I would be grateful for any 
suggestions how I
could make this procedure more secure.



--- End Message ---
--- Begin Message ---
[email protected] wrote:


My reasoning in using a cookie for user recognition, rather than relying on the 
session
ID, was that with a cookie I could ensure that the connection effectively 
lasted for some
specified period, whereas the session ID lifetime seems to be somewhat short and
ill-defined.

Shouldn't be.
You can tell sessions to last as long or short as you want.

As far as login goes, there are many ways to do it and the best way depends upon what you are doing.
--- End Message ---
--- Begin Message ---
Hi,

I have defined a stored procedure in my mySQL DB and when I call the procedure in my mySQL browser it returns the CORRECT results:

DROP PROCEDURE IF EXISTS `Get_OHC_Years`;
DELIMITER $$
CREATE definer=`do...@`` PROCEDURE `Get_OHC_Years`()
BEGIN
SELECT (YEAR(ohc_Date)) as ohc_year FROM Office_Hours_Cuttoff GROUP BY YEAR(ohc_Date) ORDER BY YEAR(ohc_Date) ASC;
END
$$

It returns:
-- ohc_year--
2010
2009
2008
2007

I was assuming this will return an array in my PHP when I call it:

    /**
    *Get All Office Hours Cut-off YEARS
    */
    $db->next_result();
$years = $db->query("CALL Get_OHC_Years()") or die("Records not found.");
    $yRow = $years->fetch_array();
    echo "<pre>";
        print_r($yRow);
        echo "</pre>";

But the result it returns on my page is:

Array (
[0] => 2007
 [ohc_year] => 2007

What am I missing?  Thanks!

Don Wieland
D W   D a t a   C o n c e p t s
~~~~~~~~~~~~~~~~~~~~~~~~~
[email protected]
Direct Line - (949) 305-2771

Integrated data solutions to fit your business needs.

Need assistance in dialing in your FileMaker solution? Check out our Developer Support Plan at:
http://www.dwdataconcepts.com/DevSup.html

Appointment 1.0v9 - Powerful Appointment Scheduling for FileMaker Pro 9 or higher
http://www.appointment10.com

For a quick overview -
http://www.appointment10.com/Appt10_Promo/Overview.html


--- End Message ---
--- Begin Message ---
Op 1/23/10 3:28 AM, Don Wieland schreef:
> Hi,
> 
> I have defined a stored procedure in my mySQL DB and when I call the
> procedure in my mySQL browser it returns the CORRECT results:
> 
> DROP PROCEDURE IF EXISTS `Get_OHC_Years`;
> DELIMITER $$
> CREATE definer=`do...@`` PROCEDURE `Get_OHC_Years`()
> BEGIN
>   SELECT (YEAR(ohc_Date)) as ohc_year FROM Office_Hours_Cuttoff GROUP BY
> YEAR(ohc_Date) ORDER BY YEAR(ohc_Date) ASC;
> END
> $$
> 
> It returns:
> -- ohc_year--
> 2010
> 2009
> 2008
> 2007

I doubt it will return the values in the order you have shown.

> 
> I was assuming this will return an array in my PHP when I call it:
> 
>     /**
>     *Get All Office Hours Cut-off YEARS
>     */
>     $db->next_result();

this call to next_result() seems strange.

>     $years = $db->query("CALL Get_OHC_Years()") or die("Records not
> found.");
>     $yRow = $years->fetch_array();
>     echo "<pre>";
>      print_r($yRow);
>      echo "</pre>";
> 
> But the result it returns on my page is:
> 
> Array (
> [0] => 2007
>  [ohc_year] => 2007
> 
> What am I missing?  Thanks!

the bit where you actually RTM or source?

you seem to be assuming what the fetch_array() call does,
not being able to tell exactly what kind of object $db
is I'll hazard a guess that fetch_array() is a wrapper method
for mysql_fetch_array() - you'd want mysql_fetch_assoc()
instead, and you'll need to loop to fetch all the rows.

maybe try something like:

echo "<pre>";
while ($yRow = $years->fetch_assoc())
        print_r($yRow);
echo "</pre>";

--- End Message ---
--- Begin Message ---
Hey all,

Can anyone recommend a good Subversion client for Ubuntu?

Thanks,
Skip

--
====================================
Skip Evans
PenguinSites.com, LLC
503 S Baldwin St, #1
Madison WI 53703
608.250.2720
http://penguinsites.com
------------------------------------
Those of you who believe in
telekinesis, raise my hand.
 -- Kurt Vonnegut

--- End Message ---
--- Begin Message ---
On Sat, Jan 23, 2010 at 10:58 AM, Skip Evans <[email protected]> wrote:
> Hey all,
>
> Can anyone recommend a good Subversion client for Ubuntu?
>

If you use an ide, there should be a subversion client for it. its
better to manage from ide.
Beside this, in kubuntu or ubuntu with kde you can use kdesvn, Its great.
Don't know about GTK. But there should be.

You can find all the subversion related package list by this following
command (without $ sign)

$ apt-cache search subversion



-- 
Shiplu Mokaddim
My talks, http://talk.cmyweb.net
Follow me, http://twitter.com/shiplu
SUST Programmers, http://groups.google.com/group/p2psust
Innovation distinguishes bet ... ... (ask Steve Jobs the rest)

--- End Message ---
--- Begin Message ---
Problem solved!!!!!!!

Everything was working ok with PHP. My class was working ok. The engineering
and logic behind PHP was working. So... what was the problem? Apache...
well, it wasn't a problem, but a misconfiguration or better said, a
mis-optimization.
In my first message, I stated: (quote)

> there is no way I can send a 304 Not Modified header, when the data is
> *over* ~100 bytes.
>

After 8 hours of working with this problem (which included sniffering and a
lot of workarounds), and while I was smoking my final cigarrette before
going to bed, I remembered that some time ago (well, some time like 2 years
ago xD), I had enabled mod_disk_cache, with the following configuration:
<IfModule mod_disk_cache.c>
  CacheRoot /tmp/apachecache/
  CacheEnable disk /
  CacheDirLength 1
  CacheDirLevels 5
  CacheMaxFileSize 128000
  *CacheMinFileSize 100*
</IfModule>

I commented that part, restarted Apache and bingo!!!! Instantly I had an 304
header.
What do I think the problem was? Whenever Apache received a request, it
handled it directly from _his_ cache and simply omitted what PHP was telling
him to do. The weird thing was that the class entered the 304 header part,
but Apache always ended up sending an 200 OK header and then the CSS. In
first place it shouldn't have sended the CSS because when I entered the 304
part, it should have died. It simply couldn't send any other output. (And
that was why I sniffered, if it shouldn't send the CSS; he must have been
send some kind of error, but my surprise was really big when I saw that the
raw data was just plain CSS, no other data was present).
Why was Apache then sending a 304 whenever the data was under the 100 byte
limit? Because he didn't have it in his cache and was obeying what PHP told
him to do. (This configuration created a cache whenever the file size is
between the 100 and 128000 bytes).

Anyway... now I will be publishing the class soon on phpclasses.org under
the BSD license. I'll work now on documentation and code cleanup but
whenever it is ready I will leave the link in this same list (if it is
allowed) xD

Greetings, a lot of thanks to Richard for his code and Rene for his
suggestion to take a look at Apache and good night :P (Despite being 7AM xD)


On Wed, Jan 20, 2010 at 21:16, Camilo Sperberg <[email protected]>wrote:

>
>
> On Wed, Jan 20, 2010 at 04:34, richard gray <[email protected]> wrote:
>
>>
>> Camilo Sperberg wrote:
>>
>>> Hi list, my first message here :)
>>>
>>> To the point: I'm programming a class that takes several CSS files,
>>> parses,
>>> compresses and saves into a cache file. However, I would like to go a
>>> step
>>> further and also use the browser cache, handling the 304 and 200 header
>>> types myself.
>>>
>>> Now, what is the problem? If I do it within a function, there is
>>> absolutely
>>> no problem, everything works like a charm. However, when I implement that
>>> same concept into my class, there is no way I can send a 304 Not Modified
>>> header, when the data is *over* ~100 bytes.
>>>
>>>
>>>
>> Hi Camilo
>>
>> For what it is worth I have implemented cacheing in a class and for me the
>> 304 not modified header gets sent fine ... some example headers output is
>> below together with the relevant code snippet..
>>
>> // See if client sent a page modified header to see if we can
>> // just send a not modified header instead
>> if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
>> $_SERVER['HTTP_IF_MODIFIED_SINCE'] == self::$_gmmodtime) {
>>
>>   header('HTTP/1.1 304 Not Modified');
>>   return null;
>> }
>>
>> if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
>> stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == self::$_etag) {
>>
>>   header('HTTP/1.1 304 Not Modified');
>>   return null;
>> }
>>
>>
>> HTTP/1.x 304 Not Modified
>> Date: Wed, 20 Jan 2010 07:21:32 GMT
>> Server: Apache/2.2.11 (Ubuntu)
>> Connection: Keep-Alive
>> Keep-Alive: timeout=5, max=1000
>> Etag: 444fbd9951f540ec1b6928db864c10dc
>> Expires: Sun, 24 Jan 2010 06:16:06 GMT
>> Cache-Control: public, must-revalidate
>> Vary: Accept-Encoding
>>
>> I hope it helps..
>>
>> Regards
>> Rich
>>
>
> I'll try this (and some other things I recently thought about) when I get
> back home on friday :) I'll keep you updated.
>
> Thanks!
>
>
> --
> Mailed by:
> UnReAl4U - unreal4u
> ICQ #: 54472056
> www1: http://www.chw.net/
> www2: http://unreal4u.com/
>



-- 
Mailed by:
UnReAl4U - unreal4u
ICQ #: 54472056
www1: http://www.chw.net/
www2: http://unreal4u.com/

--- End Message ---

Reply via email to