1

Hi,

Ive posted this in a mysql forum, but the more i think about it i think php is really where i think the solution lies.

When i call a row, how could i count the number of empty entries?

so say its an address with 5 lines, and ive only filled in 3, i want the output to simply say "2"

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by whiteyoh
0

This code is quick and dirty, definitely not the type I normally code, but I am on my way out the door and thought I would drop this before I left. May not be your solution, but it might point you in the right direction.

<?php

	$sql = "SELECT * FROM `whatever` WHERE `this`='that'";
	$result = mysql_query($sql) or die('\"'.$sql.'\" Query failed: ' . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_BOTH);
	$counter = 0;
	if(!$row['address1']) { $counter++; }
	if(!$row['address2']) { $counter++; }
	if(!$row['address3']) { $counter++; }
	if(!$row['address4']) { $counter++; }
	if(!$row['address5']) { $counter++; }
	echo $counter;
	
?>

My 2¢.

0

Hi,

Thats interesting, as since posting ive been looking at a similar "dirty solution" :-).

What i came up with was for each cell retrieved to count the length with

if (strlen($dev_contact_fname) > 0) { $count ++; } else { $count; }

which works........

BUT.....

Since then ive been looking at shortening the code and came up with the following which doesnt work yet.... so any help with the syntax would be grateful

$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;
0

First off, you'll want to make sure that the variable "count" on line 3 is actually $count. And then...

<?php

$dev_contact_fname = "Joe";
$dev_contact_lname = "";

$arr = array($dev_contact_fname, $dev_contact_lname, $dev_contact_fname);
$count = 0;
foreach ($arr as $value) {
	$arr2 = strlen($value);
	if ($arr2 > 0){
		$count ++;
	}
}
echo $count;

?>

Because our array has $dev_contact_fname twice, this comes in handy for testing. When ran as above, the output is 2. If you change the first name to "" and the last name to "Joe", then the output is 1. Code has been tested.

My 2¢. Hope it helps.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.