note 28504 added to faq.html

From: Date: Wed, 15 Jan 2003 08:18:02 +0000
Subject: note 28504 added to faq.html
Groups: php.notes 
Request: Send a blank email to php-notes+get-42157@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. 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://www.php.net/manual/en/faq.html.php http://master.php.net/manage/user-notes.php?action=edit+28504 http://master.php.net/manage/user-notes.php?action=delete+28504 http://master.php.net/manage/user-notes.php?action=reject+28504

« previous php.notes (#42157) next »