php-general Digest 30 Apr 2009 12:50:39 -0000 Issue 6095

Topics (messages 292137 through 292148):

Re: file_get_contents doesn't work on one particular server
        292137 by: Shawn McKenzie

DateInterval
        292138 by: MIke Alaimo
        292139 by: Raymond Irving
        292141 by: MIke Alaimo
        292142 by: Raymond Irving

Two very useful PHP functions
        292140 by: Raymond Irving
        292143 by: Colin Guthrie

Renaming archive entries doesnt work
        292144 by: Darren Karstens

Re: Boolean Parameter to 3 Options?
        292145 by: Richard Quadling

Getting Sub Elements with XPath
        292146 by: AngeloZanetti

Re: php forms - select menu selected behavior
        292147 by: Marcus Gnaß

Warning: Division by zero
        292148 by: Gary

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Shawn McKenzie wrote:
> Brian Dunning wrote:
>> Howdy all - We have a production server that runs our script fine. We're
>> setting up a test server, and this particular script returns a length of
>> zero:
>>
>> $ctx = stream_context_create(array('http' => array('timeout' => 1200)));
>> // 20 minutes per file
>> $contents = file_get_contents($full_url, 0, $ctx);
>>
>> You can paste the $full_url into a browser and it works fine (it's
>> always a PDF document). It works fine with the same variables on the
>> production server. Is there some configuration option that we've missed
>> that's preventing it from working? Note that this is all https, and I
>> did notice this difference in the phpinfo() but don't know if it's
>> relevant:
>>
>> Production server: PHP 5.2.6
>> Registered PHP streams: php, file, data, http, ftp, compress.zlib,
>> compress.bzip2, https, ftps, zip
>>
>> Test server: PHP 5.2.9-2
>> Registered PHP streams: php, file, data, http, ftp, compress.zlib
>>
>>
> 
> Most likely allow_url_fopen = Off in the test server php.ini.  If you
> had error reporting on it would tell you.
> 

Sorry, didn't pay attention to the registered streams :-(

You need to install a PHP package with ssl or compile it with
--with-openssl.

-- 
Thanks!
-Shawn
http://www.spidean.com

--- End Message ---
--- Begin Message ---
Hello,

I would like to know how to correctly use DateInterval::format(). The documentation is unclear.

Also, I am willing to write some documentation for the DateInterval and DatePeriod if no one has taken the task.

Thanks for the help.

Mike

--- End Message ---
--- Begin Message ---
Sounds great Mike.

I personally would also like to see standard date/time functions (strtotime, 
strftime, date, etc) support dates greater than 2038. I think this can be done 
internally by parsing the datetime value swith the DateInterval object and 
retuning the results to the standard functions.

Best regards,
__
Raymond Irving


--- On Wed, 4/29/09, MIke Alaimo <li...@reptiliannature.org> wrote:

From: MIke Alaimo <li...@reptiliannature.org>
Subject: [PHP] DateInterval
To: php-gene...@lists.php.net
Date: Wednesday, April 29, 2009, 8:53 PM

Hello,

I would like to know how to correctly use DateInterval::format().  The 
documentation is unclear.

Also, I am willing to write some documentation for the DateInterval and 
DatePeriod if no one has taken the task.

Thanks for the help.

Mike

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


--- End Message ---
--- Begin Message ---
Here is something i'd like to point out with DateInterval.

$dInt = 'PT4320S';
$d = new DateInterval($dInt);
var_dump($d);
$dz = new DateTimeZone(UTC);
$dt = new DateTime('now',$dz);
$dt->add($d);
var_dump($dt->format(DATE_ATOM));

It appears that running this code fails when it seems like it should work.

$dInt = 'PT43200S';
$d = new DateInterval($dInt);
var_dump($d);
$dz = new DateTimeZone(UTC);
$dt = new DateTime('now',$dz);
$dt->add($d);
var_dump($dt->format(DATE_ATOM));


Please note the first value in $dInt should be 72 minutes, while the later should be 720 minutes or half of a day

The PT12H  interval seems to work fine.

$dInt = 'PT12H';
$d = new DateInterval($dInt);
var_dump($d);
$dz = new DateTimeZone(UTC);
$dt = new DateTime('now',$dz);
$dt->add($d);
var_dump($dt->format(DATE_ATOM));


Raymond, have you tried using DateTime::modify? It appears to work like strtotime.
also DateTime::format works like date.  At least as far as I can tell.


Mike

Raymond Irving wrote:
Sounds great Mike.

I personally would also like to see standard date/time functions (strtotime, strftime, date, etc) support dates greater than 2038. I think this can be done internally by parsing the datetime value swith the DateInterval object and retuning the results to the standard functions.

Best regards,
__
Raymond Irving


--- On *Wed, 4/29/09, MIke Alaimo /<li...@reptiliannature.org>/* wrote:


    From: MIke Alaimo <li...@reptiliannature.org>
    Subject: [PHP] DateInterval
    To: php-gene...@lists.php.net
    Date: Wednesday, April 29, 2009, 8:53 PM

    Hello,

I would like to know how to correctly use DateInterval::format(). The documentation is unclear.

    Also, I am willing to write some documentation for the
    DateInterval and DatePeriod if no one has taken the task.

    Thanks for the help.

    Mike

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



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


--- On Wed, 4/29/09, MIke Alaimo <li...@reptiliannature.org> wrote:
> 
> Raymond, have you tried using DateTime::modify?  It
> appears to work like strtotime.
> also DateTime::format works like date.  At least as
> far as I can tell.
> 


From the little documentation that I've seen it appears that it's similar to 
strtotime and date but it would be very good if the PHP devs could make 
strtotime() and date() become wrappers for DateTime::modify and 
DateTime::format. This way we dont hae to modify our code when moving to a 
version of PHP that supports the new DateTime class.

__
Raymond Irving

--- End Message ---
--- Begin Message ---
Hello,

Every so often I have to be using the isset() function to check if a variable 
exists. It would be useful to have something like an ifset() function

Instead of doing this

$v = isset($input) ? $input : $default;

we can do this

$v = ifset($input,$default); 
// returns $input if it exists otherwise it will return $default

We could also do the same for the empty() function. So instead of doing this

$v = empty($input) ? $default : $input;

we can do this

$v = ifempty($input,$default);
// returns $input if it is not empty otherwise it will return $default

What do you think? Can they make it into 5.3?


Best regards
__
Raymond Irving


--- End Message ---
--- Begin Message ---
'Twas brillig, and Raymond Irving at 30/04/09 03:32 did gyre and gimble:
Hello,

Every so often I have to be using the isset() function to check if a variable 
exists. It would be useful to have something like an ifset() function

Instead of doing this

$v = isset($input) ? $input : $default;

we can do this

$v = ifset($input,$default); // returns $input if it exists otherwise it will return $default

We could also do the same for the empty() function. So instead of doing this

$v = empty($input) ? $default : $input;

we can do this

$v = ifempty($input,$default);
// returns $input if it is not empty otherwise it will return $default

What do you think? Can they make it into 5.3?

To be honest, I don't see the compelling need for this.

I don't disagree that the functionality is nice, however I quite like the verbose versions as they are clearer to understand without knowing an API function.

Also, most of the cases that you would use these functions are with input from GET args and the like. Most frameworks provides wrappers for this with handy ways to get the defaults etc.

So overall, I can't see this becoming a core PHP feature.

Col

--

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
  Mandriva Linux Contributor [http://www.mandriva.com/]
  PulseAudio Hacker [http://www.pulseaudio.org/]
  Trac Hacker [http://trac.edgewall.org/]


--- End Message ---
--- Begin Message ---
Hi,
I am trying to add a prefix to all of the entries in a zip file using
the ZipArchive::renameIndex function which is corrupting the files
when I try to extract them. I was wondering if anyone else has used
this method successfully? Here is the code im using:

$client='TEST';
$zip = new ZipArchive();
if ($zip->open($targetpath,ZIPARCHIVE::OVERWRITE)===true)
{
    // prefix all of the files with the clients name
    for($i = 0; $i < $zip->numFiles; $i++)
    {
        $zip->renameIndex($i,$client.$zip->getNameIndex($i));
    }
    if (!$zip->close()) echo 'Failed to rename zip';
}else echo'Failed to open zip';

This renames the files inside the zip but when I try and extract them
the zip is corrupt.

--- End Message ---
--- Begin Message ---
2009/4/29 Matt Neimeyer <m...@neimeyer.org>:
> On Wed, Apr 29, 2009 at 1:30 PM, Shawn McKenzie <nos...@mckenzies.net> wrote:
>> Philip Thompson wrote:
>>> On Apr 29, 2009, at 11:42 AM, Matt Neimeyer wrote:
>>>
>>>> I have a function that currently takes a boolean value as a parameter.
>>>> But now I want to expand it to 3 options...  So if I have...
>>>>
>>>> function doFooBar($doFoo = false)
>>>>   {
>>>>   if($doFoo)
>>>>      { echo "Did Foo"; }
>>>>   else
>>>>      { echo "Did Bar"; }
>>>>   }
>>>>
>>>> Is it as "simple" as changing it like follows to avoid having to
>>>> change existing code that won't use the new values.
>>>>
>>>> function doFooBar($doFoo = 0)
>>>>   {
>>>>   if($doFoo == 2)
>>>>      { echo "Did Baz"; }
>>>>   else if($doFoo == 1)
>>>>      { echo "Did Foo"; }
>>>>   else
>>>>      { echo "Did Bar"; }
>>>>   }
>>>>
>>>> Something about that disturbs me. Perhaps because any time I think "Oh
>>>> it will be as simple as..." it usually isn't.
>>>>
>>>> Thanks in advance!
>>>>
>>>> Matt
>>>
>>> Unless you're doing a strict comparison (===), it probably won't make a
>>> lot of difference. However, if you sent "true" to the function, I
>>> believe it will reach the last else condition. You may revisit all the
>>> locations you call it and update them appropriately.
>>>
>>> Those are my initial thoughts....
>>>
>>> ~Philip
>>>
>>
>> No, true will match the first condition.  If you're using true and false
>> now and just want to add a second option then continue using true/false.
>>  Don't mix and match.  1 == true and 2 == true.
>>
>> function doFooBar($doFoo = false)
>> {
>>   if($doFoo === 2) {
>>        //something
>>   }
>>   elseif($doFoo === true) {
>>        //something true
>>   }
>>   elseif($doFoo === false) {
>>        //something false
>>   }
>> }
>>
>
> Ah. I somehow missed the direction of the typecasting. Not that the
> documentation isn't completely explicit on the matter but for some
> reason I was thinking that the bool got cast to 1 or 0... not that the
> string/number got cast to a bool (following the standards there).
>
> Good to know.
>
> Thanks
>
> Matt
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Another way ...

<?php
define ('OPTION_NONE', 0);
define ('OPTION_ALL', -1);

// Options are defined with the value of 1, 2, 4, 8, 16, 32, etc.
define ('OPTION_1', 1);
define ('OPTION_2', 2);
define ('OPTION_3', 4);

function foo($i_Options = OPTION_NONE)
        {
        if ($i_Options & OPTION_1) { echo 'Option 1 chosen', PHP_EOL; } //
Note single &
        if ($i_Options & OPTION_2) { echo 'Option 2 chosen', PHP_EOL; } //
Note single &
        if ($i_Options & OPTION_3) { echo 'Option 3 chosen', PHP_EOL; } //
Note single &
        }

foo();
foo(OPTION_ALL);
foo(OPTION_1 | OPTION_3);
?>

outputs ...

Option 1 chosen
Option 2 chosen
Option 3 chosen
Option 1 chosen
Option 3 chosen


-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

--- End Message ---
--- Begin Message ---
Hi all, 

I have the following XML returned to us.

<Response ResponseReference="REF_657-17000-305754723972091">
        <ResponseDetails Language="en">
                <SearchChargeConditionsResponse>
                        <ChargeConditions>
                                <ChargeCondition Type="cancellation">
                                        <Condition Charge="true" FromDay="0" 
ToDay="0" Currency="ZAR"
ChargeAmount="320.00"/>
                                        <Condition Charge="false" FromDay="1"/>
                                </ChargeCondition>
                                <ChargeCondition Type="amendment" 
MaximumPossibleChargesShown="true">
                                        <Condition Charge="false" FromDay="0"/>
                                </ChargeCondition>
                                <PassengerNameChange Allowable="true"/>
                        </ChargeConditions>
                </SearchChargeConditionsResponse>
        </ResponseDetails>
</Response>

We need to get the values of the FromDay, ToDay and ChargeAmount from the
following sub elements: 

<Condition Charge="true" FromDay="0" ToDay="0" Currency="ZAR"
ChargeAmount="320.00"/>
<Condition Charge="false" FromDay="1"/>

I hve the following code: But I am not able to get the second element's
details: (Charge: false, FromDay: 1)

$Elements = $xpath->query(
'ResponseDetails/SearchChargeConditionsResponse/ChargeConditions/ChargeCondition/',
$responseElement );
                                

                                foreach( $Elements as $Element ) 
                                {
                                        
                                        $itemStatus = 
$xpath->query('Condition/Charge' , $Element);
                                        $FromDay2 = 
$xpath->query('Condition/FromDay' , $Element);
                                        
                                        $condition = 
trim($itemStatus->item(0)->textContent);
                                        echo "<br /><br />CONDITION: #" . 
$condition."#";
                                        
                                        $FromDay = 
trim($FromDay2->item(0)->textContent);
                                        echo " FromDay: " . $FromDay;

                                        if ($condition =="true")
                                        {
                                                $ChargeAmount2 = 
$xpath->query('Condition/@ChargeAmount' , $Element);                            
               
                                                $ChargeAmount = 
trim($ChargeAmount2->item(0)->textContent);
                                                echo "  CHARGE AMT: " . 
$ChargeAmount;

                                                $ToDay = 
$xpath->query('Condition/@ToDay' , $Element);
                                                $ToDay = 
trim($ToDay->item(0)->textContent);
                                                echo " ToDay: " . $ToDay;

                                        }
                                        else if($condition =="false")
                                        {
                                                $FromDay2 = 
$xpath->query('Condition/@FromDay' , $Element);
                                                $FromDay = 
trim($FromDay2->item(0)->textContent);
                                                echo " FromDay: " . $FromDay;
                                        }
                                }                                       

It apprears that I need to add a further loop through the "ChargeCondition"
elements, IE

change 

$Elements = $xpath->query(
'ResponseDetails/SearchChargeConditionsResponse/ChargeConditions/ChargeCondition/',
$responseElement );


to

$Elements = $xpath->query(
'ResponseDetails/SearchChargeConditionsResponse/ChargeConditions/',
$responseElement );

Then I can test the type if its "cancellation" and loop through the
subelements (Condition)

Would this appear to be correct?

Anything else that would appear to be incorrect from the above code for the
accompanying structure?

thanks
angelo

-- 
View this message in context: 
http://www.nabble.com/Getting-Sub-Elements-with-XPath-tp23312506p23312506.html
Sent from the PHP - General mailing list archive at Nabble.com.


--- End Message ---
--- Begin Message ---
Troy Oltmanns wrote:
> I have the code below being used to rifle through a list of available
> categories and create select options for them. The code is being used to
> query the database and compare the product category to the current
> iteration, if there's a match, then add selected code so the category is
> prechosen. More code (not included) does the saving and all that, I've check
> phpmyadmin. But when the page submits, the old category appears in the drop
> down as selected. If I leave the page and come back it's fine, it's just
> right after it is saved. The form script is being used on itself, in that
> there is only one file for the form, the submission, etc. All of the other
> input elements will load the data after being saved, is it something
> specific to dropdowns, or it is the way the code is being instatiated?
> 
> All help is much appreciated. Please let me know if anymore info is needed.
> 

//MAKE CATEGORIES DROPDOWN

$catlist1 = "";

// read product
$catmatch = "SELECT prod_cat0 FROM product WHERE dbi='$dbi';";
$catresult = mysql_query($catmatch);
$catquery = mysql_fetch_array($catresult);

// read categories
$sql = "SELECT category FROM categories ORDER BY category;";
$result = mysql_query($sql);
while ($col2 = mysql_fetch_array($result)) {

        $id = $col2["category"];

        if ($id == $catquery['prod_cat0']){

                $catlist1 .= "<option value=\"$id\" 
selected=\"selected\">$id</option>";

        }       else {

                $catlist1 .= "<option value=\"$id\">$id</option>";

        }

}

> 
> to instantiate <?=$catlist1?>
> 

The only data you need from table product is the column prod_cat0, from
table categories it's category, so you should read only the needed data
instead of using * for better performance.

Take the SQL and verify if it returns what you want it to return then.

--- End Message ---
--- Begin Message ---
I have a script that is a result of data entered in a form

On the script (when I test without data entry), I am getting a warning that 
Warning: Division by zero in .inc.php on line 15.

The warning is correct, however the viewer cannot access the second script 
without entering the data that would cancel the warning.

Is this something I should worry about? or would it be better to right in an 
isset?

I'm sorry if this does not seem clear. 



--- End Message ---

Reply via email to