如何在PHP中从1开始生成序列号并将其插入表中?
我想在每个循环的总元素(如何在PHP中从1开始生成序列号并将其插入表中?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想在每个循环的总元素()后面插入一个序列号,从1开始,然后到2,3,4 为了更加清晰,它应该对所有项目进行编号。这是代码,下面是我试图达到的图像 <tr class="order-total recurring-total"> <?php if ( $display_th ) : $display_th = false; ?> <th rowspan=""><?php esc_html_e( 'Recurring Total',
)后面插入一个序列号,从1开始,然后到2,3,4
为了更加清晰,它应该对所有项目进行编号。这是代码,下面是我试图达到的图像
<tr class="order-total recurring-total">
<?php if ( $display_th ) : $display_th = false; ?>
<th rowspan=""><?php esc_html_e( 'Recurring Total', 'woocommerce-subscriptions' ); ?></th>
<td data-title="<?php esc_attr_e( 'Recurring Total', 'woocommerce-subscriptions' ); ?>"><?php wcs_cart_totals_order_total_html( $recurring_cart ); ?></td>
<?php else : ?>
<th rowspan=""><?php esc_html_e( 'Recurring Total', 'woocommerce-subscriptions' ); ?></th>
<td><?php wcs_cart_totals_order_total_html( $recurring_cart ); ?></td>
<?php endif; ?>
</tr>
您需要使用for循环并打印循环中的表行
<?php for($i = 1; $i <= $n ; $i++) { ?>
<tr class="order-total recurring-total">
<?php if ( $display_th ) : $display_th = false; ?>
<th rowspan=""><?php esc_html_e( 'Recurring Total', 'woocommerce-subscriptions' ); ?></th>
<td data-title="<?php esc_attr_e( 'Recurring Total', 'woocommerce-subscriptions' ); ?>"><?php wcs_cart_totals_order_total_html( $recurring_cart ); ?></td>
<?php else : ?>
<th rowspan=""><?php esc_html_e( 'Recurring Total', 'woocommerce-subscriptions' ); ?></th>
<td><?php wcs_cart_totals_order_total_html( $recurring_cart ); ?></td>
<?php endif; ?>
</tr>
<?php } ?>
如果我理解正确,请在打印表行的循环之前创建一个变量:$counter=1
然后只需将其打印到表中所需的位置:
。这将打印当前值并增加一个值。非常感谢,效果很好!也许我会问,是否有类似的简单方法来显示数字,如1/3、2/3……只要你知道总数,就加上它:
不,我不知道总数,但在循环解决问题之前,这个$total=count($carts)。再次感谢您的帮助,非常感谢!你的for循环中有一个输入错误。它应该是$i
<?php echo $i ?>