Maybe I'm having a bad day, but I can't seem to combine these 2 conditions into 1. They obviously should be, because they output the exact same data. The following code creates a list with A-Z to act as anchor links, and is then meant to traverse an array in the format 'word'=>'definition'
. I wanted the PHP to output a h3 element with the current letter everytime it detected a new letter.
Here is what I have
$letters = range('A', 'Z');
echo '<em>Jump to:</em><ul class="letters">';
// anchors
foreach ($letters as $letter) {
echo '<li><a href="#botany-words-' . $letter . '">' . $letter . '</a></li>';
}
echo '</ul>';
// start listing them!
$currentLetter = 0;
echo '<dl id="botany-words">';
foreach($content as $botanyWord=>$definition) {
if ($botanyWord == key(array_slice($content, 0, 1))) { // must be first, aka 'A'
echo '<h3 id="botany-words-' . $letters[$currentLetter] . '">' . $letters[$currentLetter] . '</h3>';
}
if (substr($botanyWord, 0, 1) != $letters[$currentLetter]) { // if the first character of this botany word is different to the last
echo '<h3 id="botany-words-' . $letters[$currentLetter] . '">' . $letters[$currentLetter] . '</h3>';
$currentLetter++;
}
?>
<dt><?php echo $botanyWord; ?></dt>
<dd><?php // echo $definition; ?></dd>
<?
}
echo '</dl>';
Everytime I tried to do
if ($botanyWord == key(array_slice($content, 0, 1)) || substr($botanyWord, 0, 1) != $letters[$currentLetter])
it didn't work right, and error'd because it overflowed the 26th index (obviously being 26 chars in the alphabet).
Thank you for your assistance!