Jump to content

Recommended Posts

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. :)

Link to comment
https://forums.phpfreaks.com/topic/129689-php-loop-help/
Share on other sites

 

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

Link to comment
https://forums.phpfreaks.com/topic/129689-php-loop-help/#findComment-672409
Share on other sites

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;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/129689-php-loop-help/#findComment-672417
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.