hey guys.

okay, I know how to count form values.

something like;

<?php echo count($_POST; ?> where the field would be something like field

however, I am cracking my head to get a solution to count, but only where the field has a value.

Please, someone, help me with this ;-)

I tried !isset($_POST) but it does not work :-(

Recommended Answers

All 6 Replies

Member Avatar for diafol

Whadda you mean Willis? You wanna know which keys are set?

I have got several text boxes in a form. when I post these textboxes, I simply just want to count / have the total of how many textboxes contains data OR how many was elft empty. thats it...

Member Avatar for diafol
<input type="checkbox" name="monkey[]" id="clegg" />
<input type="checkbox" name="monkey[]" id="brown" />
<input type="checkbox" name="monkey[]" id="cameron" />

these create a form field array

Get the count via:

count($_POST['monkey']);
commented: LOL - excellent topical naming :) +20

No, it is textfields, not checkboxes. Checkboxes acts totally different than to other fields. Managing and extracting data from checkboxes is easy.

However, I got a solution.

Basically, all I wanted is to have a total in regards to how many textboxes contains data, after form submittal. But it is far more complex than I thought. Exactly 15 hours and 48 minutes of constant working, I got it working. I have search the entire net, and no where I could find a proper solution. My only solution was to physically puzzle it out. Change lines of code, move lines, and try every possible count.

Somewhere on a post I ready something about someone who asked about a SQL statement containing "IN (xxx,xxx)" but noboy could give more clarity on that. I also realised that if I want to achieve something that I have never done before (yes I did many checkboxes but as said, thats something totally different) - then I will need to try something that I have never tried before.

All I can say is that it was kinda torture and hard work, but, the end result was worth it. And I am sharing, for if someone would be in the same situation.

Here it is:

foreach ($_POST as $key => $value)
{
if ($value != '')
{
	$finale = count(array_keys($_POST, ""));
}
}
$finalevalue = (count($_POST)-$finale)-2;

I used minus two, because I am not posting the textboxes only, but two other values too.
You can, however, use a loop and post the data as $_POST[$i] instead. I did that during testing but my form required more complex detail.

So basically, to get the total textboxes containing values, I take the total values posted, and minus it with the empty values.

Then - the reason why I needed this, is because it am building a filter application for some site. So I'll post my SQL statement here too, but not going to elaborate too much, because its not relevant.

foreach ($_POST as $key => $value)
{
if ($value != '')
{
	$finale = count(array_keys($_POST, ""));
}
}
$finalevalue = (count($_POST)-$finale)-2;
	 
if (!isset($_POST['f1'])) { $f1 = ''; } else if ($_POST['f1'] != 'All') { $f1 = "'".$_POST['f1']."',"; } else $f1 = '';
if (!isset($_POST['f2'])) { $f2 = ''; } else if ($_POST['f2'] != 'All') { $f2 = "'".$_POST['f2']."',"; } else $f2 = '';

("SELECT * FROM item RIGHT JOIN itemfield ON (item.adID = itemfield.adID) WHERE Status = 'Active' AND item.catID = %s AND FieldValue IN (".$f1."".$f2.") GROUP BY item.adID HAVING COUNT(FieldValue)=".$finalevalue." ORDER BY item.itemID DESC", GetSQLValueString($colname_listads, "int"));

f1/f2 is form values, used with a loop. So in truth its something like input name = "f<?php echo $i; ?>"
Then, I join my item with my itemfields, cause the itemfield column containst the itemID too.

Then this part : FieldValue IN (".$f1."".$f2.")

Why this? This is weird, but apparently you cannot do this : WHERE FieldValue = 'valueone' AND FieldValue = 'valuetwo' AND FieldValue = 'valuethree'. I dont know why, but instead one must use the "IN" method. Its jsut strange, because when I work with times or dates or prices, I can do something like "WHERE Price <= 100 AND Price >= 1000" - well something like that. And it works, but I guess it is because I used a join fucntion and/or group by, thats why it did not work.

Nevertheless, it feels like as if it took me a million years and loads of energy to crack.

Hope I could've helped someone at least.

forgot to mention; I used GROUP BY to eliminate duplicate entries as a result from the JOIN statement.

Member Avatar for diafol

I know the thread is solved - sorry missed the 'textbox' bit.

I didn't really follow what you're trying to do, but you can use a similar concept to the checkbox solution:

<input type="text" name="monkey['clegg']" id="clegg" value="" />
<input type="text" name="monkey['brown']" id="brown" value="" />
<input type="text" name="monkey['cameron']" id="cameron" value="" />

Check the $_POST array for specific data. If what you want, add the key as the fieldname (or whatever you're trying to do) and the value to the SQL.

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.