Count the number of values that dont have zero as a value and then insert or update

I have around 12 dropdowns that have a value of zero if nothing is selected and then a value of 1+ if something is selected as below.

<select name="foodcheck" size="1">
<option value="0">N/A</option>
<option <?php if($FoodCheckName == "1") echo "selected"; ?> value="1">Monthly</option> 
<option <?php if($FoodCheckName == "2") echo "selected"; ?> value="2">Every 2 Months</option>
<option <?php if($FoodCheckName == "3") echo "selected"; ?> value="3">Quarterly</option> 
<option <?php if($FoodCheckName == "4") echo "selected"; ?> value="4">Twice A Year</option> 
<option <?php if($FoodCheckName == "5") echo "selected"; ?> value="5">Yearly</option> 
<option <?php if($FoodCheckName == "6") echo "selected"; ?> value="6">CS</option>
</select>

<select name="supplycheck" size="1">
<option value="0">N/A</option> 
<option <?php if($SupplyCheckName == "1") echo "selected"; ?> value="1">Monthly</option> 
<option <?php if($SupplyCheckName == "2") echo "selected"; ?> value="2">Every 2 Months</option>
<option <?php if($SupplyCheckName == "3") echo "selected"; ?> value="3">Quarterly</option> 
<option <?php if($SupplyCheckName == "4") echo "selected"; ?> value="4">Twice A Year</option> 
<option <?php if($SupplyCheckName == "5") echo "selected"; ?> value="5">Yearly</option> 
<option <?php if($SupplyCheckName == "6") echo "selected"; ?> value="6">CS</option> 
</select>

So what I would like to do is on click of the submit button that I can tally up or count the number of dropdowns that have a value greater than zero, and then insert that number into the correct field in the database.

In the php code, can’t you just loop through each field name, check the value and insert as appropriate, or have I misunderstood the question?

When you say tally up, are you just asking for the value of selected option?

$foodcheck = (isset($_POST['foodcheck']) && $_POST['foodcheck'] < 0 ? $_POST['foodcheck'] : '');

Evening guys,

This is what I’m thinking.

As the user skips through the drop downs he is making a selection, so if he selects anything but 0 then its a hit in so many words, what I need to do is once the user clicks the submit button after making his selections, it works out how many of the drop downs are more than the value of zero. Then that number is stored in a variable and in the insert statement inputted into the database in its correct field.

So if there 20 drop downs, and he selects 18 of the value ‘Monthly’ but leaves 2 as N/A, then the count will equal 18.

Thanks for getting back to me

Probably something along these lines.

<?php
//list different select option names
$options = array('foodcheck','supplycheck');
$cnt = 0;
foreach($options as $option):
    if(!empty($_POST[$option])):
        $cnt++;
    endif;
endforeach;
?>

Hi Drummin,

I understand the listing of the different select options, but will that be able to tell what each selects value is, and so if more than 0 add it to a count.

I would think so as zero is considered empty. Give it a go.

You could also replace that condition line with this.

if(isset($_POST[$option]) && $_POST[$option] > 0):

I’ll try it first thing and get back to you, thanks again.

Hi Drummin,

OK have added it in as below -

But I think I don’t need this first bit anymore, as it seems your ways deals with it instead -

$BrandCheckNameb=$_POST['brandcheck'];
$StandardsCheckNameb=$_POST['standardscheck'];
$RoomCheckNameb=$_POST['roomcheck'];
$FoodCheckNameb=$_POST['foodcheck'];

So have cancelled those out and using yours below

$options = array('brandcheck','standardscheck','roomcheck','foodcheck','supplycheck');
$cnt = 0;
foreach($options as $option):
if(!empty($_POST[$option])):
$cnt++;
endif;
endforeach;

$nModulesb=$option;

$query="update Intranet set No_of_Modules='$nModulesb' where ID=$sr";

I don’t think I’m getting this right as getting an error

Error in executing query.
Array ( [0] => Array ( [0] => 22018 [SQLSTATE] => 22018 [1] => 245 [code] => 245 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Conversion failed when converting the varchar value ‘supplycheck’ to data type int. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Conversion failed when converting the varchar value ‘supplycheck’ to data type int. ) )

Surely it would be

$query="update Intranet set No_of_Modules='$cnt' where ID=$sr";

Assuming you want to store the count of how many fields contained something other than zero, and assuming you’ve figured out $sr from somewhere? This line

$nModulesb=$option;

will just contain the name of the final dropdown, left over from the foreach() loop.

Yes your right sorry, I wasn’t reading it properly.

That works fine, and so thank you all.

Hi guys, I got one little issue that has arisen.

I’m not going to be able to use that code when reading the values from the database to display when a contract is clicked to view all its detail.

The code above worked well when inserting a new contract in first time, and also editing an existing contract but when you click to view an existing contract I need for it to read the values from straight from the database.

if(isset($_GET["ID"]))
{        
$BrandCheckName=$rows["Brandcheck_Frequency"];
$StandardsCheckName=$rows["StandardsCheck_Frequency"];
$RoomCheckName=$rows["Roomcheck_Frequency"];
$FoodCheckName=$rows["Foodcheck_Frequency"];
$CrisisCheckName=$rows["Crisischeck_Frequency"];
$PoolCheckName=$rows["Poolcheck_Frequency"];
$FireCheckName=$rows["Firecheck_Frequency"];
$AquaCheckName=$rows["Aquacheck_Frequency"];
$SpaCheckName=$rows["Spacheck_Frequency"];
$SafetyCheckName=$rows["Safetycheck_Frequency"];
$AccessCheckName=$rows["Accesscheck_Frequency"];
$EcoCheckName=$rows["Ecocheck_Frequency"];
$SupplyCheckName=$rows["Supplycheck_Frequency"];
$DineCheckName=$rows["Dinecheck_Frequency"];
$TourCheckName=$rows["Tourcheck_Frequency"];
$TrainingName=$rows["Training_Days"];
$LCAquaName=$rows["Labcheck_Aqua_Frequency"];
$LCPoolName=$rows["Labcheck_Pool_Frequency"];
$LCRoomName=$rows["Labcheck_Room_Frequency"];
$LegionellaName=$rows["Legionella_Test_Frequency"];

$options = array('Brandcheck_Frequency','StandardsCheck_Frequency','Roomcheck_Frequency','Foodcheck_Frequency','Crisischeck_Frequency','Poolcheck_Frequency','Firecheck_Frequency','Aquacheck_Frequency','Spacheck_Frequency','Safetycheck_Frequency','Accesscheck_Frequency','Ecocheck_Frequency','Supplycheck_Frequency','Dinecheck_Frequency','Tourcheck_Frequency','Labcheck_Aqua_Frequency','Labcheck_Pool_Frequency','Labcheck_Room_Frequency','Legionella_Test_Frequency');
//$cnt = 0;
$nModules = 0;
foreach($options as $option):
if(!empty($_POST[$option])):
    $nModules++;
endif;
endforeach;
}

I tried doing this

$options = array('$rows["Brandcheck_Frequency"];');

But didn’t work, but also realised that the code was set up to deal with option values, so its not going to work anyway, so could you help with adapting it for this situation.

Have you tried changing the condition to $rows instead of $_POST?

$nModules = 0;
foreach($options as $option):
if(!empty($rows[$option])):
    $nModules++;
endif;
endforeach;

Sorry Drummin yes, that’s worked.

Just had a chat with the IT guy and he says that N/A which I have as equal to 0 is now equal to ‘1’ so I can no longer use the empty call, this time I’m looking to count the values that have a value greater than ‘1’, sorry about this, so something like below, but I’m bound to have it wrong.

$nModules = 0;
foreach($options as $option):
if(>1($rows[$option])):
$nModules++;
endif;
endforeach;

Thought this might do it, but didn’t work either

$nModules = 0;
foreach($options as $option):
if(count($rows[$option])>1):
    $nModules++;
endif;
endforeach;

I think I may have it if someone could say yes that’s fine, that would be great.

$nModules = 0;
foreach($options as $option):
if(($rows[$option])>'1'):
    $nModules++;
endif;
endforeach;
1 Like

That should work, though if it’s the form submission won’t you need to check $POST rather than $rows? $rows would be used when you’re displaying data, $POST is the data that comes from your form.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.