Re: [PHP] New way to make select boxes auto select

2002-07-25 Thread Roger Thomas

i used the same techniqu a while back when i worked with Fast Template.
scenario:
- present a user with a form to input
- there are several text fields and several select options
- if there is/are error(s) upon form submission, we will call the same template
file and insert proper error messages at appropriate places, normally close to
the input field.
- we would also luv to make the select option field to DEFAULT to the PREVIOUS
value that WAS selected.

with the technique that Nathan describe below, you can do just that.

At present i have dropped this technique as I have switched to Smarty. Smarty
can do all this with its {html_optios blah blah blah}.

Recently I made frequent use of Webreference javascript to make select boxes
that are related to each other. ie changing(selecting) a left select box would
make a right select box change accordingly. I am trying to apply Smarty to this
but my javascript knowledge is too shallow. after submitting a form with
errors, the 2 select boxes will default back when the script starts. which is
NOT what i want. if nebody have done this, appreciate your help.

--
roger

--- Nathan Cook <[EMAIL PROTECTED]> wrote:
> You may already be doing it like this, but I think I found a new way to
> make select boxes auto-select (what data they put in) a lot easier. All you
> have to do is put a variable in each select tag that is equal to the value
> of the select option i.e.:  -- then all you
> have to do is base the variable on that  $$interest =
> "selected"; quick and easy with out having to loop through an if elseif
> statement.  Let me know if you like that method or have any objections.
> 
> Full example below.
> 
> print("\n");
> // creates a variable with a name based on
> // the value of interest with a value of "selected"
> $$interest = "selected";
> print("- Select One -\n");
> print("Teacher\n");
> print("Lego Enthusiast\n");
> print("Student\n");
> print("Homeschool Parent\n");
> print("Browsing\n");
> print("Afterschool\n");
> print("Boys & Girls Club\n");
> print("YMCA\n");
> print("  \n");
> 
> [ Nathan Cook | [EMAIL PROTECTED] ]
> 
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 


__
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com

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




RE: [PHP] New way to make select boxes auto select

2002-07-25 Thread Matt Schroebel

> From: Nathan Cook [mailto:[EMAIL PROTECTED]] 
> Sent: Thursday, July 25, 2002 2:52 PM
> Subject: Re: [PHP] New way to make select boxes auto select

> How are you able to quickly and painlessly determine which 
> key gets the
> selected value, from form submission data, when building the 
> initial array?

Via the selected name, say, $POST['location']. I usually use the db code table method 
and something like:
echo setupSelect($db,"locations","LocName","LocName",$_POST['location']);

If you were using hard coded values like Active, Inactive,etc you can just do a:
$status = array('Active','Inactive');
$status[$_POST['status']] = 'selected;
echo buildSelect($status);

It doesn't handle multi selects and it's resulting arrays, but that should be too hard 
to do by checking if the selected value is an array & modify the comparison in that 
case.

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




Re: [PHP] New way to make select boxes auto select

2002-07-25 Thread Nathan Cook

From: "Johnson, Kirk" <[EMAIL PROTECTED]>
> Do you know what happens here if the error reporting is set to max?
> Are a bunch of "unitialized variables" warnings issued?

That would be my assumption.  I suppose you could initialize the variables
first to circumvent that. I was more or less looking for a quick and dirty
way to accomplish the task.

However, it looks like some good functions have been submitted to the list
which would solve the error reporting.  Something along the lines of
building an array and then calling a function to loop through the array
pieces and build a select menu.

In my case, I wasn't looking to create another function, just a quick
statement.


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




Re: [PHP] New way to make select boxes auto select

2002-07-25 Thread Nathan Cook

> // buildSelect -- return a Select box named $selectName based on key
value array $selectArray
> ...
> // $arr = array('MD'=>'selected','DC'=>'','VA'=>'');

How are you able to quickly and painlessly determine which key gets the
selected value, from form submission data, when building the initial array?


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




RE: [PHP] New way to make select boxes auto select

2002-07-25 Thread Matt Schroebel

> From: Nathan Cook [mailto:[EMAIL PROTECTED]] 
> Sent: Thursday, July 25, 2002 12:34 PM
> Subject: [PHP] New way to make select boxes auto select
> 
> 
> You may already be doing it like this, but I think I found a 
> new way to
> make select boxes auto-select (what data they put in) a lot 
> easier. All you
> have to do is put a variable in each select tag that is equal 
> to the value
> of the select option i.e.:  -- 
> then all you
> have to do is base the variable on that  name=interest> $$interest =
> "selected"; quick and easy with out having to loop through an 
> if elseif
> statement.  Let me know if you like that method or have any 
> objections.

I use these functions which do something similar.  The first function accepts an array 
of names for the option, and returns a string of all of the  but not the 
 tags.  The ones to be selected have a value of 'selected'.  The second 
function accepts a code table, of say states or provinces, and a value that is to be 
selected, and returns the .  Both are only for single selected values, but 
could be easily extended.  
Usage:

// 
---
// buildSelect -- return a Select box named $selectName based on key value array 
$selectArray
//
// 'selected','DC'=>'','VA'=>'');
// echo buildSelect($arr);
// ?>
// 
// 
---
function buildSelect($selectArray) {
$str = '';
$count = count($selectArray);
for ($i=0;$i<$count;++$i) {
list($key,$selected) = each($selectArray[$i]);
$selectValue = htmlspecialchars($key);
$str .= "$key\n";
}
return($str);
}
// 
---
// setupSelect -- Read table $table from database $db, load all values of $field, and 
build 
//a select box from this list named $selName
// 
// 
// 
// 
---
function setupSelect($db,$table,$field,$orderBy,$value) {
$sql = "SELECT " . $field . " from " . $table;
if (empty($orderBy)) {
$orderBy = $field;
}
$sql .= " order by " . $orderBy;
$result = mysql_query($sql,$db) or 
die(log_mysql_error($sql,__FILE__,__LINE__));
while ($row = mysql_fetch_array($result)) {
$columnData = $row[$field];
if (!empty($columnData)) {
$selected = ($columnData == $value ? "selected" : "");
$selArray[] = array($columnData => $selected);
}
}
if (count($selArray) != 0) {
return(buildSelect($selArray));
}
}
// -
// logs to syslog define the constant DISPLAY_MYSQL_ERRORS as 1 
// if you want to see errors in browser too
// usage: $result = mysql_query($sql,$db) 
//  or die(log_mysql_error($sql,__FILE__,__LINE__));
// -
function log_mysql_error($sql='',$file='',$line=0) {
$msg="{$_SERVER['PHP_SELF']}: (FILE=$file) (LINE=$line)\n";
$msg .= "MySQL Says: " . mysql_error() . "\n";
if (!empty($sql)) {
$msg .= "SQL Statement was: $sql";
  }
error_log($msg,0);
if (1 == DISPLAY_MYSQL_ERRORS) {
$msg=nl2br($msg);
echo "$msg\n";
}
exit;
}

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




RE: [PHP] New way to make select boxes auto select

2002-07-25 Thread Johnson, Kirk

Nathan, this is a new idea to me and very interesting. Do you know what
happens here if the error reporting is set to max? Are a bunch of
"unitialized variables" warnings issued?

Thanks for contributing this to the list.

Kirk

> -Original Message-
> From: Nathan Cook [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, July 25, 2002 10:34 AM
> To: Php List
> Cc: David Chase
> Subject: [PHP] New way to make select boxes auto select
> 
> 
> You may already be doing it like this, but I think I found a 
> new way to
> make select boxes auto-select (what data they put in) a lot 
> easier. All you
> have to do is put a variable in each select tag that is equal 
> to the value
> of the select option i.e.:  -- 
> then all you
> have to do is base the variable on that  name=interest> $$interest =
> "selected"; quick and easy with out having to loop through an 
> if elseif
> statement.  Let me know if you like that method or have any 
> objections.
> 
> Full example below.
> 
> print("\n");
> // creates a variable with a name based on
> // the value of interest with a value of "selected"
> $$interest = "selected";
> print("- Select One -\n");
> print("Teacher\n");
> print("Lego Enthusiast\n");
> print("YMCA\n");
> print("  \n");

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




Re: [PHP] New way to make select boxes auto select

2002-07-25 Thread 1LT John W. Holmes

Interesting technique. Depending on your error_reporting levels, you may get
a bunch of undefined variable warnings, though.

I usually use a config file with arrays for my select boxes. Something like

$_CONF['Something'] = array("This","That","And","Something");

Then just create a function like conf_select($key,$selected); where you pass
the $_CONF variable key that you want the select box to be made of and the
default value.

---John Holmes...

- Original Message -
From: "Nathan Cook" <[EMAIL PROTECTED]>
To: "Php List" <[EMAIL PROTECTED]>
Cc: "David Chase" <[EMAIL PROTECTED]>
Sent: Thursday, July 25, 2002 12:33 PM
Subject: [PHP] New way to make select boxes auto select


> You may already be doing it like this, but I think I found a new way to
> make select boxes auto-select (what data they put in) a lot easier. All
you
> have to do is put a variable in each select tag that is equal to the value
> of the select option i.e.:  -- then all you
> have to do is base the variable on that  $$interest
=
> "selected"; quick and easy with out having to loop through an if elseif
> statement.  Let me know if you like that method or have any objections.
>
> Full example below.
>
> print("\n");
> // creates a variable with a name based on
> // the value of interest with a value of "selected"
> $$interest = "selected";
> print("- Select One -\n");
> print("Teacher\n");
> print("Lego Enthusiast\n");
> print("Student\n");
> print("Homeschool
Parent\n");
> print("Browsing\n");
> print("Afterschool\n");
> print("Boys & Girls Club\n");
> print("YMCA\n");
> print("  \n");
>
> [ Nathan Cook | [EMAIL PROTECTED] ]
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


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




[PHP] New way to make select boxes auto select

2002-07-25 Thread Nathan Cook

You may already be doing it like this, but I think I found a new way to
make select boxes auto-select (what data they put in) a lot easier. All you
have to do is put a variable in each select tag that is equal to the value
of the select option i.e.:  -- then all you
have to do is base the variable on that  $$interest =
"selected"; quick and easy with out having to loop through an if elseif
statement.  Let me know if you like that method or have any objections.

Full example below.

print("\n");
// creates a variable with a name based on
// the value of interest with a value of "selected"
$$interest = "selected";
print("- Select One -\n");
print("Teacher\n");
print("Lego Enthusiast\n");
print("Student\n");
print("Homeschool Parent\n");
print("Browsing\n");
print("Afterschool\n");
print("Boys & Girls Club\n");
print("YMCA\n");
print("  \n");

[ Nathan Cook | [EMAIL PROTECTED] ]



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