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.
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.
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.
$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. ) )
$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.
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.
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.
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;
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.