使用数组或变量将这个html组合成一个html的更好方法是什么


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

将这些代码组合为一个代码的更好方法是什么?它们有相同的类,id有相同的前缀名称,使用一、二、三来区分它们。我正在考虑使用类似array=('one','two','three')的东西,但后来,它将变成array[0],array[1],仍然无法使它们成为一个。感谢你的帮助。

<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-->

试试这个:

<?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
}
?>

这样,由于您使用了数组,所以代码较少,但它可能仍然不是最佳解决方案,这取决于您尝试做什么。

相关文章: