I am creating a simple one-page web page and wish to use a for loop to output the navigation links. I am having an issue in pairing the stage number ($x
) to a string.
The final output is expected to be/should be:
- Stage 1, Victoria Bridge, Leeds
- Stage 2, Second Stage Location
- Stage 3, Third Stage Location
- Stage 4, Any Other
- Stage 5, Any Other
However, the output is
- Stage 1, Third Stage Location
- Stage 2, Third Stage Location
- Stage 3, Third Stage Location
- Stage 4, Any Other
- Stage 5, Any Other
And I am not too sure where I have gone wrong.
I have tried using if/else statements as shown below.
<!-- vertical nav -->
<nav class="v-nav p-4">
<p class="title">Stage</p>
<ul>
<?php
for ($x = 1; $x <= 21; $x++) {
if ($x = 1) {
$place = 'Victoria Bridge, Leeds';
} else if ($x = 2) {
$place = 'Victoria Bridge 2, Leeds';
} else {
$place = 'test'
}
?>
<li>
<a href="#stage<?php echo $x ?>" data-number="<?php echo $x ?>">
<span class="label">Stage <?php echo $x ?> - <?php echo $place ?></span>
<span class="dot"></span>
</a>
</li>
<?php } ?>
</ul>
</nav>
It caused $x
to repeat past 21 times for some reason and prevent the load of all the other page elements. Below is what I currently have using ternary operators and can't seem to figure out where the flaw is.
<!-- vertical nav -->
<nav class="v-nav p-4">
<p class="title">Stage</p>
<ul>
<?php for ($x = 1; $x <= 21; $x++) { ?>
<li>
<a href="#stage<?php echo $x ?>" data-number="<?php echo $x ?>">
<span class="label">
<?php
$navLink = 'Stage '.$x.', '.(
$x == 1 ? 'Victoria Bridge, Leeds' :
$x == 2 ? 'Second Stage Location' :
$x == 3 ? 'Third Stage Location' : 'Any Other');
echo $navLink;
?>
</span>
<span class="dot"></span>
</a>
</li>
<?php } ?>
</ul>
</nav>
Kind Regards