Page 1 of 1

flipping $_Post or input names and values

Posted: Sat Aug 19, 2006 11:10 pm
by tomisina
I feel bad for two post in one day but I've searched pretty extensively for the answer to this question elsewhere and I haven't quite found an answer. feyd tried to answer it for me but I accidently eplained it improperly... and I can't pay $500 to have him take another shot...

Basically I have multiple "suggestions" on a page and I want two buttons tied to each suggestion... a ++ button and a -- button. What is the most simple and secure way to do this? Say I have this code:

Code: Select all

<form action="index.php?page=<?php echo $page ?>" method="post">
	<?php foreach($suggestions as $key => $value){ ?>
		<div align="center" class="ranking">
			<?php echo '<input type="submit" value="'.$value['id'].'" name="++"/>'; ?>
			<div class="votes">
			     <?php
				     echo $value['rating'];
			     ?>
			</div>
			<?php echo '<input type="submit" value="'.$value['id'].'" name="++"/>'; ?>	  	</div>
	<?php } ?>
</form>
then I can just put

Code: Select all

if($_POST['++']){
	bumpSuggestion(sanitize_int($_POST['++']),1);
}
else if($_POST['--']){
	bumpSuggestion(sanitize_int($_POST['--']),-1);
}
in the header of the next page and everything will be fine... BUT (and the but of the week for me) I don't ever want the user to see the $value['id'] cause it's meaningless to them and then they wont know what the buttons do. If I switch the value and name field of the submit then I have to walk through the entire $_POST looking for one that points to ++ or -- and when the number of suggestions increases that becomes dumber and dumber... I had a moment of brilliance and thought I could just array_flip($_POST) but I dont think that works... and furthermore that'll mess with my other $_POST vars... if only i could mask over the value of those submit buttons with a different value...

there's got to be a really simple elegant solution to this common problem right?

Thanks so much.
-Tommy

Posted: Sat Aug 19, 2006 11:19 pm
by daedalus__
Use a hidden input to store the $value['id']:

Code: Select all

<form action="index.php?page=<?php echo $page ?>" method="post"> 
<input type="hidden" value="<?php echo $value['id']; ?>" />
   <?php foreach($suggestions as $key => $value)
       { ?> 
      <div align="center" class="ranking"> 
         <?php echo '<input type="submit" value="++" name="++"/>'; ?> 
         <div class="votes"> 
              <?php 
                 echo $value['rating']; 
              ?> 
         </div> 
         <?php echo '<input type="submit" value="--" name="++"/>'; ?>        </div> 
   <?php } ?> 
</form>

Posted: Sat Aug 19, 2006 11:24 pm
by feyd
Transpose your name and value attributes of your buttons.

Did this need a new thread?

Posted: Sun Aug 20, 2006 12:28 am
by tomisina
Ok say we add a hidden input to store the $value['id]... then we should probably bring that into the scope of that big foreach suggestions loop there... with that done then we'd need to bring that form into the suggestion loop as well or that hidden field would just keep the id of the first suggestion passed to it... if we want to get around that then we'd bring the </form> into the loop at the bottom and then we're successfully using 2 post vars to hold 1 key and 1 value... sooo probably not the simplest solution as that leaves us with a 2 inputs and 1 form for every suggestion... which over a lot of suggestions equals a lot more processing for the browser.... (that's versus 1 form total and 1 input for each suggestion)

If we were to transpose the name and value... essentially using the name of the submit button to hold the suggestion id then we'd be walking through the entire POST array looking for the value ++ or --... then finding the key which coresponds with that to bump it up or down... is that really the most efficient way to solve this problem?

Posted: Sun Aug 20, 2006 12:32 am
by daedalus__
tomisina wrote:Ok say we add a hidden input to store the $value['id]... then we should probably bring that into the scope of that big foreach suggestions loop there... with that done then we'd need to bring that form into the suggestion loop as well or that hidden field would just keep the id of the first suggestion passed to it... if we want to get around that then we'd bring the </form> into the loop at the bottom and then we're successfully using 2 post vars to hold 1 key and 1 value... sooo probably not the simplest solution as that leaves us with a 2 inputs and 1 form for every suggestion... which over a lot of suggestions equals a lot more processing for the browser.... (that's versus 1 form total and 1 input for each suggestion)
Too much reading for me. It's not going to hurt anything to store the id in the form.

Code: Select all

<?php foreach($suggestions as $key => $value) 
       { ?> 
<input type="hidden" value="<?php echo $value['id']; ?>" />

Posted: Sun Aug 20, 2006 12:42 am
by feyd
tomisina wrote:If we were to transpose the name and value... essentially using the name of the submit button to hold the suggestion id then we'd be walking through the entire POST array looking for the value ++ or --... then finding the key which coresponds with that to bump it up or down... is that really the most efficient way to solve this problem?
As I said before, if you name it such that PHP automatically creates an array for it, you don't have to go searching for keys. The only stored element would be the button pressed in this situation. array_keys(), array_values() and probably array_flip() may be of interest.