Automatic numbering of Kirby table columns

I’ve built a five column table with some information about different organizations (there’s a bunch of them). One of those five columns (NR) is the number ID of the row. Right now those numbers can only be entered manually and when for example the first organization should be removed from the table, all the numbers have to be changed. Is it possible to have the rows number themselves automatically? So that they’re also displayed in the frontend, not just the # field in the panel.

Thanks!

				  <?php foreach($page->threecoltablebody()->toStructure() as $tableBodyItem): ?>
					<tr>
					  <td data-label="<?php echo $page->threeColTableHeadOne()->html() ?>"> <strong><?php echo $tableBodyItem->table_col_1() ?></strong></td>
					  <td data-label="<?php echo $page->threeColTableHeadTwo()->html() ?>"><?php echo $tableBodyItem->table_col_2() ?></td>
					  <td data-label="<?php echo $page->threeColTableHeadThree()->html() ?>"><?php echo $tableBodyItem->table_col_3() ?></td>
					  <td data-label="<?php echo $page->threeColTableHeadFour()->html() ?>"><?php echo $tableBodyItem->table_col_4() ?></td>				  
					</tr>
				  <?php endforeach ?>
				</table>

Frontend:

<?php echo $page->threeColTableHeadOne()->html() ?> <?php echo $page->threeColTableHeadTwo()->html() ?> <?php echo $page->threeColTableHeadThree()->html() ?> <?php echo $page->threeColTableHeadFour()->html() ?>

Welcome!

Just set a variable like $nr = 0 and then in the td cells for the NR column, you can just add one to it in your loop and echo it with <?= $nr++ ?>. See here.

<?php 
$nr = 0;
foreach($page->threecoltablebody()->toStructure() as $tableBodyItem): ?>
<tr>
<td> <?= $nr++ ?> </td>
<td data-label="<?php echo $page->threeColTableHeadOne()->html() ?>"> <strong><?php echo $tableBodyItem->table_col_1() ?></strong></td>
<td data-label="<?php echo $page->threeColTableHeadTwo()->html() ?>"><?php echo $tableBodyItem->table_col_2() ?></td>
<td data-label="<?php echo $page->threeColTableHeadThree()->html() ?>"><?php echo $tableBodyItem->table_col_3() ?></td>
<td data-label="<?php echo $page->threeColTableHeadFour()->html() ?>"><?php echo $tableBodyItem->table_col_4() ?></td>				  
</tr>
<?php endforeach ?>
1 Like

Alternative without an additional variable:

<?php
$table = $page->threecoltablebody()->toStructure();
foreach ($table as $tableBodyItem): ?>
<tr>
  <td><?= $table->indexOf($tableBodyItem) ?></td>
  <td data-label="<?php echo $page->threeColTableHeadOne()->html() ?>"> <strong><?php echo $tableBodyItem->table_col_1() ?></strong></td>
  <td data-label="<?php echo $page->threeColTableHeadTwo()->html() ?>"><?php echo $tableBodyItem->table_col_2() ?></td>
  <td data-label="<?php echo $page->threeColTableHeadThree()->html() ?>"><?php echo $tableBodyItem->table_col_3() ?></td>
  <td data-label="<?php echo $page->threeColTableHeadFour()->html() ?>"><?php echo $tableBodyItem->table_col_4() ?></td>				  
</tr>
<?php endforeach ?>
1 Like

I ended up using texnice’s solution but thanks for the quick response! I guess I’m not acquainted enough with PHP yet, what a simple solution :slight_smile:

No worries, glad you got it going. After 3+ years I still have not memorised all of the little helpers Kirby has. @texnixe has the golden sock award for that :slight_smile: