Search code examples
phpdatabaseprintingrequired

Database Field, Required


I was trying to ask about required field print out to make sure its working. But the question was rated so badly to prevent me from asking more questions. this was the solution for it, try to rate it up so i can remove block.

 <?php   if (empty($_POST) === false) {  
    $required_fields = array(
        'username', 'password', 'password_again', 
        'first_name', 'last_name', 
        'email', 'bday', 
        'county', 'zip', 'gender'
    );
    // create our output array
    $output = array(); 
    $altOutput = array();

    // this adds the post value to our output array
    // using a foreach loop
    // it simply looks for the POST by the field in your array
    // if it's found and not empty, it will add to our new output array
    foreach ($required_fields as $field) 
        if (!empty($_POST[$field])) {
            // this adds the field to your new output array
            $output[] = $_POST[$field];
            // this removes it from the POST array
            unset($_POST[$field]);
        }

    // now output the new array having only the fields you want
    echo '<pre>', print_r($output, true), '</pre>';

    // if you want to print out what's left over
    echo '<pre>', print_r($_POST, true), '</pre>';}

Solution

  • So, I just noticed, despite having fully answered the question. Despite even 
    being selected as correct answer. The 3 upvotes I had gained on this lost the 
    final mark today, thus putting this answer at 0. I also noticed this question 
    is now marked closed. Could someone please explain to me why an answered 
    question is closed after the fact and why the selected correct answer is 
    steadily being downvoted?
    


    It's doing exactly as you said because that's what you have set up for it to do.

    Quick break down:

    <?php // init php code
        // here you check if post has anything in it's array
        // that's right, $_POST will be an ARRAY
        if (empty($_POST) === false) { // same as `if (!empty($_POST)) {` except longer
            // here you simply set an array variable
            $required_fields = array(username, password, password_again, first_name, last_name, email, bday, county, zip, gender);
            // here you're one lining it, but suffice it to say, you're sending a <pre> tag to the browser
            // which helps to make what `print_r` produces more readable as you're printing an array
            echo '<pre>', print_r($_POST, true), '</pre>';
    

    As you may have noticed, you're not even using your array, $required_fields.

    Now

    If you would like to print each one of those fields and only those fields AS AN ARRAY, then you need to make it so

    <?php   
    if (empty($_POST) === false) {  
            $required_fields = array(
                'username', 'password', 'password_again', 
                'first_name', 'last_name', 
                'email', 'bday', 
                'county', 'zip', 'gender'
            );
    
            // create our output array
            $output = array(); 
            // $altOutput = array(); // not used in this example
    
            // this adds the post value to our output array
            // using a foreach loop
            // it simply looks for the POST by the field in your array
            // if it's found and not empty, it will add to our new output array
            foreach ($required_fields as $field) 
                if (!empty($_POST[$field])) {
                    // this adds the field to your new output array
                    $output[] = $_POST[$field];
                    // this removes it from the POST array
                    unset($_POST[$field]);
                }
    
            // now output the new array having only the fields you want
            echo '<pre>', print_r($output, true), '</pre>';
    
            // if you want to print out what's left over
            echo '<pre>', print_r($_POST, true), '</pre>';
    }