Count NULL fields in a form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • foxylady
    New Member
    • Sep 2006
    • 2

    Count NULL fields in a form

    Hi, everyone I have a issue I cant seem to figure out. I am trying to count how many fields are not NULL or empty.

    the form that I have that post this script has the field name n1,n2,n3 ect...

    The reason for this script is to see how many questions are answered.

    If anyone can help I would love it so much thanks :)


    for ($i=1; $i<count($_POST ) ; $i++)
    {
    $varname = "n$i";
    if( ! isset( $_POST[$varname] ) ) break;

    $val = $$varname;
    // $question_answe red = count($_POST);
    if(strlen($val) > 0 )
    {
    $test = array($val, $varname);
    echo $val."<br />";
    // echo $varname."<br />";

    $question_answe red = sizeof( $_POST ) != (strlen($val) > 0 );


    }
    }

    echo $question_answe red."<br />";
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    I could not figure out what was wrong, and while toying I made some new code. Throw it away if you don't want this.
    [PHP]<?php
    $question_answe red=0;
    $test = array();
    for ($i=0, $j=1;$i<count($ _POST) ; $i++, $j++) {
    if (isset($_POST["n$j"]) AND strlen(trim($_P OST["n$j"])) > 0) {
    $test["n$j"] = $_POST["n$j"];
    $question_answe red++;
    }
    }
    echo '<pre> ';
    print_r($test);
    echo '<br>Questions answered: '.$question_ans wered."<br />";
    ?>[/PHP]
    Ronald :cool:

    Comment

    • foxylady
      New Member
      • Sep 2006
      • 2

      #3
      OMG thank you so much it works

      Comment

      Working...