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"

Recommended Answers

All 4 Replies

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¢.

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;

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.

Brilliant thank you so much for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.