php-general Digest 16 Dec 2011 17:04:44 -0000 Issue 7612

Topics (messages 316027 through 316035):

Re: Preferred Syntax
        316027 by: Louis Huppenbauer
        316030 by: Jim Lucas
        316033 by: Tedd Sperling
        316034 by: Ross McKay

Re: Unique items in an array
        316028 by: Marc Guay
        316031 by: Jim Lucas
        316032 by: Marc Guay

Re: OOP problems
        316029 by: Fatih P.

Working on a Subsummary Report
        316035 by: Dave

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 ---
Another nice way would be sprintf. So your string really is just a string
and nothing more.
I don't know how it would affect performance, but just for the eye I find
it much simpler.

echo sprintf("<a style='text-align:left;size:**14;font-weight:bold'
href='/mypage.php/%d'>%s</a><br>", $page_id, $page_name);

2011/12/15 Robert Cummings <rob...@interjinn.com>

> On 11-12-15 02:50 AM, Ross McKay wrote:
>
>> On Wed, 14 Dec 2011 07:59:46 -0500, Rick Dwyer wrote:
>>
>>  Can someone tell me which of the following is preferred and why?
>>>
>>>  echo "<a style='text-align:left;size:**14;font-weight:bold' href='/
>>> mypage.php/$page_id'>$page_**name</a><br>";
>>>
>>>  echo "<a style='text-align:left;size:**14;font-weight:bold' href='/
>>> mypage.php/".$page_id."'>".$**page_name."</a><br>";
>>> [...]
>>>
>>
>> Just to throw in yet another possibility:
>>
>> echo<<<HTML
>> <a style="text-align:left;size:**14;font-weight:bold"
>>    href="/mypage.php/$page_id">$**page_name</a><br>
>> HTML;
>>
>> I love HEREDOC for slabs of HTML, sometimes SQL, email bodies, etc.
>> because they allow you to drop your variables into the output text
>> without crufting up the formatting with string concatenation, AND they
>> allow you to use double quotes which can be important for HTML
>> attributes that may contain single quotes.
>>
>> So whilst either above option is fine for the specific context, I prefer
>> HEREDOC when there's attributes like href.
>>
>> But what is "preferred" is rather dependent on the "preferrer".
>>
>
> Heredoc and Nowdoc are nice but I hate the way they muck up my indentation
> aesthetics. As such when I use them I use as minimalist a terminator as
> possible:
>
> <?php
>
>    echo <<<_
>        <a href="foo.html">Blah blah blah</a>
> _;
>
> ?>
>
>
> Cheers,
> Rob.
> --
> E-Mail Disclaimer: Information contained in this message and any
> attached documents is considered confidential and legally protected.
> This message is intended solely for the addressee(s). Disclosure,
> copying, and distribution are prohibited unless authorized.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
On 12/14/2011 11:50 PM, Ross McKay wrote:
> On Wed, 14 Dec 2011 07:59:46 -0500, Rick Dwyer wrote:
> 
>> Can someone tell me which of the following is preferred and why?
>>
>>  echo "<a style='text-align:left;size:14;font-weight:bold' href='/ 
>> mypage.php/$page_id'>$page_name</a><br>";
>>
>>  echo "<a style='text-align:left;size:14;font-weight:bold' href='/ 
>> mypage.php/".$page_id."'>".$page_name."</a><br>";
>> [...]
> 
> Just to throw in yet another possibility:
> 
> echo <<<HTML
> <a style="text-align:left;size:14;font-weight:bold"
>    href="/mypage.php/$page_id">$page_name</a><br>
> HTML;
> 
> I love HEREDOC for slabs of HTML, sometimes SQL, email bodies, etc.
> because they allow you to drop your variables into the output text
> without crufting up the formatting with string concatenation, AND they
> allow you to use double quotes which can be important for HTML
> attributes that may contain single quotes.
> 
> So whilst either above option is fine for the specific context, I prefer
> HEREDOC when there's attributes like href.
> 
> But what is "preferred" is rather dependent on the "preferrer".

I second this example, with one minor change, I would add '{' and '}' around
variables.

echo <<<HTML
<a style="text-align:left;size:14;font-weight:bold"
   href="/mypage.php/{$page_id}">{$page_name}</a><br>
HTML;

This works for $variables, $objects, and variable functions calls.  But doesn't
work if you try to call functions directly (bummer).

$ cat variable_usage.php
<?php

$v1 = 'Variable 1';

$o1 = new stdClass();
$o1->v1 = 'Object 1';

function something($str) {
        return "...{$str}...";
}

$f1 = "something";

echo <<<_
{$v1}
{$o1->v1}
{$f1('Function 1')}
{something('F1')}
_;

?>

Results in this

$ php -f variable_usage.php
Variable 1
Object 1
...Function 1...
{something('F1')}


This is why I like heredoc syntax over pretty much everything else.
-- 
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

C - (541) 408-5189
O - (541) 323-9113
H - (541) 323-4219

--- End Message ---
--- Begin Message ---
On Dec 14, 2011, at 12:09 PM, Peter Ford wrote:
> 
> With respect to tedd and Al, you've misread the question: the important 
> PHP-related bit is about whether to embed variables in double-quoted strings 
> or to concatenate them. These are only two of the options, and each has it's 
> pros and cons. There has been (some time ago) plenty of discussion on this 
> list about this sort of thing, including (ISTR) a timed test of the 
> performance implications of various forms - that only really matters in big 
> loops, of course...
> 
> Horses for courses. I use whatever I feel like at the time, and mix various 
> styles, as long as it's readable!
> 
> Cheers
> Pete
> 

I didn't misread the OP's post. Realize that the OP provided two ways of doing 
the task and asked which way was best?

I responded with a recommendation to remove all style elements and my 
recommended solution to his problem.

He can either consider or not.

Cheers,

tedd

_____________________
t...@sperling.com
http://sperling.com




--- End Message ---
--- Begin Message ---
Jim Lucas wrote:

>I second this example, with one minor change, I would add '{' and '}' around
>variables.
>
>echo <<<HTML
><a style="text-align:left;size:14;font-weight:bold"
>   href="/mypage.php/{$page_id}">{$page_name}</a><br>
>HTML;
>
>This works for $variables, $objects, and variable functions calls.  But doesn't
>work if you try to call functions directly (bummer).

In fact, we are in agreement here :) I was just simplifying for the
example at hand. And as for calling functions directly, also add
constants :(

However, it's easy enough to assign a constant to a variable and embed
it in a HEREDOC, and also easy to wrap a function in a method,
especially when your HEREDOC is within a method itself:

define('MSG', 'My name is');

class X {
  function html($text) {
    return htmlspecialchars($text);
  }

  function output($name) {
    $msg = MSG;
    echo <<<HTML
<p>$msg {$this->html($name)}</p>
HTML;
  }
}

$x = new X();
$x->output('silly "rockstar" name like <&>');

>[...]
>This is why I like heredoc syntax over pretty much everything else.

Concur!
-- 
Ross McKay, Toronto, NSW Australia
"Pay no attention to that man behind the curtain" - Wizard of Oz

--- End Message ---
--- Begin Message ---
> Assuming you want to make things unique based on the "contact_first_name" 
> field,
> how would you decide which record to keep?  The first one you run in to, the
> last one you come across, or some other criteria?

The unique field is actually the contact_id.

Marc

--- End Message ---
--- Begin Message ---
On 12/15/2011 6:24 AM, Marc Guay wrote:
>> Assuming you want to make things unique based on the "contact_first_name" 
>> field,
>> how would you decide which record to keep?  The first one you run in to, the
>> last one you come across, or some other criteria?
> 
> The unique field is actually the contact_id.
> 
> Marc
> 

Marc,

If that is the case, can you explain to us how you got the two entries in the
array to begin with?  If this is from a DB query, then it should probably be
address else where.  If it was loaded from a text file, it should be handled
differently.

No matter how it was created, you could always build a method (if the dataset
isn't too large) that would filter things out.

Given your example array, I would do something like this:

<?php

$oldDataSet = array(
  array(
    "contact_id" => "356",
    "contact_first_name" => "Marc",
  ),
  array(
    "contact_id" => "247",
    "contact_first_name" => "Marc",
  ),
  array(
    "contact_id" => "356",
    "contact_first_name" => "Marc",
  ),
);

$newDataSet = array();

foreach ( $oldDataSet AS $k => $entry ) {
  $newDataSet[$entry['contact_id']] = $entry;
  unset($oldDataSet[$k]);
}

print_r($newDataSet);

?>

This would result in something like this:

Array
(
    [356] => Array
        (
            [contact_id] => 356
            [contact_first_name] => Marc
        )

    [247] => Array
        (
            [contact_id] => 247
            [contact_first_name] => Marc
        )

)

Give it a try, should do what you are wanting.

-- 
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

--- End Message ---
--- Begin Message ---
> Give it a try, should do what you are wanting.

Hi Jim,

I appreciate your dedication to this problem but it was solved 2 days ago!  :)

Thanks
Marc

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

On 12/15/2011 01:05 PM, Alex Pojarsky wrote:
I'm not sure I've understood you correctly, but you may try something
like the following primitive autoloader (I didn't debug it, it's just
an example):

class Base
{
     protected $_path = '';

     public function construct($base_path)
     {
         $this->_path = $base_path;
     }
     public function __get($name)
     {
         $requested_path = $this->_path . DIRECTORY_SEPARATOR . $name;
         if (is_dir($requested_path))
         {
             return new Base($requested_path);
         }
         else if (is_file($requested_path . '.php'))
         {
             include ($requested_path . '.php');
             $classname = ucfirst($name);
             return new $clasname();
         }
     }
}

// Assuming you have Mysql class in /home/user/project/classes/db/mysql.php
// you may try

$base = new Base("/home/user/project/classes/");
$base->db->mysql->someFunctionOfMysqlClass();

2011/12/15 Dominik Halvoník<dominik.halvo...@gmail.com>:
Hello,

I would like to ask you for help. This days I am trying to build one of my
applications. But I have problem which stopped me. I have folder whit php
files like connect.php, delete.php etc. These files contains classes named
the same as files. So in file connect.php is class Connect. These files are
placed in folder named mysql and this folder is inside folder named db. In
folder db is a php file named mysql.php, in this file I include classes
from folder mysql, after include I declare class MySQL and in it I have
method __construct(). In this method I create dynamic objects from included
classes. And this is the problem that I can not solve, I have more then one
of this files(mysql.php[whit class MySQL], oracle.php[whit class Oracle]
etc.) and I need to include them to file called db.php that is in the main
folder of my app. In db.php is an class called db, how can I add classes
MySQL, Oracle etc. to class db? I try to use abstract class whit __set and
__get methods but I also need to include class db to main class
application. I am really sorry for my English, so please be indulgent. So I
need to connect classes like this:

application->db->mysql->connect, but I can not use extends because in php
you can have only one parent class. The reason why I am trying to do
something like this is because I want to call methods like this:
$test = new application();
$test->db->connect();

If it is mysql or othet database I set in config.php file.

I need to achieve this schema( ->  is something like ../ it means that it is
one level up folder):

connec.php(class Connect MySql)->
select.php(class Select MySql) ->
.... ->  mysql.php(class MySQL include all classes, Connect...)->
.... ->
... ->
->  db.php(class db include all classes, MySQL, Oracle..)
connec.php(class Connect Oracle)->
select.php(class Select Oracle ) ->
.... ->  oracle .php(class Oracle include all classes, Connect...)->
.... ->
... ->

download.php(class Download)->
unzip.php(class Unzip) ->
.... ->  files.php(class Files include all classes, Download...) ->
file.php(class file include class Files)
.... ->
... ->

hash.php(class Hash)->
capcha.php(class Capcha) ->
.... ->  secure.php(class Secure include all classes, Hash...) ->
security.php(class security include class Secure)
.... ->
... ->
ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect.

And in the end, in the same folder as db.php and security.php I will have
file application.php which will contain class application and in its
__construct() method I will make link classes db, security, file ect. ect.
So I will just include file application.php make object from class
application and then just do $object->db->connect()(of course if it will by
MySql or other database will be stored in some config.php file).

Thanks,

Dominik
Why don't you modify include_path on initialization of 'Base' class?
would make things much simpler.

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

I need to create a year to date report with individual  Subsummary
Report (ala filemaker / others?) headings for each month. So, I’m
curious the best way to approach this for performance speed and
flexibility etc.

-  I can do 1 sql query for the whole year’s data - or 12 individual
queries... probably best to do 1 big year sql query - but I’m not sure
how to split things up with loops etc.

each summary month heading will need to show:
- total month count of records
- special counts within each month
- more stuff
- also the record data

like:

JAN - total record count = 50 special count = 3

data record 1
data record 2
data record 3

FEB - total record count = 47 special count = 4

data record 1
data record 2
data record 3
etc.


Q: is it better to do 1 year find or 12 month queries - is 1 big find faster?

Q: with one year find - how do I setup 12 loops to simulate summaries?
do i just loop through the full set and only display if record is in
month 1 or 2 or whatever?

If there are any links describing this process - that would be great

any help would be appreciated...


-- 
Thanks - Dave

--- End Message ---

Reply via email to