Altec Posted October 23, 2008 Share Posted October 23, 2008 I currently have this code: $month = htmlspecialchars(strip_tags($_POST['month'])); $date = htmlspecialchars(strip_tags($_POST['date'])); $year = htmlspecialchars(strip_tags($_POST['year'])); $time = htmlspecialchars(strip_tags($_POST['time'])); $title = htmlspecialchars(strip_tags($_POST['title'])); $entry = $_POST['entry']; To save a bit of space and add error detection without a massive if statement, I decided to do this: foreach($_POST as $key => $value) { $value = trim($value); if(empty($value)) { $error .= 'Field ' . $key . ' empty.<br />'; } if($key != 'entry') { $_POST[$key] = htmlspecialchars(strip_tags($value)); } } if(isset($error)) { $page = new Page('templates/sys/info.html'); $page->replace_tags(array( 'CONTENT' => 'Error when processing post:<br /><br />' . $error, 'REFRESH' => '', )); $page->output(); exit; } However, the rest of my code still uses the original variable names ($date instead of $_POST['date']). Instead of going through the rest of my code and several included files and changing all the variable names, how can I easily make a new variable with the name of $key with the value of $value? For some reason I just can't think of how to do this. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/129689-php-loop-help/ Share on other sites More sharing options...
aniesh82 Posted October 23, 2008 Share Posted October 23, 2008 I think, the extract() function will help you to do the task. See the details at: http://in2.php.net/manual/en/function.extract.php I have also a suggestion for you. Please print the $_POST array before the first foreach loop. Mostly the POST array will contain more values than you expect. So in your case, it only need to loop the form fields... Thank you, Joseph Quote Link to comment https://forums.phpfreaks.com/topic/129689-php-loop-help/#findComment-672409 Share on other sites More sharing options...
Altec Posted October 23, 2008 Author Share Posted October 23, 2008 Thank you very much for the link about extract(). It was very helpful and is exactly what I was looking for. Also, what do you mean by "print the $_POST array first?" EDIT: Do you mean to actually define a new array with the $_POST values I want? <?php $fields = array( 'month' => $_POST['month'], 'date' => $_POST['date'], 'year' => $_POST['year'], 'time' => $_POST['time'], 'title' => $_POST['title'], ); foreach($fields as $key => $value) { $value = trim($value); if(empty($value)) { $error .= 'Field ' . $key . ' empty.'; } $fields[$key] = htmlspecialchar(strip_tags($value)); } if(isset($error)) { $page = new Page('templates/sys/info.html'); $page->replace_tags(array( 'CONTENT' => 'Error when processing post:<br /><br />' . $error, 'REFRESH' => '', )); $page->output(); exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/129689-php-loop-help/#findComment-672417 Share on other sites More sharing options...
Maq Posted October 23, 2008 Share Posted October 23, 2008 To print out an array you should use the print_r() function. Put this in before the foreach loop: print_r ($fields); Quote Link to comment https://forums.phpfreaks.com/topic/129689-php-loop-help/#findComment-672453 Share on other sites More sharing options...
Altec Posted October 23, 2008 Author Share Posted October 23, 2008 I'm not seeing how that will help me... Quote Link to comment https://forums.phpfreaks.com/topic/129689-php-loop-help/#findComment-672458 Share on other sites More sharing options...
Maq Posted October 23, 2008 Share Posted October 23, 2008 Also, what do you mean by "print the $_POST array first?" Quote Link to comment https://forums.phpfreaks.com/topic/129689-php-loop-help/#findComment-672461 Share on other sites More sharing options...
Altec Posted October 23, 2008 Author Share Posted October 23, 2008 Right; what exactly will that do for me, other than print out an array? Quote Link to comment https://forums.phpfreaks.com/topic/129689-php-loop-help/#findComment-672470 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.