php-general Digest 27 Feb 2013 12:27:47 -0000 Issue 8139

Topics (messages 320301 through 320314):

Re: Stupid question
        320301 by: Daniel Brown
        320302 by: Ashley Sheridan
        320303 by: Curtis Maurand
        320304 by: Serge Fonville
        320305 by: Jim Lucas
        320310 by: tamouse mailing lists
        320313 by: Sebastian Krebs
        320314 by: Jim Giner

Re: Cold Intermediate Programmer trying to modularize website...
        320306 by: Barry Smith
        320309 by: tamouse mailing lists

Lifetime
        320307 by: Brian Smither
        320308 by: shiplu

Re: Arrays
        320311 by: tamouse mailing lists
        320312 by: Karl DeSaulniers

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 ---
On Tue, Feb 26, 2013 at 4:27 PM, Curtis Maurand <cur...@maurand.com> wrote:
> I have the following:
>
> $dsn = "mysqli://$username:$password@$hostname2/$database";
> $options = array(
>     'debug' => 3,
>     'result_buffering' => false,
>   );
>   $dbh =& MDB2::factory($dsn, $options);
>         if (PEAR::isError($mdb2))
>         {
>                 die($mdb2->getMessage());
>         }
>
>
>
>
> function tallyCart($_u_id,$dbh){
>        while($row = $result->fetchrow(MDB2_FETCHMODE_ASSOC)) {
>                 $_showCheckOut=1;
>                 $_pdetail=new ProductDetail($row{'product_ID'},
> $row{'product_Quantity'}, $_u_id);
>                  $_getSubTotal += $_pdetail->_subTotal;
>                  $_counter++;
>         }
> }
>
> I'm getting:  Call to undefined method MDB2_Error::fetchrow()
>
> anyone have any ideas?  Can I not pass a database handle to a function?
>
> Thanks,
> Curtis

    Hate to answer a question with a question, but:

        1.) Do you have the PEAR package MDB2 installed?
        2.) Where is $result defined?  I don't see it in your code snippet here.

-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

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

Daniel Brown <danbr...@php.net> wrote:

>On Tue, Feb 26, 2013 at 4:27 PM, Curtis Maurand <cur...@maurand.com>
>wrote:
>> I have the following:
>>
>> $dsn = "mysqli://$username:$password@$hostname2/$database";
>> $options = array(
>>     'debug' => 3,
>>     'result_buffering' => false,
>>   );
>>   $dbh =& MDB2::factory($dsn, $options);
>>         if (PEAR::isError($mdb2))
>>         {
>>                 die($mdb2->getMessage());
>>         }
>>
>>
>>
>>
>> function tallyCart($_u_id,$dbh){
>>        while($row = $result->fetchrow(MDB2_FETCHMODE_ASSOC)) {
>>                 $_showCheckOut=1;
>>                 $_pdetail=new ProductDetail($row{'product_ID'},
>> $row{'product_Quantity'}, $_u_id);
>>                  $_getSubTotal += $_pdetail->_subTotal;
>>                  $_counter++;
>>         }
>> }
>>
>> I'm getting:  Call to undefined method MDB2_Error::fetchrow()
>>
>> anyone have any ideas?  Can I not pass a database handle to a
>function?
>>
>> Thanks,
>> Curtis
>
>    Hate to answer a question with a question, but:
>
>        1.) Do you have the PEAR package MDB2 installed?
>2.) Where is $result defined?  I don't see it in your code snippet
>here.

Also, there is no such thing as a stupid question, only a stupid answer...

Thanks,
Ash
http://www.ashleysheridan.co.uk

--- End Message ---
--- Begin Message ---
On 2/26/2013 4:33 PM, Daniel Brown wrote:
On Tue, Feb 26, 2013 at 4:27 PM, Curtis Maurand <cur...@maurand.com> wrote:
I have the following:

$dsn = "mysqli://$username:$password@$hostname2/$database";
$options = array(
     'debug' => 3,
     'result_buffering' => false,
   );
   $dbh =& MDB2::factory($dsn, $options);
         if (PEAR::isError($mdb2))
         {
                 die($mdb2->getMessage());
         }




function tallyCart($_u_id,$dbh){
        while($row = $result->fetchrow(MDB2_FETCHMODE_ASSOC)) {
                 $_showCheckOut=1;
                 $_pdetail=new ProductDetail($row{'product_ID'},
$row{'product_Quantity'}, $_u_id);
                  $_getSubTotal += $_pdetail->_subTotal;
                  $_counter++;
         }
}

I'm getting:  Call to undefined method MDB2_Error::fetchrow()

anyone have any ideas?  Can I not pass a database handle to a function?

Thanks,
Curtis
     Hate to answer a question with a question, but:

         1.) Do you have the PEAR package MDB2 installed?
         2.) Where is $result defined?  I don't see it in your code snippet 
here.

Sorry,

        $myquery  = "SELECT * from tbl_Cart where u_ID='$_u_id'";
        echo $myquery;
        $result =& $dbh->query($myquery);

I then tried setting the buffering to true and did a if($result->numrows() >0) and wrapped it around the entire fetchrow loop and I still get the same thing.

I just took a look and the libraries are installed if not a bit outdated, but they are there.


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

I'm getting:  Call to undefined method MDB2_Error::fetchrow()

Have your checked if $dbh->query() throws an error?
It seems $result is a different type than expected.

HTH

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table


2013/2/26 Curtis Maurand <cur...@maurand.com>

> On 2/26/2013 4:33 PM, Daniel Brown wrote:
>
>> On Tue, Feb 26, 2013 at 4:27 PM, Curtis Maurand <cur...@maurand.com>
>> wrote:
>>
>>> I have the following:
>>>
>>> $dsn = "mysqli://$username:$password@**$hostname2/$database";
>>> $options = array(
>>>      'debug' => 3,
>>>      'result_buffering' => false,
>>>    );
>>>    $dbh =& MDB2::factory($dsn, $options);
>>>          if (PEAR::isError($mdb2))
>>>          {
>>>                  die($mdb2->getMessage());
>>>          }
>>>
>>>
>>>
>>>
>>> function tallyCart($_u_id,$dbh){
>>>         while($row = $result->fetchrow(MDB2_**FETCHMODE_ASSOC)) {
>>>                  $_showCheckOut=1;
>>>                  $_pdetail=new ProductDetail($row{'product_**ID'},
>>> $row{'product_Quantity'}, $_u_id);
>>>                   $_getSubTotal += $_pdetail->_subTotal;
>>>                   $_counter++;
>>>          }
>>> }
>>>
>>> I'm getting:  Call to undefined method MDB2_Error::fetchrow()
>>>
>>> anyone have any ideas?  Can I not pass a database handle to a function?
>>>
>>> Thanks,
>>> Curtis
>>>
>>      Hate to answer a question with a question, but:
>>
>>          1.) Do you have the PEAR package MDB2 installed?
>>          2.) Where is $result defined?  I don't see it in your code
>> snippet here.
>>
>>  Sorry,
>
>         $myquery  = "SELECT * from tbl_Cart where u_ID='$_u_id'";
>         echo $myquery;
>         $result =& $dbh->query($myquery);
>
> I then tried setting the buffering to true and did a if($result->numrows()
> >0) and wrapped it around the entire fetchrow loop and I still get the same
> thing.
>
> I just took a look and the libraries are installed if not a bit outdated,
> but they are there.
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
On 02/26/2013 01:27 PM, Curtis Maurand wrote:
I have the following:

$dsn = "mysqli://$username:$password@$hostname2/$database";
$options = array(
'debug' => 3,
'result_buffering' => false,
);
$dbh =& MDB2::factory($dsn, $options);
if (PEAR::isError($mdb2))
{
die($mdb2->getMessage());
}




function tallyCart($_u_id,$dbh){
while($row = $result->fetchrow(MDB2_FETCHMODE_ASSOC)) {

Talking in code.  The above two lines tell me...

$dbh != $result

isset($result) === false


$_showCheckOut=1;
$_pdetail=new ProductDetail($row{'product_ID'},
$row{'product_Quantity'}, $_u_id);
$_getSubTotal += $_pdetail->_subTotal;
$_counter++;
}
}

I'm getting: Call to undefined method MDB2_Error::fetchrow()

anyone have any ideas? Can I not pass a database handle to a function?

Thanks,
Curtis




--
Jim Lucas

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

--- End Message ---
--- Begin Message ---
On Tue, Feb 26, 2013 at 4:41 PM, Jim Lucas <li...@cmsws.com> wrote:
> On 02/26/2013 01:27 PM, Curtis Maurand wrote:
>>
>> I have the following:
>>
>> $dsn = "mysqli://$username:$password@$hostname2/$database";
>> $options = array(
>> 'debug' => 3,
>> 'result_buffering' => false,
>> );
>> $dbh =& MDB2::factory($dsn, $options);
>> if (PEAR::isError($mdb2))
>> {
>> die($mdb2->getMessage());
>> }
>>
>>
>>
>>
>> function tallyCart($_u_id,$dbh){
>> while($row = $result->fetchrow(MDB2_FETCHMODE_ASSOC)) {
>
>
> Talking in code.  The above two lines tell me...
>
> $dbh != $result
>
> isset($result) === false
>
>
>
>> $_showCheckOut=1;
>> $_pdetail=new ProductDetail($row{'product_ID'},
>> $row{'product_Quantity'}, $_u_id);
>> $_getSubTotal += $_pdetail->_subTotal;
>> $_counter++;
>> }
>> }
>>
>> I'm getting: Call to undefined method MDB2_Error::fetchrow()
>>
>> anyone have any ideas? Can I not pass a database handle to a function?
>>
>> Thanks,
>> Curtis
>>
>>
>
>
> --
> Jim Lucas
>
> http://www.cmsws.com/
> http://www.cmsws.com/examples/
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Well, *I* have a stupid question: is $lhv =& expr the same as $lhv = &expr ??

--- End Message ---
--- Begin Message ---
2013/2/27 tamouse mailing lists <tamouse.li...@gmail.com>

> On Tue, Feb 26, 2013 at 4:41 PM, Jim Lucas <li...@cmsws.com> wrote:
> > On 02/26/2013 01:27 PM, Curtis Maurand wrote:
> >>
> >> I have the following:
> >>
> >> $dsn = "mysqli://$username:$password@$hostname2/$database";
> >> $options = array(
> >> 'debug' => 3,
> >> 'result_buffering' => false,
> >> );
> >> $dbh =& MDB2::factory($dsn, $options);
> >> if (PEAR::isError($mdb2))
> >> {
> >> die($mdb2->getMessage());
> >> }
> >>
> >>
> >>
> >>
> >> function tallyCart($_u_id,$dbh){
> >> while($row = $result->fetchrow(MDB2_FETCHMODE_ASSOC)) {
> >
> >
> > Talking in code.  The above two lines tell me...
> >
> > $dbh != $result
> >
> > isset($result) === false
> >
> >
> >
> >> $_showCheckOut=1;
> >> $_pdetail=new ProductDetail($row{'product_ID'},
> >> $row{'product_Quantity'}, $_u_id);
> >> $_getSubTotal += $_pdetail->_subTotal;
> >> $_counter++;
> >> }
> >> }
> >>
> >> I'm getting: Call to undefined method MDB2_Error::fetchrow()
> >>
> >> anyone have any ideas? Can I not pass a database handle to a function?
> >>
> >> Thanks,
> >> Curtis
> >>
> >>
> >
> >
> > --
> > Jim Lucas
> >
> > http://www.cmsws.com/
> > http://www.cmsws.com/examples/
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
> Well, *I* have a stupid question: is $lhv =& expr the same as $lhv = &expr
> ??
>

Yes :) Because an operator "=&" doesn't exists, thus the lexer will split
them into the tokens "= &", or "= WHITESPACE &" respectively. The parser
again ignores whitespaces.


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


-- 
github.com/KingCrunch

--- End Message ---
--- Begin Message ---
On 2/26/2013 4:27 PM, Curtis Maurand wrote:
I have the following:

$dsn = "mysqli://$username:$password@$hostname2/$database";
$options = array(
     'debug' => 3,
     'result_buffering' => false,
   );
   $dbh =& MDB2::factory($dsn, $options);
         if (PEAR::isError($mdb2))
         {
                 die($mdb2->getMessage());
         }




function tallyCart($_u_id,$dbh){
        while($row = $result->fetchrow(MDB2_FETCHMODE_ASSOC)) {
                 $_showCheckOut=1;
                 $_pdetail=new ProductDetail($row{'product_ID'},
$row{'product_Quantity'}, $_u_id);
                  $_getSubTotal += $_pdetail->_subTotal;
                  $_counter++;
         }
}

I'm getting:  Call to undefined method MDB2_Error::fetchrow()

anyone have any ideas?  Can I not pass a database handle to a function?

Thanks,
Curtis


This may be that stupid answer, but I see what appears to be two problems.

1 - $result is not declared globally in your function header, so hence it's undefined, hence all of its methods are.

2 - you have a couple indices wrapped in curly braces, not parens. Is that some new kind of syntax I'm not aware of?


--- End Message ---
--- Begin Message ---
Yes, Jim, and "tamouse",

Apologies for the length of this response.

I am asking for help organizing the rewrite of the site, from a LOT of
HTMLs that have 90% in common, containing a couple of php links to do a php
mail form and command, and an article... to a "template-article-footer"
format.

I am having a problem visualizing how I need to proceed (from the highest
level), so I was asking for opinions on how to organize the index.php plus
template.php plus home.article design... at the highest level.

>From the sounds of it ( I didn't bother to un-zip his file) it appears that
> he just wants to create a model template and fill in the center (the
> 'article'?) appropriately depending upon the user's request


That is basically the project.

I have used Joomla for sites like this before, but the business owner is
not tech-savvy (not a power-user) in the least.

Setting up Joomla for her site, teaching her how to navigate Joomla to
modify articles, and giving her the access to the articles that she wants
access to is WAY beyond what she is capable of and REALLY overkill for this
small website. Besides, she could destroy the whole site as Joomla admin or
a Joomla user very easily.

She wants access to some of the articles, ONLY to change the wording of
some articles (the article on the about page and the home page, and maybe a
"specials" page yet to be designed) but I am trying not to giving her the
ability to destroy the whole site because she has access to.  I want to
place .article files in a folder, and let her work on the articles.  I can
build a default article into the PHP if the document that she edits gets
deleted.  Yet she's a smart business owner... "if i have access to the
articles to change them and I mess up, I don't want the website to
disappear" was how she expressed it to me.

The other wrinkle (at a programming level) is that she wants a header in
every email that the website sends to the company, to track which link was
clicked.

I'm thinking that (as an example of the changes that I need to make to the
files contained in the zip )--

I. index.html would "require" index.php.
1) index.php would define variables for that page, i.e. a var
building="index" or "about" or "services" or "products", and then include
"template.php", a home.article file, and a footer.php .
2) template.php would use the "building" value to build each custom header
and left column and right column. Within the header, it would use the
variable "building" to send a label that identifies "building" and another
value "header" to "contact.php" (see more about "contact.php" later).
Programatically, template.php will highlight tab buttons at the top of the
page (if they need to be highlighted) and menu buttons in left column (if
they need to be highlighted) dependent on the value of "building", and in
column 2 choose the correct list of buttons to display in a vendor list
dependent on the value of "building".
3) footer.php would use the "building" value to build each custom footer
for the page.  Within the footer, it would use the variable "building" to
send a label that identifies "building" and another value "footer" to
"contact.php" (see more about "contact.php" later).

II. contact.php will bring up an email form, use the label passed in to
create a custom header to insert into the php mail command/function.

III/IV/V/VI. Each HTML in the site will have an associated php that calls
template, grabs any appropriate .article file, calls footer.

Does that structure make sense?

Are there gotchas, things to "verify" and "watch out for" that you have run
into doing similar projects?

As a past for this design procedure, I worked on a "sales agent" site years
ago that did a similar "build each page dependent on logged.in.user" ...
but the control for this project is not logged.in.user in this project...
the control is page.being.built .

Thank you for the input, and any links to online articles that I should
probably read, or topics that I should make sure I understand completely in
php.

Peace,
--
Barry Smith
c 704-497-4217
e scs.bns...@gmail.com

--- End Message ---
--- Begin Message ---
Have you considered setting this up in wordpress?

On Tue, Feb 26, 2013 at 5:14 PM, Barry Smith <scs.bns...@gmail.com> wrote:
> Yes, Jim, and "tamouse",
>
> Apologies for the length of this response.
>
> I am asking for help organizing the rewrite of the site, from a LOT of
> HTMLs that have 90% in common, containing a couple of php links to do a php
> mail form and command, and an article... to a "template-article-footer"
> format.
>
> I am having a problem visualizing how I need to proceed (from the highest
> level), so I was asking for opinions on how to organize the index.php plus
> template.php plus home.article design... at the highest level.
>
> From the sounds of it ( I didn't bother to un-zip his file) it appears that
>> he just wants to create a model template and fill in the center (the
>> 'article'?) appropriately depending upon the user's request
>
>
> That is basically the project.
>
> I have used Joomla for sites like this before, but the business owner is
> not tech-savvy (not a power-user) in the least.
>
> Setting up Joomla for her site, teaching her how to navigate Joomla to
> modify articles, and giving her the access to the articles that she wants
> access to is WAY beyond what she is capable of and REALLY overkill for this
> small website. Besides, she could destroy the whole site as Joomla admin or
> a Joomla user very easily.
>
> She wants access to some of the articles, ONLY to change the wording of
> some articles (the article on the about page and the home page, and maybe a
> "specials" page yet to be designed) but I am trying not to giving her the
> ability to destroy the whole site because she has access to.  I want to
> place .article files in a folder, and let her work on the articles.  I can
> build a default article into the PHP if the document that she edits gets
> deleted.  Yet she's a smart business owner... "if i have access to the
> articles to change them and I mess up, I don't want the website to
> disappear" was how she expressed it to me.
>
> The other wrinkle (at a programming level) is that she wants a header in
> every email that the website sends to the company, to track which link was
> clicked.
>
> I'm thinking that (as an example of the changes that I need to make to the
> files contained in the zip )--
>
> I. index.html would "require" index.php.
> 1) index.php would define variables for that page, i.e. a var
> building="index" or "about" or "services" or "products", and then include
> "template.php", a home.article file, and a footer.php .
> 2) template.php would use the "building" value to build each custom header
> and left column and right column. Within the header, it would use the
> variable "building" to send a label that identifies "building" and another
> value "header" to "contact.php" (see more about "contact.php" later).
> Programatically, template.php will highlight tab buttons at the top of the
> page (if they need to be highlighted) and menu buttons in left column (if
> they need to be highlighted) dependent on the value of "building", and in
> column 2 choose the correct list of buttons to display in a vendor list
> dependent on the value of "building".
> 3) footer.php would use the "building" value to build each custom footer
> for the page.  Within the footer, it would use the variable "building" to
> send a label that identifies "building" and another value "footer" to
> "contact.php" (see more about "contact.php" later).
>
> II. contact.php will bring up an email form, use the label passed in to
> create a custom header to insert into the php mail command/function.
>
> III/IV/V/VI. Each HTML in the site will have an associated php that calls
> template, grabs any appropriate .article file, calls footer.
>
> Does that structure make sense?
>
> Are there gotchas, things to "verify" and "watch out for" that you have run
> into doing similar projects?
>
> As a past for this design procedure, I worked on a "sales agent" site years
> ago that did a similar "build each page dependent on logged.in.user" ...
> but the control for this project is not logged.in.user in this project...
> the control is page.being.built .
>
> Thank you for the input, and any links to online articles that I should
> probably read, or topics that I should make sure I understand completely in
> php.
>
> Peace,
> --
> Barry Smith
> c 704-497-4217
> e scs.bns...@gmail.com

--- End Message ---
--- Begin Message ---
I'm curious to know what the lifetime of a constant is.

I know that sounds like a stupid question, but the context is this:

I have a file I am writing to up to the very last possible micro-second. As 
such, I know that as PHP is destroying itself after having executed the last 
statement in the program, things begin to disappear. The Class objects destruct 
in no discernable order (they have destructors). Any open file handles close. 
Etc.

My question is: During all this destruction, at what point must I no longer 
expect to have a constant available? That is, when the classes are executing 
their __destruct() functions, et al, I want to manage something with those 
destructors.

So when does the constant actually-factually disappear?



--- End Message ---
--- Begin Message ---
Constants are available as long as the PHP process is executing.
Destructors are called before a class instance is being wiped out from
memory. But it does not remove a class or undefine a class. It works with
instance.  So a class constant never gets undefined. It stays with class
definition.

--- End Message ---
--- Begin Message ---
On Mon, Feb 25, 2013 at 9:51 PM, Karl DeSaulniers <k...@designdrumm.com> wrote:
> Never mind. I found a different function that reads out the children as well
> into the array.
>
>
>                 function xml_parse_into_assoc($data) {
>                           $p = xml_parser_create();
>
>                           xml_parser_set_option($p, XML_OPTION_CASE_FOLDING,
> 0);
>                           xml_parser_set_option($p, XML_OPTION_SKIP_WHITE,
> 1);
>
>                           xml_parse_into_struct($p, $data, $vals, $index);
>                           xml_parser_free($p);
>
>                           $levels = array(null);
>
>                           foreach ($vals as $val) {
>                             if ($val['type'] == 'open' || $val['type'] ==
> 'complete') {
>                               if (!array_key_exists($val['level'], $levels))
> {
>                                 $levels[$val['level']] = array();
>                               }
>                             }
>
>                             $prevLevel =& $levels[$val['level'] - 1];
>                             $parent = $prevLevel[sizeof($prevLevel)-1];
>
>                             if ($val['type'] == 'open') {
>                               $val['children'] = array();
>                               array_push(&$levels[$val['level']], $val);
>                               continue;
>                             }
>
>                             else if ($val['type'] == 'complete') {
>                               $parent['children'][$val['tag']] =
> $val['value'];
>                             }
>
>                             else if ($val['type'] == 'close') {
>                               $pop = array_pop($levels[$val['level']]);
>                               $tag = $pop['tag'];
>
>                               if ($parent) {
>                                 if (!array_key_exists($tag,
> $parent['children'])) {
>                                   $parent['children'][$tag] =
> $pop['children'];
>                                 }
>                                 else if
> (is_array($parent['children'][$tag])) {
>                                     if(!isset($parent['children'][$tag][0]))
> {
>                                         $oldSingle =
> $parent['children'][$tag];
>                                         $parent['children'][$tag] = null;
>                                         $parent['children'][$tag][] =
> $oldSingle;
>
>                                     }
>                                       $parent['children'][$tag][] =
> $pop['children'];
>                                 }
>                               }
>                               else {
>                                 return(array($pop['tag'] =>
> $pop['children']));
>                               }
>                             }
>
>                             $prevLevel[sizeof($prevLevel)-1] = $parent;
>                           }
>                 }
>
>
>                         $params = xml_parse_into_assoc($result);//$result =
> xml result from USPS api
>
> Original function by: jemptymethod at gmail dot com
> Duplicate names fix by: Anonymous (comment right above original function)
>
> Best,
> Karl
>
>
>
> On Feb 25, 2013, at 7:50 PM, Jim Lucas wrote:
>
>> On 02/25/2013 05:40 PM, Karl DeSaulniers wrote:
>>>
>>> Hi Guys/Gals,
>>> If I have an multidimensional array and it has items that have the same
>>> name in it, how do I get the values of each similar item?
>>>
>>> EG:
>>>
>>> specialservices => array(
>>> specialservice => array(
>>> serviceid => 1,
>>> servicename=> signature required,
>>> price => $4.95
>>> ),
>>> secialservice => array(
>>> serviceid => 15,
>>> servicename => return receipt,
>>> price => $2.30
>>> )
>>> )
>>>
>>> How do I get the prices for each? What would be the best way to do this?
>>> Can I utilize the serviceid to do this somehow?
>>> It is always going to be different per specialservice.
>>>
>>> TIA,
>>>
>>> Best,
>>>
>>> Karl DeSaulniers
>>> Design Drumm
>>> http://designdrumm.com
>>>
>>>
>>
>> This will never work.  Your last array will always overwrite your previous
>> array.
>>
>> Here is how I would suggest building it:
>>
>>
>> $items = array(
>>    1 => array(
>>        serviceid => 1,
>>        servicename=> signature required,
>>        price => $4.95
>>    ),
>>    15 => array(
>>        serviceid => 15,
>>        servicename => return receipt,
>>        price => $2.30
>>    )
>> )
>>
>> This will ensure that your first level indexes never overwrite themselves.
>>
>> But, with that change made, then do this:
>>
>> foreach ( $items AS $item ) {
>>  if ( array_key_exists('price', $item) ) {
>>    echo $item['price'];
>>  } else {
>>    echo 'Item does not have a price set';
>>  }
>> }
>>
>> Resources:
>> http://php.net/foreach
>> http://php.net/array_key_exists
>>
>> --
>> Jim Lucas
>>
>> http://www.cmsws.com/
>> http://www.cmsws.com/examples/
>
>
> Karl DeSaulniers
> Design Drumm
> http://designdrumm.com
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Would this work for you?
http://us.php.net/manual/en/function.xml-parse-into-struct.php

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

On Feb 26, 2013, at 10:35 PM, tamouse mailing lists wrote:

On Mon, Feb 25, 2013 at 9:51 PM, Karl DeSaulniers <k...@designdrumm.com > wrote:
Never mind. I found a different function that reads out the children as well
into the array.


               function xml_parse_into_assoc($data) {
                         $p = xml_parser_create();

xml_parser_set_option($p, XML_OPTION_CASE_FOLDING,
0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE,
1);

xml_parse_into_struct($p, $data, $vals, $index);
                         xml_parser_free($p);

                         $levels = array(null);

                         foreach ($vals as $val) {
if ($val['type'] == 'open' || $val['type'] ==
'complete') {
if (!array_key_exists($val['level'], $levels))
{
                               $levels[$val['level']] = array();
                             }
                           }

                           $prevLevel =& $levels[$val['level'] - 1];
$parent = $prevLevel[sizeof($prevLevel)-1];

                           if ($val['type'] == 'open') {
                             $val['children'] = array();
array_push(&$levels[$val['level']], $val);
                             continue;
                           }

                           else if ($val['type'] == 'complete') {
                             $parent['children'][$val['tag']] =
$val['value'];
                           }

                           else if ($val['type'] == 'close') {
$pop = array_pop($levels[$val['level']]);
                             $tag = $pop['tag'];

                             if ($parent) {
                               if (!array_key_exists($tag,
$parent['children'])) {
                                 $parent['children'][$tag] =
$pop['children'];
                               }
                               else if
(is_array($parent['children'][$tag])) {
if(!isset($parent['children'] [$tag][0]))
{
                                       $oldSingle =
$parent['children'][$tag];
$parent['children'][$tag] = null;
                                       $parent['children'][$tag][] =
$oldSingle;

                                   }
                                     $parent['children'][$tag][] =
$pop['children'];
                               }
                             }
                             else {
                               return(array($pop['tag'] =>
$pop['children']));
                             }
                           }

$prevLevel[sizeof($prevLevel)-1] = $parent;
                         }
               }


$params = xml_parse_into_assoc($result);// $result =
xml result from USPS api

Original function by: jemptymethod at gmail dot com
Duplicate names fix by: Anonymous (comment right above original function)

Best,
Karl



On Feb 25, 2013, at 7:50 PM, Jim Lucas wrote:

On 02/25/2013 05:40 PM, Karl DeSaulniers wrote:

Hi Guys/Gals,
If I have an multidimensional array and it has items that have the same
name in it, how do I get the values of each similar item?

EG:

specialservices => array(
specialservice => array(
serviceid => 1,
servicename=> signature required,
price => $4.95
),
secialservice => array(
serviceid => 15,
servicename => return receipt,
price => $2.30
)
)

How do I get the prices for each? What would be the best way to do this?
Can I utilize the serviceid to do this somehow?
It is always going to be different per specialservice.

TIA,

Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com



This will never work. Your last array will always overwrite your previous
array.

Here is how I would suggest building it:


$items = array(
  1 => array(
      serviceid => 1,
      servicename=> signature required,
      price => $4.95
  ),
  15 => array(
      serviceid => 15,
      servicename => return receipt,
      price => $2.30
  )
)

This will ensure that your first level indexes never overwrite themselves.

But, with that change made, then do this:

foreach ( $items AS $item ) {
if ( array_key_exists('price', $item) ) {
  echo $item['price'];
} else {
  echo 'Item does not have a price set';
}
}

Resources:
http://php.net/foreach
http://php.net/array_key_exists

--
Jim Lucas

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


Karl DeSaulniers
Design Drumm
http://designdrumm.com


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


Would this work for you?
http://us.php.net/manual/en/function.xml-parse-into-struct.php


That is where I got this function. :)
Comment 12 and 13 on that page.
Yes it worked for me.

Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com


--- End Message ---

Reply via email to