Jump to content

PHP Loop Help


Altec

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.