php - What is the better way to combine this html into one by using array or variable? -


what better way combine these code one? have same class, id have same prefix name, use one, 2 , 3 differentiate them. thinking using like, array=('one','two','three'), then, become array[0],array[1], still cannot make them become one. appreciate 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-->

try this:

<?php  $arr = array("one", "two", "three");  foreach ($arr $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 lesser code since make use of array may still not optimal solution though depending on you're trying do.


Popular posts from this blog