php-general Digest 23 Aug 2008 13:54:38 -0000 Issue 5640

Topics (messages 278555 through 278563):

Re: Quick question regarding getcwd() and directory location.
        278555 by: Eric Butera
        278556 by: Robert Cummings
        278560 by: Ólafur Waage

Re: Sorting Arrays
        278557 by: Jim Lucas

Re: Which Exception to use ....
        278558 by: David Otton
        278561 by: Jochem Maas
        278562 by: Jochem Maas

Help and Advice needed please.
        278559 by: Byron

Re: Restore Leading Zeros in Zip Codes
        278563 by: tedd

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 Thu, Aug 21, 2008 at 8:50 PM, Ólafur Waage <[EMAIL PROTECTED]> wrote:
> I'll throw out an example here.
>
> I have a directory structure of: /var/www/example/
> And in that i have a file: index.php
> That file echo's getcwd() and returns: /var/www/example/
>
> Now i tell Apache or Lighty that if a directory does not have an
> index.php file, that it should use /example/index.php (for example via
> DirectoryIndex of Apache)
>
> Then i make a new directory: /var/www/test/
> And browse to it and it echo's /var/www/example/ since its running
> that file via DirectoryIndex
>
> This is true for all directories i make. Both for Windows and Linux
> based servers.
>
> To my question.
>
> I am trying to get the full path of the current directory i am in. Not
> from where the file is executing. How is that possible so it displays
> a full path to that directory (regardless to the server setup)?
>
> I have tried a variation of things and have gotten mixed results.
> Using $_SERVER["SCRIPT_FILENAME"] gives me the index file and the full
> path to that.
> $_SERVER["DOCUMENT_ROOT"] gets me part of the way but on a hosted
> server with multiple domains it only gives me a partial path (rough
> example: /var/www/ when the directory is /var/www/domain.com/test/)
> And __FILE__ gives me the index.php's path.
> I have also tried realpath(".");
> And server("pwd"); with no luck.
>
> Any help would be appreciated.
>
> Ólafur Waage
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Maybe dirname(__FILE__) will help.

--- End Message ---
--- Begin Message ---
On Fri, 2008-08-22 at 22:13 -0400, Eric Butera wrote:
> On Thu, Aug 21, 2008 at 8:50 PM, Ólafur Waage <[EMAIL PROTECTED]> wrote:
> > I'll throw out an example here.
> >
> > I have a directory structure of: /var/www/example/
> > And in that i have a file: index.php
> > That file echo's getcwd() and returns: /var/www/example/
> >
> > Now i tell Apache or Lighty that if a directory does not have an
> > index.php file, that it should use /example/index.php (for example via
> > DirectoryIndex of Apache)
> >
> > Then i make a new directory: /var/www/test/
> > And browse to it and it echo's /var/www/example/ since its running
> > that file via DirectoryIndex
> >
> > This is true for all directories i make. Both for Windows and Linux
> > based servers.
> >
> > To my question.
> >
> > I am trying to get the full path of the current directory i am in. Not
> > from where the file is executing. How is that possible so it displays
> > a full path to that directory (regardless to the server setup)?
> >
> > I have tried a variation of things and have gotten mixed results.
> > Using $_SERVER["SCRIPT_FILENAME"] gives me the index file and the full
> > path to that.
> > $_SERVER["DOCUMENT_ROOT"] gets me part of the way but on a hosted
> > server with multiple domains it only gives me a partial path (rough
> > example: /var/www/ when the directory is /var/www/domain.com/test/)
> > And __FILE__ gives me the index.php's path.
> > I have also tried realpath(".");
> > And server("pwd"); with no luck.
> >
> > Any help would be appreciated.
> >
> > Ólafur Waage
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> 
> Maybe dirname(__FILE__) will help.

When I'm looking for candidates to determine stuff like this I usually
do print_r( $_SERVER ) and see what's what.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


--- End Message ---
--- Begin Message ---
Robert, thanks for the reply but i had tried __FILE__ and __DIR__
(which is dirname(__FILE__)) but it doesnt work.

And thanks for the reply also Ashley but as i said in my first post, i
had tried $_SERVER with limited results

Ólafur Waage

2008/8/23 Robert Cummings <[EMAIL PROTECTED]>:
> On Fri, 2008-08-22 at 22:13 -0400, Eric Butera wrote:
>> On Thu, Aug 21, 2008 at 8:50 PM, Ólafur Waage <[EMAIL PROTECTED]> wrote:
>> > I'll throw out an example here.
>> >
>> > I have a directory structure of: /var/www/example/
>> > And in that i have a file: index.php
>> > That file echo's getcwd() and returns: /var/www/example/
>> >
>> > Now i tell Apache or Lighty that if a directory does not have an
>> > index.php file, that it should use /example/index.php (for example via
>> > DirectoryIndex of Apache)
>> >
>> > Then i make a new directory: /var/www/test/
>> > And browse to it and it echo's /var/www/example/ since its running
>> > that file via DirectoryIndex
>> >
>> > This is true for all directories i make. Both for Windows and Linux
>> > based servers.
>> >
>> > To my question.
>> >
>> > I am trying to get the full path of the current directory i am in. Not
>> > from where the file is executing. How is that possible so it displays
>> > a full path to that directory (regardless to the server setup)?
>> >
>> > I have tried a variation of things and have gotten mixed results.
>> > Using $_SERVER["SCRIPT_FILENAME"] gives me the index file and the full
>> > path to that.
>> > $_SERVER["DOCUMENT_ROOT"] gets me part of the way but on a hosted
>> > server with multiple domains it only gives me a partial path (rough
>> > example: /var/www/ when the directory is /var/www/domain.com/test/)
>> > And __FILE__ gives me the index.php's path.
>> > I have also tried realpath(".");
>> > And server("pwd"); with no luck.
>> >
>> > Any help would be appreciated.
>> >
>> > Ólafur Waage
>> >
>> > --
>> > PHP General Mailing List (http://www.php.net/)
>> > To unsubscribe, visit: http://www.php.net/unsub.php
>> >
>> >
>>
>> Maybe dirname(__FILE__) will help.
>
> When I'm looking for candidates to determine stuff like this I usually
> do print_r( $_SERVER ) and see what's what.
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
>

--- End Message ---
--- Begin Message ---
Tom Shaw wrote:
I'm having a problem sorting my array and wondered if anybody had experience
sorting arrays by their values. What I need to do is resort the array below
where the most expensive product shipping price starts at position zero no
matter how big the array is.

array(2) {

  [0] => array(48) {

    ["product_id"] => string(2) "34"

    ["product_name"] => string(29) "Bears Ball Cap"

    ["product_ordered_size"] => string(5) "ADULT"

    ["product_sales_price"] => string(8) "11.90"

    ["product_shipping_price"] => string(4) "7.85"

    ["product_shipping_extra"] => string(4) "0.06"

  }

  [1] => array(48) {

    ["product_id"] => string(2) "37"

    ["product_name"] => string(21) "Baldwin L Grand Piano"

    ["product_ordered_size"] => string(5) "Grand"

    ["product_sales_price"] => string(8) "11671.90"

    ["product_shipping_price"] => string(6) "500.00"

    ["product_shipping_extra"] => string(6) "450.00"

  }

Thanks
[EMAIL PROTECTED]



Everything can be done with three custom lines of code, the rest is PHP existing function calls.

http://us3.php.net/manual/en/function.array-multisort.php

Example #3 Sorting database results

I expanded your list below and randomly added some data... Anyways, replace my $ar array definition with your array and you should be good to go. One question though, is this coming from a DB? If so, why not do the sorting in the SQL call instead of PHP? Just a thought.


<plaintext><?php

$ar = array(
  array(
    "product_id" => "34",
    "product_name" => "Bears Ball Cap #34",
    "product_ordered_size" => "ADULT",
    "product_sales_price" => "11.90",
    "product_shipping_price" => "7.85",
    "product_shipping_extra" => "0.06",
  ),
  array(
    "product_id" => "35",
    "product_name" => "Baldwin L Grand Piano #35",
    "product_ordered_size" => "Grand",
    "product_sales_price" => "11671.90",
    "product_shipping_price" => "51.00",
    "product_shipping_extra" => "450.00",
  ),
  array(
    "product_id" => "36",
    "product_name" => "Baldwin L Grand Piano #36",
    "product_ordered_size" => "Grand",
    "product_sales_price" => "11671.90",
    "product_shipping_price" => "500.00",
    "product_shipping_extra" => "450.00",
  ),
  array(
    "product_id" => "37",
    "product_name" => "Baldwin L Grand Piano #37",
    "product_ordered_size" => "Grand",
    "product_sales_price" => "11671.90",
    "product_shipping_price" => "5000.00",
    "product_shipping_extra" => "450.00",
  ),
  array(
    "product_id" => "38",
    "product_name" => "Baldwin L Grand Piano #38",
    "product_ordered_size" => "Grand",
    "product_sales_price" => "11671.90",
    "product_shipping_price" => "50.00",
    "product_shipping_extra" => "450.00",
  ),
  array(
    "product_id" => "39",
    "product_name" => "Baldwin L Grand Piano #39",
    "product_ordered_size" => "Grand",
    "product_sales_price" => "11671.90",
    "product_shipping_price" => "1.00",
    "product_shipping_extra" => "450.00",
  )
);

foreach ( $ar AS $k => $v ) {
  $p_shipping_price[$k] = $v['product_shipping_price'];
}

array_multisort($p_shipping_price, SORT_DESC, $ar);

var_dump($ar);

?>




--- End Message ---
--- Begin Message ---
2008/8/22 Jochem Maas <[EMAIL PROTECTED]>:

> still leaves the questions as to what a DomainException and a ErrorException
> is meant to model.

A domain exception is thrown when a value is valid according to its
type, but not within the domain of the function that it is being
passed to.

For example if you had a function that only processed odd numbers,
passing it (int)6 would be a domain exception.

ErrorExceptions model genuine PHP errors - the mechanism that you
manipulate with error_reporting(). You can convert a PHP error to an
Exception with set_error_handler().

Your list brings together exceptions from all over PHP which shouldn't
really be considered together. For example, many of them are from the
SPL. They're supposed to be generic - that's what the SPL is for, it's
there to solve a specific class of problems. In that context, those
exceptions make complete sense. Other exceptions on your list are only
ever meant to be thrown from within PHP itself - they're there for us
to catch, not to throw.

Take a look at the Zend Framework as an example of best practice in
these things. I don't think that throws a single built-in exception
(although some of the SPL exceptions might be nice to use, they mostly
solve the same class of problems as assert()).

-- 

http://www.otton.org/

--- End Message ---
--- Begin Message ---
Al schreef:


Jochem Maas wrote:
here is a list of built in Exception classes, I'd figured I'd start using them,
and save on rolling my own:

Exception
ErrorException
DOMException
LogicException
BadFunctionCallException
BadMethodCallException
DomainException
InvalidArgumentException
LengthException
OutOfRangeException
RuntimeException
OutOfBoundsException
OverflowException
RangeException
UnderflowException
UnexpectedValueException
PDOException
SQLiteException
ReflectionException
mysqli_sql_exception
PharException

for starters, what is meant by DomainException? and ErrorException?

secondly if you wanted to throw an exception for a missing config file in
your app, which would you choose? if you ask me only Exception is really
correct in this instance, but it's not very descriptive is it.

the last five in the list are only handy if you use those extensions, and
then only in terms of catching.

anyone actually using these built-ins in their work?


I've been using the php5 exception try/catch feature for several months and find it most useful. e.g.,

I've been using them (and custom derivatives) since Nov2003 :-)


try
    {
$flagsArray = getHideAndMarkFlagsFromPost(); //Get the records flagged mark and hide
        $recordsArray = getRegistryTable();
        $userDataArray = $recordsArray[$flagsArray['marks'][0]];
    }
    catch (Exception $e)
    {
        $errorMsg = $e->getMessage();
    }
In my getHideAndMarkFlagsFromPost() function, I have:

if(empty($flagsArray)) throw new Exception("No records were flagged");

For your missing config file, simply use something like:
if(!file_exists('foo') throw new Exception("Config file missing");
return $file-string; // or whatever.

agreed. given that I don't feel the project requires a stack of custom
Exceptions e.g. ConfigException (which would be thrown for anything related
to unresovable application init/config errors)




--- End Message ---
--- Begin Message ---
David Otton schreef:
2008/8/22 Jochem Maas <[EMAIL PROTECTED]>:

still leaves the questions as to what a DomainException and a ErrorException
is meant to model.

A domain exception is thrown when a value is valid according to its
type, but not within the domain of the function that it is being
passed to.

For example if you had a function that only processed odd numbers,
passing it (int)6 would be a domain exception.

aha. that clears up the definition of 'Domain' for me, thank you.

ErrorExceptions model genuine PHP errors - the mechanism that you
manipulate with error_reporting(). You can convert a PHP error to an
Exception with set_error_handler().

I guessed as much, but wasn't sure ... the manual should document this as
best practice me thinks ... I have various projects that convert errors
to exceptions but I always used a custom PhpErrorException :-)

Your list brings together exceptions from all over PHP which shouldn't
really be considered together.

true, it was merely a dump from the following oneliner:

php -r 'foreach(get_declared_classes() as $c) if (stripos($c, "Exception") !== false) 
echo "$c\n";'

For example, many of them are from the
SPL.  They're supposed to be generic - that's what the SPL is for, it's
there to solve a specific class of problems.

I figured that there could/should be a few more that cover more
general web application errors. but it's probably very difficult to
come up with a set that is adequate ... in which case it's no wonder
that SPL only covers subclassed Exceptions for very specific situations

In that context, those
exceptions make complete sense. Other exceptions on your list are only
ever meant to be thrown from within PHP itself - they're there for us
to catch, not to throw.

5 of them to be exact, those were clear to me.

Take a look at the Zend Framework as an example of best practice in
these things. I don't think that throws a single built-in exception
(although some of the SPL exceptions might be nice to use, they mostly
solve the same class of problems as assert()).

can't say I've ever used assert() although I know of it. I'll take another
dive into ZF soonish to see what they're upto with regard to Exceptions.

thanks for the feedback :-)




--- End Message ---
--- Begin Message ---
Hey.

I do some part-time IT work for a voluntary paramilitary youth organisation,
and we're loooking for a system to digitize the personell files of our
members. Here's a features list, all advice on how to implement will be a
great help.

* Web-accesable via login
* Rank, Name, Phone Number, Address, Email Address, Physical Address.
* Training History
* Promotion History
* Miscellanous Notes.
* Different types of Administrator and notes that can be attached to
personell files that are seen by different types of administrator.
* Activity report page. I.e. This activity happenened on this date and these
people attended from this time to this time. Attendance must be visible on
personell files.
* Attendance Register and counter for attendances of members.
* UI that a 65-year old who believes dialup is fast (the Unit Commander) can
find his way around.
* Easily copiable and deployable. As in can be used by more than one unit.

-- 
I'm going out to find myself, if you see me here, keep me here untill I can
catch up

If I haven't said so already,

Thanks
Byron

--- End Message ---
--- Begin Message ---
At 11:13 PM +0100 8/22/08, Ashley Sheridan wrote:

Not to mention, but of the two major English speaking countries, both America and England have different address standards. All too often an American site seems to think that a postcode is the same thing as a zip code, and then rejects it in a form for being in the wrong format!

Yeah, but that's to be expected.

You Brits get everything wrong -- you drive on the wrong side of the street, you eat with your fork in the wrong hand, and your postal codes are all messed up. :-)

As Churchill once said "We are two peoples separated by a common language."

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--- End Message ---

Reply via email to