<?php
if ($lastChar !== ($currentChar = mb_convert_case(mb_substr($row['category_name'], 0, 1), CASE_UPPER))):
?>
<?php
$lastChar = $currentChar;
?>
<h1 class="bukva" id="bukva_<?= mb_convert_case($lastChar, CASE_LOWER); ?>"><?= $lastChar; ?></h1>
<?php
endif;
?>
<?php
foreach ($conndb->query($sql) as $row):
?>
<a href="http://localhost/menu_general/masters_page/masters/masters.php">
<?php
$lastChar = "А";
?>
<div class="kategoriya">
<div class="kolichestvo">0</div>
<?php echo $row['category_name'] . "\t"; ?>
</div>
</a>
<?php
endforeach;
?>
так выводит:<?php
$lastChar = "А";
foreach ($conndb->query($sql) as $row):
?>
<a href="http://localhost/menu_general/masters_page/masters/masters.php">
<?php if ($lastChar !== ($currentChar = mb_convert_case(mb_substr($row['category_name'], 0, 1), CASE_UPPER))): ?>
<?php $lastChar = $currentChar; ?>
<h1 class="bukva" id="bukva_<?= mb_convert_case($lastChar, CASE_LOWER); ?>"><?= $lastChar; ?></h1>
<?php endif; ?>
</a>
<?php
endforeach;
?>
<?php
foreach ($conndb->query($sql) as $row):
?>
<a href="http://localhost/menu_general/masters_page/masters/masters.php">
<div class="kategoriya">
<div class="kolichestvo">0</div>
<?php echo $row['category_name'] . "\t"; ?>
</div>
</a>
<?php
endforeach;
?>