Counting empty Cells in a row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whitep8
    New Member
    • Oct 2009
    • 65

    Counting empty Cells in a row

    Hi All,

    I want to count the number of empty cells in a returned row. Can somebody get me on my way?

    Regards
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    I presume your row is just a standard array, returned by a function like the mysql_fetch_arr ay?

    If so you have a number of options, like:

    Comment

    • whitep8
      New Member
      • Oct 2009
      • 65

      #3
      Hi,

      Thanks for the response.

      Whilst waiting for a response I came up with checking the string length of the items called:
      Code:
      if (strlen($dev_business_name) > 0) { $count ++; } else { $count; }
      Im fine with while loops, but this is a new one to me. From your suggestions, without testing as im home from work now I have this.......

      Code:
      <?php
      $arr = array($address, $telephone number, $dob);
      foreach ($arr as &$value) {
          count =0;
          $value = strlen($arr);
          if ($value > 0){
             $count ++
          }else{
              $count)
      }
      // $arr is now array(2, 4, 6, 8)
      unset($value); // break the reference with the last element
      ?>
      could this work? Could you help me with the syntax?

      Very much appreciated

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Your close. You just need to:
        • Move the $count = 0; out of the loop, or it will reset on each iteration.
        • Use the strlen function on $value, not $arr; to check the current element, not the array itself.
        • Reverse the strlen check. (You wanted to count empty fields not non-empty fields, right?)
        • Remove the else clause. You don't need it. You only need to count the empty elements. If it is not empty, no code is required.

        And that should make it work.

        Also, to make improve it a bit:
        • You may be better of using the empty function instead of strlen. (That also means a bit of a change to your if statement.)
        • You should remove the & from &$value. Passing foreach values by reference (which is what the & does) is only needed when you need to alter the value.
        • Remove the unset after the loop. By removing the & (in my last point) it just becomes redundant. The $value will be out of scope and thus be collected by the garbage collector.

        Comment

        • whitep8
          New Member
          • Oct 2009
          • 65

          #5
          Interesting, I didnt realise php did its own garbage collection?

          Ive made a test, incorporating your suggstions. not quite there yet. Hope you dont mind taking a look...

          Code:
          <?php
          session_start();
          include "confun.php";
          $query="SELECT * FROM tbl_dev_dilligence WHERE dev_id = 1";
          $result=mysql_query($query);
          $row = mysql_fetch_assoc($result); 
          
          $dev_contact_fname = $row['dev_contact_fname'];
          $dev_contact_lname = $row['dev_contact_lname'];
          $dev_contact2_fname = $row['dev_contact2_fname'];
          //$dev_contact2_lname = $row['dev_contact2_lname']; 
          //$dev_position = $row['dev_position'];
          //$dev_add1 = $row['dev_add1'];
          //$dev_add2 = $row['dev_add2'];
          //$dev_county = $row['dev_county'];
          //$dev_towncity = $row['dev_towncity'];
          //$dev_tel = $row['dev_tel'];
          //$dev_fax = $row['dev_fax'];
          //$dev_email = $row['dev_email'];
          
          
          
          
          $arr = array($dev_contact_fname, $dev_contact_lname, $dev_contact_fname);
          count = 0;
          foreach ($arr as $value) {
          $arr = strlen($value);
          if ($value > 0){
          $count ++;
          }
          echo $count;
          ?>

          Comment

          • whitep8
            New Member
            • Oct 2009
            • 65

            #6
            ive a feeling its line 27.... maybe need to have a new var in there

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Yea, line #27 overwrites the array with the output of the strlen() function, thus ending the loop. You would have to put that into a new variable.

              However, you don't really need that. The output of the strlen() function is only used once - in the following if statement - so you could just move it into the if statement itself.
              [code=php]if (strlen($value) <= 0){[/code]
              (Also note; if you want to count empty elements, you want to be checking if it is 0 or less, not greater than 0.)

              You could also just use the empty() function:
              [code=php]if (empty($value)) {[/code]

              Comment

              Working...