php-general Digest 13 Mar 2013 09:35:16 -0000 Issue 8160

Topics (messages 320496 through 320505):

Mystery foreach error
        320496 by: Angela Barone
        320497 by: Jim Giner
        320499 by: Marco Behnke
        320500 by: Angela Barone
        320501 by: Jim Giner
        320502 by: Angela Barone
        320503 by: David Robley
        320504 by: Angela Barone

Re: XML to Array
        320498 by: Bastien

mysql custom global defined variable
        320505 by: Kevin Peterson

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 ---
        I've been getting the following error for awhile now, but I can't 
figure out why it's happening:

Invalid argument supplied for foreach() in ... sample.php on line 377

        Here's that portion of code:

include("states_zipcodes.php");

// Check if Zip Code matches from states_zipcodes
$zip_short = substr($zip, 0, 3);
foreach ($states[$state] as &$zip_prefix) {   // <-- line 377
    if ($zip_prefix == $zip_short) {
        break;
    } else {
        $match = 'no';
    }
}

        It doesn't happen all the time, so I'm thinking that some spambot is 
populating the HTML form with something the script doesn't like(?).  However, I 
tested that myself by entering text, or by entering just 2 digits, but there 
was no error.  FYI, I do have code in the script that catches faulty input and 
warns people in their browser to go back and re-enter the correct data, so I'm 
at a loss as to why this is happening.

        How can I see what's triggering this to happen?  I have the following 
line in my php.ini:

error_reporting = E_ALL & E_STRICT & E_NOTICE & E_DEPRECATED

Thank you,
Angela

--- End Message ---
--- Begin Message ---
On 3/12/2013 3:45 PM, Angela Barone wrote:
$zip_short = substr($zip, 0, 3);
foreach ($states[$state] as &$zip_prefix) {   // <-- line 377
     if ($zip_prefix == $zip_short) {
         break;
     } else {
         $match = 'no';
     }
}

I see the & in the foreach. Since you are not modifying anything in the array, why are you using that?
Also - I'm assuming that $states is a multi-dimensional array?

--- End Message ---
--- Begin Message ---
Am 12.03.13 20:45, schrieb Angela Barone:
>       I've been getting the following error for awhile now, but I can't 
> figure out why it's happening:
>
> Invalid argument supplied for foreach() in ... sample.php on line 377
>
>       Here's that portion of code:
>
> include("states_zipcodes.php");
>
> // Check if Zip Code matches from states_zipcodes
> $zip_short = substr($zip, 0, 3);
> foreach ($states[$state] as &$zip_prefix) {   // <-- line 377

what is in $states?
Looks like $states[$state] is not an array.

And don't use & reference operator in foreach loop, there can be strange
side effects if you don't act carefully with it.

>     if ($zip_prefix == $zip_short) {
>         break;
>     } else {
>         $match = 'no';
>     }
> }
>
>       It doesn't happen all the time, so I'm thinking that some spambot is 
> populating the HTML form with something the script doesn't like(?).  However, 
> I tested that myself by entering text, or by entering just 2 digits, but 
> there was no error.  FYI, I do have code in the script that catches faulty 
> input and warns people in their browser to go back and re-enter the correct 
> data, so I'm at a loss as to why this is happening.
>
>       How can I see what's triggering this to happen?  I have the following 
> line in my php.ini:
>
> error_reporting = E_ALL & E_STRICT & E_NOTICE & E_DEPRECATED
>
> Thank you,
> Angela


-- 
Marco Behnke
Dipl. Informatiker (FH), SAE Audio Engineer Diploma
Zend Certified Engineer PHP 5.3

Tel.: 0174 / 9722336
e-Mail: ma...@behnke.biz

Softwaretechnik Behnke
Heinrich-Heine-Str. 7D
21218 Seevetal

http://www.behnke.biz


Attachment: signature.asc
Description: OpenPGP digital signature


--- End Message ---
--- Begin Message ---
On Mar 12, 2013, at 2:26 PM, Marco Behnke wrote:

> what is in $states?
> Looks like $states[$state] is not an array.

        Here's a sample:

<?php
$states = array(
        'AL' => array( '350','351','352','353', ),
        'AK' => array( '995','996','997','998','999', ),
        'AZ' => array( '850','851','852','853','854', ),
...
        'WI' => array( '530','531','532', ), 
        'WY' => array( '820','821','822','823','824', ),
);
?>


> side effects if you don't act carefully with it.

        I don't remember where that came from, but for the most part, this 
script works perfectly.  However, I removed it and will test without it.

Angela

--- End Message ---
--- Begin Message ---
$states = array(
        'AL' => array( '350','351','352','353', ),
        'AK' => array( '995','996','997','998','999', ),
        'AZ' => array( '850','851','852','853','854', ),
...
        'WI' => array( '530','531','532', ),
        'WY' => array( '820','821','822','823','824', ),
);
?>
Seeing your structure now, I think the problem is that '$state' is not set to a value in the array. Are you picking a State and assigning it to $state before executing this loop? If not, then $states[$state] is undefined which is not an array var.


--- End Message ---
--- Begin Message ---
        I think I figured it out.

<?php
$states = array(
    'AL' => array( '350','351','352','353', ),
    'AK' => array( '995','996','997','998','999', ),
    'AZ' => array( '850','851','852','853','854', ),
    'WI' => array( '530','531','532', ), 
    'WY' => array( '820','821','822','823','824', ),
);

$zip = 35261;
$state = 'XX';

$zip_short = substr($zip, 0, 3);
foreach ($states[$state] as $zip_prefix) { 
    if ($zip_prefix == $zip_short) {
        echo "State = $state";
    } else {
        echo 'no';
    }
}
?>

        Running this script, I got the same error as before.  If $state is a 
known state abbreviation in the array, everything is fine, but if someone was 
to enter, say 'XX' like I did above or leave it blank, then the error is 
produced.  I placed an if statement around the foreach loop to test for that 
and I'll keep an eye on it.

        Thank you for getting me to look at the array again, which led me to 
look at the State.

Angela

--- End Message ---
--- Begin Message ---
Angela Barone wrote:

> I think I figured it out.
> 
> <?php
> $states = array(
>     'AL' => array( '350','351','352','353', ),
>     'AK' => array( '995','996','997','998','999', ),
>     'AZ' => array( '850','851','852','853','854', ),
>     'WI' => array( '530','531','532', ),
>     'WY' => array( '820','821','822','823','824', ),
> );
> 
> $zip = 35261;
> $state = 'XX';
> 
> $zip_short = substr($zip, 0, 3);
> foreach ($states[$state] as $zip_prefix) {
>     if ($zip_prefix == $zip_short) {
>         echo "State = $state";
>     } else {
>         echo 'no';
>     }
> }
> ?>
> 
> Running this script, I got the same error as before.  If $state is a known
> state abbreviation in the array, everything is fine, but if someone was to
> enter, say 'XX' like I did above or leave it blank, then the error is
> produced.  I placed an if statement around the foreach loop to test for
> that and I'll keep an eye on it.
> 
> Thank you for getting me to look at the array again, which led me to look
> at the State.
> 
> Angela

Presumably there is a fixed list of State - those are US states? - so why 
not provide a drop down list of the possible choices?

-- 
Cheers
David Robley

"I need to be careful not to add too much water," Tom said with great 
concentration.


--- End Message ---
--- Begin Message ---
On Mar 12, 2013, at 5:16 PM, David Robley wrote:
> Presumably there is a fixed list of State - those are US states? -

> so why not provide a drop down list of the possible choices?

        There is, but the problem must have been that if someone didn't select 
a State, $state was blank.  I've since given the "Select a State..." choice a 
value of 'XX' and I'm now looking for that in the if statement I mentioned 
before.

Angela

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

Bastien Koert

On 2013-03-11, at 10:50 PM, Karl DeSaulniers <k...@designdrumm.com> wrote:

> 
> On Mar 11, 2013, at 8:22 AM, Bastien Koert wrote:
> 
>> On Sun, Mar 10, 2013 at 6:28 PM, Karl DeSaulniers <k...@designdrumm.com> 
>> wrote:
>>> 
>>> On Mar 10, 2013, at 6:03 AM, richard gray wrote:
>>> 
>>>> On 10/03/2013 11:47, Karl DeSaulniers wrote:
>>>>> 
>>>>> Hi Guys,
>>>>> I am hoping someone can guide me or help me fix this issue.
>>>>> I have been lost in the code for some time now.
>>>>> I am trying to get the attributes of an xml node.
>>>>> I have this code:
>>>>> [snip]
>>>> 
>>>> this may help ->
>>>> http://stackoverflow.com/questions/1156957/php-xml-attribute-parsing
>>>>> 
>>>>> I do admit I haven't the foggiest idea what I am doing here, so I am
>>>>> noobing out here on how this php function is really working.
>>>>> I got it off php.net and am trying to implement it in my code. Without my
>>>>> addition it works well except it doesn't grab any attributes
>>>>> which I need in order for my script to work properly.
>>>>> 
>>>>> Any ideas on what I am doing wrong?
>>>>> 
>>>>> TIA,
>>>> 
>>>> 
>>>> HTH
>>>> rich
>>> 
>>> 
>>> 
>>> Thanks rich,
>>> That uses simpleXML. I am using xml_parse_create.
>>> Any ideas for xml_parse_create? I really like the way this function
>>> puts everything into an array that I can traverse. I only need it to put
>>> the attribute values in the array for the corresponding node and I am done.
>>> *Sigh
>>> 
>>> Thanks for your help,
>>> 
>>> Best,
>>> 
>>> 
>>> Karl DeSaulniers
>>> Design Drumm
>>> http://designdrumm.com
>>> 
>>> 
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>> 
>> I came across this XML to JSON to an array some time ago. It might be 
>> something
>> 
>> function xmlToArray($xml)
>> {
>>    return json_decode(json_encode((array) simplexml_load_string($xml)),1);
>> }
>> 
>> -- 
>> 
>> Bastien
>> 
>> Cat, the other other white meat
> 
> Actually no, I don't need the root node after all.
> Looks like it finally works! :)
> 
> Thank you Bastien! Nice one-liner.
> 
> Best,
> 
> Karl DeSaulniers
> Design Drumm
> 

Glad it works for you 
> 

--- End Message ---
--- Begin Message ---
In my database design, I tend to store some variable that is meant to be acting 
as a ROLE or TYPE as SMALLINT. For example : 

    CREATE TABLE `house` (
       `id` int(11) NOT NULL AUTO_INCREMENT,
       `type` smallint(11) NOT NULL,
    )


And in php, I do

    define('HOUSE_SMALL_TYPE', '0');
    define('HOUSE_MEDIUM_TYPE', '1');

So in php, in SELECT queries I do :

    $this->db->query("SELECT * FROM house  
                        WHERE type=?;", HOUSE_SMALL_TYPE);

My questions are : 
1. In the php part, is there is a better way to do this ? 
2. In the mysql itself, does mysql also has global define functionality (like 
the define in php) ? I also want to do kind of SELECT * FROM house WHERE type = 
HOUSE_SMALL_TYPE in mysql query.


--- End Message ---

Reply via email to