note 28504 modified in faq.html by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 13:55:26 +0000 |
| Subject: | note 28504 modified in faq.html by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80038@lists.php.net to get a copy of this message | ||
For those of you familiar with ASP or apache aprea_request_params_as_string, this function should be
very welcome to you to turn things like
<input type="hidden" name="selected_category_ids[]" value="1">
<input type="hidden" name="selected_category_ids[]" value="4">
<input type="hidden" name="selected_category_ids[]" value="5">
<input type="hidden" name="selected_category_ids[]" value="6">
<input type="hidden" name="selected_category_ids[]" value="7">
into
1, 4, 5, 6, 7
To use it, pass the name from html into this function, WITH the []'s (you can remove the
log_func from this function if you want).
Basically, this functgion checks for a [] at the end of the string. If it's there, it converts
it to a comma delimited string, if not, it returns the value as normal.
<?php
function getBlindFormData( $form_name ) {
if ( !is_string( $form_name ) ) {
$this->{$this->log_func} ( "File: %s. Line: %d. \$form_name is NOT a string.",
__FILE__, __LINE__ );
}
$offs = strlen( $form_name ) - 2;
if ( strpos ( $form_name, "[]", $offs ) === $offs ) {
$form_name = substr( $form_name, 0, $offs );
$isarray = 1;
} else {
$isarray = 0;
}
if ( isset( $_GET[$form_name] ) ) {
$request = $_GET[$form_name];
}
if ( isset( $_POST[$form_name] ) ) {
$request = $_POST[$form_name];
}
$ret = NULL;
if ( isset( $request ) ) {
if ( $isarray ) {
$ret = $request[0];
for ( $i = 1; $i < count( $request ); $i++ ) {
$ret .= ", ".$request[$i];
}
} else {
$ret = $request;
}
}
return $ret;
}
?>
Usage could be as follows
To select a comma delimited list of values.
<?php
$sql = sprintf( "DELETE FROM categories\n"
. " WHERE category_id IN ( %s );\n"
, $wrapper->getRequiredFormData( "selected_category_ids[]" ) );
?>
or just $wrapper->getOptionalFormData( "category_id" ); for retrieval of a normal http
variable.
Any questions, problems, bugs you find in this email me at the abvoe email address. (remove the NO
before chatgris and the SPAM after chatgris etc.)
--was--
For those of you familiar with ASP or apache aprea_request_params_as_string, this function should be
very welcome to you to turn things like
<input type="hidden" name="selected_category_ids[]" value="1">
<input type="hidden" name="selected_category_ids[]" value="4">
<input type="hidden" name="selected_category_ids[]" value="5">
<input type="hidden" name="selected_category_ids[]" value="6">
<input type="hidden" name="selected_category_ids[]" value="7">
into
1, 4, 5, 6, 7
To use it, pass the name from html into this function, WITH the []'s (you can remove the
log_func from this function if you want).
Basically, this functgion checks for a [] at the end of the string. If it's there, it converts
it to a comma delimited string, if not, it returns the value as normal.
function getBlindFormData( $form_name ) {
if ( !is_string( $form_name ) ) {
$this->{$this->log_func} ( "File: %s. Line: %d. \$form_name is NOT a string.",
__FILE__, __LINE__ );
}
$offs = strlen( $form_name ) - 2;
if ( strpos ( $form_name, "[]", $offs ) === $offs ) {
$form_name = substr( $form_name, 0, $offs );
$isarray = 1;
} else {
$isarray = 0;
}
if ( isset( $_GET[$form_name] ) ) {
$request = $_GET[$form_name];
}
if ( isset( $_POST[$form_name] ) ) {
$request = $_POST[$form_name];
}
$ret = NULL;
if ( isset( $request ) ) {
if ( $isarray ) {
$ret = $request[0];
for ( $i = 1; $i < count( $request ); $i++ ) {
$ret .= ", ".$request[$i];
}
} else {
$ret = $request;
}
}
return $ret;
}
Usage could be as follows
To select a comma delimited list of values.
$sql = sprintf( "DELETE FROM categories\n"
. " WHERE category_id IN ( %s );\n"
, $wrapper->getRequiredFormData( "selected_category_ids[]" ) );
or just $wrapper->getOptionalFormData( "category_id" ); for retrieval of a normal http
variable.
Any questions, problems, bugs you find in this email me at the abvoe email address. (remove the NO
before chatgris and the SPAM after chatgris etc.)
http://php.net/manual/en/faq.html.php