Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

I'm stumped!

LEGEND ,
Jan 18, 2008 Jan 18, 2008

Copy link to clipboard

Copied

I am using a function to populate a select tag with
states/provinces/territories -

<?php function stateselectlist() {

$states = array('AL' => 'Alabama', 'AK' =>
'Alaska', 'AZ' => 'Arizona', 'AR' => 'Arkansas', 'CA' => 'California',
'CO' => 'Colorado', 'CT' => 'Connecticut', 'DC' => 'District of
Columbia', 'DE' => 'Delaware', 'FL' => 'Florida', 'GA' => 'Georgia', 'HI'
=> 'Hawaii', 'ID' => 'Idaho', 'IL' => 'Illinois', 'IN' => 'Indiana', 'IA'
=> 'Iowa', 'KS' => 'Kansas', 'KY' => 'Kentucky', 'LA' => 'Louisiana',
'ME' => 'Maine', 'MD' => 'Maryland', 'MA' => 'Massachusetts', 'MI' =>
'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi', 'MO' =>
'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada', 'NH'
=> 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' =>
'New York', 'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' =>
'Ohio', 'OK' => 'Oklahoma', 'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'PR
'=> 'Puerto Rico', 'RI' => 'Rhode Island', 'SC' => 'South Carolina', 'SD'
=> 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah',
'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' =>
'West Virginia', 'WI' => 'Wisconsin', 'WY' => 'Wyoming', 'ONT' =>
'Ontario',
'QUE' => 'Quebec', 'NOV' => 'Nova Scotia', 'NEW' => 'New Brunsick',
'MAN' => 'Manitoba', 'BRI' => 'British Columbia', 'PEI' => 'Prince Edward
Island',
'SAS' => 'Saskatchewan', 'ALB' => 'Alberta', 'NFL' => 'Newfoundland and
Labrador',
'NWT' => 'Northwest Territories', 'YUK' => 'Yukon', 'NUN' => 'Nunavut');

$str = "<select name=\"VisitorBillStateID\" id=\"VisitorBillStateID\">\n";
foreach ($states as $key => $value) {
if($key == 'AL') { $str .= "<optgroup label=\"States\">"; }
if($key == 'ONT') { $str .= "<optgroup label=\"Provinces\">"; }
if($key == 'NWT') { $str .= "<optgroup label=\"Territories\">"; }
$str .= "<option value=\"$key\"";
if ($row_rsAccount['VisitorBillStateID'] == $key) {
$str .= " selected=\"selected\"";
}
$str .= ">$value</option>\n";
}
$str .= "</select>\n";
return $str;
} // END function stateselectlist


//echo $row_rsAccount['VisitorBillStateID']; exit();
echo stateselectlist();

This is being used on an "account" form page.

When the page loads the user has already logged in and the recordset has
been picked based on a session variable's value.

If I uncomment that next-to-last echo, I get the correct state printed to
the page for the value of $row_rsAccount['VisitorBillStateID'].

If I comment it out, and allow the function to be called, I get oodles of
instances of -

Notice: Undefined variable: row_rsAccount in
/vservers/squarewheeli/htdocs/s_account.php on line 268

Line 268 is this line -

if ($row_rsAccount['VisitorBillStateID'] == $key) {

What, please, am I overlooking here?

Thanks,

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


TOPICS
Server side applications

Views

481
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 18, 2008 Jan 18, 2008

Copy link to clipboard

Copied

Murray *ACE* wrote:
> What, please, am I overlooking here?

Variable scope.

http://docs.php.net/manual/en/language.variables.scope.php

A variable inside a function exists only within the function. The simple
way around your dilemma is to pass the variable to the function:

function stateselectlist($selectedState) {
// yadda, yadda
if ($selectedState == $key) {
// yadda, yadda
}

stateselectlist($row_rsAccount['VisitorBillStateID']);


--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 18, 2008 Jan 18, 2008

Copy link to clipboard

Copied

.oO(Murray *ACE*)

>I am using a function to populate a select tag with
>states/provinces/territories -
>
><?php function stateselectlist() {
>
>$states = array('AL' => 'Alabama', 'AK' =>
>[...]
>
>$str = "<select name=\"VisitorBillStateID\" id=\"VisitorBillStateID\">\n";

Please do yourself and other readers of your code a favour and try to
avoid this ugly escaping (unless you really like it 😉 ), HTML also
allows single quotes:

$str = "<select name='VisitorBillStateID' id='VisitorBillStateID'>\n";

>[...]
>//echo $row_rsAccount['VisitorBillStateID']; exit();
>echo stateselectlist();
>
>This is being used on an "account" form page.
>
>When the page loads the user has already logged in and the recordset has
>been picked based on a session variable's value.
>
>If I uncomment that next-to-last echo, I get the correct state printed to
>the page for the value of $row_rsAccount['VisitorBillStateID'].
>
>If I comment it out, and allow the function to be called, I get oodles of
>instances of -
>
>Notice: Undefined variable: row_rsAccount in
>/vservers/squarewheeli/htdocs/s_account.php on line 268
>
>Line 268 is this line -
>
> if ($row_rsAccount['VisitorBillStateID'] == $key) {
>
>What, please, am I overlooking here?

It's a scope issue. $row_rsAccount is defined in the global scope
outside of your function, hence it's not visible inside of it by
default. There are three ways to solve this:

1) Use

global $row_rsAccount;

at the beginning of the function to import this variable into the local
scope.

=> Really ugly, you should avoid it if possible.

2) Pass the current state as a parameter to the function:

function stateselectlist($state) {
...
}

echo stateselectlist($row_rsAccount['VisitorBillStateID']);

=> Much better.

3) Use OOP.

=> Probably overkill in this case.

Micha

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 18, 2008 Jan 18, 2008

Copy link to clipboard

Copied

On Fri, 18 Jan 2008 16:34:22 -0500, "Murray *ACE*"
<forums@HAHAgreat-web-sights.com> wrote:

> if ($row_rsAccount['VisitorBillStateID'] == $key) {
>
>What, please, am I overlooking here?

That the $row_rsAccount is out of scope where you're using it inside the
function. Pass it as a parameter to the function:

echo stateselectlist($row_rsAccount['VisitorBillStateID']);

Then, in the function, change this:

function stateselectlist() {

To this:

function stateselectlist($state) {

And this:

if ($row_rsAccount['VisitorBillStateID'] == $key) {

To this:

if ($state == $key) {

Gary

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 18, 2008 Jan 18, 2008

Copy link to clipboard

Copied

Thanks all - that'd be the solution!

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Gary White" <reply@newsgroup.please> wrote in message
news:00c2p3t30o20jtqsltpogr4h2b3bmt6n1a@4ax.com...
> On Fri, 18 Jan 2008 16:34:22 -0500, "Murray *ACE*"
> <forums@HAHAgreat-web-sights.com> wrote:
>
>> if ($row_rsAccount['VisitorBillStateID'] == $key) {
>>
>>What, please, am I overlooking here?
>
> That the $row_rsAccount is out of scope where you're using it inside the
> function. Pass it as a parameter to the function:
>
> echo stateselectlist($row_rsAccount['VisitorBillStateID']);
>
> Then, in the function, change this:
>
> function stateselectlist() {
>
> To this:
>
> function stateselectlist($state) {
>
> And this:
>
> if ($row_rsAccount['VisitorBillStateID'] == $key) {
>
> To this:
>
> if ($state == $key) {
>
> Gary

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 18, 2008 Jan 18, 2008

Copy link to clipboard

Copied

LATEST
On Fri, 18 Jan 2008 21:03:02 -0500, "Murray *ACE*"
<forums@HAHAgreat-web-sights.com> wrote:

>Thanks all - that'd be the solution!

Chure welcome.

Gary

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines