我有這個函數給循環的每個項目一個特定的數字。簡化一個使用計數器的php函數
<?php $counter =0; ?>
<?php while(have_rows('profs_associes')): the_row(); ?>
<?php
++$counter;
if($counter == 1) {$imageclass = 'data-contentid="1"';}
if($counter == 2) {$imageclass = 'data-contentid="2"';}
if($counter == 3) {$imageclass = 'data-contentid="3"';}
if($counter == 4) {$imageclass = 'data-contentid="4"';}
if($counter == 5) {$imageclass = 'data-contentid="5"';}
if($counter == 6) {$imageclass = 'data-contentid="6"';}
if($counter == 7) {$imageclass = 'data-contentid="7"';}
if($counter == 8) {$imageclass = 'data-contentid="8"';}
if($counter == 9) {$imageclass = 'data-contentid="9"';}
if($counter == 10) {$imageclass = 'data-contentid="10"';}
?>
<div class="img-row" <?php echo $imageclass; ?>>
但似乎它可以簡化(我做了很多的話)可以通過寫一個功能設置項作爲所述項目的的「身份證」的數量。 任何人都可以幫忙嗎?
看看這給你任何想法:http://stackoverflow.com/questions/4676417/should-i-use-curly-brackets-or-concatenate-variables-within-string s – mustaccio
請更具體地解釋你想要完成什麼以及問題是什麼。請閱讀關於如何[問]好問題和Jon Skeet的博客文章[編寫完美問題]的建議(http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect- question.aspx)。請特別注意「黃金法則」,但我強烈建議您閱讀整篇文章。 –