Simplify processing of form data

I have created an online registration form for an event based on the Event Kit. Among other things, users can select 4 different program items and optionally 2 alternative menus

I save these values - from 4 checkboxes and 2 radio buttons - in 2 fields.

Eventprogrampart:

- "1"
- "2"
- "4"

----

Eventmenu: - "1"

----

I then “decode” these data in a template, that shows the organizer a table of registrations:

<tbody>
	<?php foreach($registrations as $registration): ?>
		<?php $topics = implode(',', $registration->eventProgramPart()->toArray()); ?>
		<tr class="border-b border-stone-400">
		...
			<td class="text-center"><?php e(str_contains($topics, '1'),'1','') ?></td>
			<td class="text-center">
				<?php
					if (str_contains($topics, '2')) {
						$dishes = implode(',', $registration->eventMenu()->toArray());
						if (str_contains($dishes, '1')) {	
							echo 'Fleisch';
						}
						else {
							echo 'vegetarisch';
						}
					}
				?>
			</td>
			<td class="text-center"><?php e(str_contains($topics, '3'),'1','') ?></td>
		...
		</tr>
	<?php endforeach ?>
</tbody>

then again in a template for an email, that the user receives as confirmation

<?php
	$topics = implode(',', $meventProgramPart);
	if (str_contains($topics, '1')) {
		echo '- Teilnahme an der Exkursion am Freitag, 9. Mai 2025' . '<br>';
	}
	if (str_contains($topics, '2')) {
		$dishes = implode(',', $meventMenu);
		if (str_contains($dishes, '1')) {	
			echo '- Teilnahme am Nachtessen am Freitag, 9. Mai 2025' . ', Menü Fleisch<br>';
		}
		else {
			echo '- Teilnahme am Nachtessen am Freitag, 9. Mai 2025' . ', Menü vegetarisch<br>';
		}
	}
	if (str_contains($topics, '3')) {
		echo '- Teilnahme am Landeskonvent am Samstag, 10. Mai 2025' . '<br>';
	}
	if (str_contains($topics, '4')) {
		echo '- Teilnahme am Mittagessen am Samstag, 10. Mai 2025' . '<br>';
	}
?>

and finally in a route that allows the organizer to download all registrations as a CSV file:

...
foreach ($registrations as $registration) {
	$topics = implode(',', $registration->eventProgramPart()->toArray());
	...
	$exkursion = (str_contains($topics, '1') ? '1' : '');
	$nachtessen = (str_contains($topics, '2') ? '1' : '');
	$konvent = (str_contains($topics, '3') ? '1' : '');
	$mittagessen = (str_contains($topics, '4') ? '1' : '');
	...
	$registrationData = array($nachname, $vorname, $landesgruppe, $exkursion, $nachtessen, $konvent, $mittagessen, $buchung);
	array_push($list, $registrationData);
}
...

Since it had to be done very quickly, this solution is quite “hacky”, mixes a lot of PHP logic with HTML and contradicts all the rules of “Don’t repeat yourself”. Does anyone have an idea how I can do the “decoding” only once and then use the resulting values in template, email and CSV file?

Answer to myself: RTFM, think again and extend the controller to update the page with the desired “decoded” fields after the children page for the registration is created. Use these fields in the email, the table of registrations and the CSV file without the need to use intermangled PHP logic any more.