Let me see if I am track here:
You have a hidden input on your page and you have it set to one value when the page first loads then you want to change it based on some input by a user. Is this after they hit submit, or does it need to change dynamically. If its after they hit submit I think you could use this
at the start set $hiddeninput = "";
then wherever you have the hidden input
<input type="hidden" name="name" value="<?php print $hiddeninput1; ?>">
Now after they hit submit you say you are looping through some criteria and wherever you meet your criteria just set $hiddeninput1 = to whatever you need to now when the page loads the hidden input will have the value you need. Let me know if this what you are after?