473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Counting empty Cells in a row

65
Hi All,

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

Regards
Feb 19 '10 #1

✓ answered by Atli

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.
Expand|Select|Wrap|Line Numbers
  1. if (strlen($value) <= 0){
(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:
Expand|Select|Wrap|Line Numbers
  1. if (empty($value)){

6 4478
Atli
5,058 Expert 4TB
Hey.

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

If so you have a number of options, like:
  • Loop through the array using a foreach loop, checking whether each element is empty or not.
  • Do that same, using the array_walk function.
  • Use the array_count_value function.
Feb 19 '10 #2
whitep8
65
Hi,

Thanks for the response.

Whilst waiting for a response I came up with checking the string length of the items called:
Expand|Select|Wrap|Line Numbers
  1. 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.......

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $arr = array($address, $telephone number, $dob);
  3. foreach ($arr as &$value) {
  4.     count =0;
  5.     $value = strlen($arr);
  6.     if ($value > 0){
  7.        $count ++
  8.     }else{
  9.         $count)
  10. }
  11. // $arr is now array(2, 4, 6, 8)
  12. unset($value); // break the reference with the last element
  13. ?>
  14.  
could this work? Could you help me with the syntax?

Very much appreciated
Feb 19 '10 #3
Atli
5,058 Expert 4TB
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.
Feb 19 '10 #4
whitep8
65
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...

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. include "confun.php";
  4. $query="SELECT * FROM tbl_dev_dilligence WHERE dev_id = 1";
  5. $result=mysql_query($query);
  6. $row = mysql_fetch_assoc($result); 
  7.  
  8. $dev_contact_fname = $row['dev_contact_fname'];
  9. $dev_contact_lname = $row['dev_contact_lname'];
  10. $dev_contact2_fname = $row['dev_contact2_fname'];
  11. //$dev_contact2_lname = $row['dev_contact2_lname']; 
  12. //$dev_position = $row['dev_position'];
  13. //$dev_add1 = $row['dev_add1'];
  14. //$dev_add2 = $row['dev_add2'];
  15. //$dev_county = $row['dev_county'];
  16. //$dev_towncity = $row['dev_towncity'];
  17. //$dev_tel = $row['dev_tel'];
  18. //$dev_fax = $row['dev_fax'];
  19. //$dev_email = $row['dev_email'];
  20.  
  21.  
  22.  
  23.  
  24. $arr = array($dev_contact_fname, $dev_contact_lname, $dev_contact_fname);
  25. count = 0;
  26. foreach ($arr as $value) {
  27. $arr = strlen($value);
  28. if ($value > 0){
  29. $count ++;
  30. }
  31. echo $count;
  32. ?>  
  33.  
Feb 19 '10 #5
whitep8
65
ive a feeling its line 27.... maybe need to have a new var in there
Feb 19 '10 #6
Atli
5,058 Expert 4TB
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.
Expand|Select|Wrap|Line Numbers
  1. if (strlen($value) <= 0){
(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:
Expand|Select|Wrap|Line Numbers
  1. if (empty($value)){
Feb 20 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Stan Brown | last post by:
http://www.acad.sunytccc.edu/instruct/sbrown/stat04/qz05_.htm A table comes near the beginning of the page. I have the <td> of row 1 column 1 styled in line with border-top:none and...
39
by: fleemo17 | last post by:
I'm wondering whether it's better to leave an alt tag blank (alt=" ") or specify something like "alt='spacer'" when referring to objects that merely help the layout of the page? -Fleemo
2
by: Wendy Spray | last post by:
Hi I have an xml document that has been created from an excel spreadsheet. In the s/s there are some empty cells however these are not captured in the xml file. All that is added is for the cell...
3
by: Simon Prince | last post by:
Hi, I'm trying detect an empty cell in a datagrid when the content is posted back to the server. My code is... Dim vDgi_GridItem As DataGridItem For Each vDgi_GridItem In...
7
by: Neo Geshel | last post by:
Greetings. I have a serious problem. I have multiple sets of tables, several of which are chained more than two tables deep. That is, I have a parent, a child, and a great-grandchild table. ...
1
by: mgoold2002 | last post by:
I'm moving some code from Excel vbs into a .NET context and I'm very new to .NET. I'm trying to make this Excel vb code work in .NET. My questions are: how do I reference cells correctly using...
2
by: OceanBreeze | last post by:
Border drawn in C# Table programmatically even if several adjacent horizontal & vertical cells are empty in the table I want to programmatically have border on each and every row and column in the...
2
by: ytramblay | last post by:
Hi, I am currently working on huge files imported into Access (1.5 Go in total splitted in 12 files). It is climatic datasets, and there is emply cells. i would like to find a easy way to replace...
0
meLady
by: meLady | last post by:
Hello, I have a form called feedback which has three columns and each cell in a column has a combo box that holds (Yes/No/Empty), and I want to count each column's value for example let's supposed...
1
by: Microsoft Newsserver | last post by:
I know this is not really an asp.net issue.But I know this is the home of the experts so I thought I would ask anyway. I am trying to reduce traffic and would prefer to have empty cells empty....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.