-1

what is the better way to combine these code into one? They have the same class, id have the same prefix name, use one, two , three to differentiate them. I am thinking about using something like, array=('one','two','three'), but then, it will become array[0],array[1], still cannot make them become one. Appreciate your help.

<div class="user_voting" id="user_diet_one">
		<div class="vote_title" id="diet_one"><span class="title_text">one:</span>
			
		</div><!-- vote_title-->
		
		<div class="my_vote_body" id="my_vote_one"> 
		<input type="button" class="user_save_button" value="Save"/>
					
	</div><!-- my_vote_body-->
    </div><!-- user_voting one-->

<div class="user_voting" id="user_diet_two">
		<div class="vote_title" id="diet_two"><span class="title_text">two:</span>
			
		</div><!-- vote_title-->
		
		<div class="my_vote_body" id="my_vote_two"> 
		<input type="button" class="user_save_button" value="Save"/>
					
	</div><!-- my_vote_body-->
    </div><!-- user_voting two-->


<div class="user_voting" id="user_diet_three">
		<div class="vote_title" id="diet_three"><span class="title_text">three:</span>
			
		</div><!-- vote_title-->
		
		<div class="my_vote_body" id="my_vote_three"> 
		<input type="button" class="user_save_button" value="Save"/>
					
	</div><!-- my_vote_body-->
    </div><!-- user_voting-->

4
  • 4
    Have you at least tried something? Commented Apr 9, 2015 at 20:04
  • 2
    If your purpose is simply to vote on something, I would use either radio buttons or a dropdown box so they can select their option (or options) and submit it as a form. That'll make it simple and shorten it. It also gives them a chance to change their mind prior to submitting. Commented Apr 9, 2015 at 20:07
  • Have a look at this: stackoverflow.com/a/13199303/1837329 Commented Apr 9, 2015 at 20:08
  • I edit the question again, I hope it will not people misunderstand it again. Thanks for you time anyway. Commented Apr 9, 2015 at 20:18

1 Answer 1

0

Try this:

<?php

$arr = Array("one", "two", "three");

foreach ($arr as $key)
{
?>
<div class="user_voting" id="user_diet_"<?php echo $key; ?> >
<div class="vote_title" id="diet_"<?php echo $key; ?>><span class="title_text"><?php echo $key; ?>:</span>

    </div>

    <div class="my_vote_body" id="my_vote_"<?php echo $key; ?>> 
    <input type="button" class="user_save_button" value="Save"/>

</div>
</div>
<?php
}
?>

This way there is lesser code since you make use of array but it may still not be an optimal solution though depending on what you're trying to do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.