<?php 
$result4 = $mysqli->query("SELECT insert_id, page, name, title, text, image FROM sections WHERE parent=$id AND lang_id=$lang AND publish='yes' ORDER BY priority");
while($row = $result4->fetch_array()) {	
$secid1=$row['insert_id'];
$pagesection1=$row['page'];
$secname=$row['name'];
$secdesc=$row['title'];
$cimage=$row['image'];
if (!$secdesc) { 
$secdesc=$secname;
}
$catt=$row['text'];
$photopath="images/sections/";
?>
<div class="boxcat boxcat2"><div class="boxincat">
<div class="boximage-cat colorlight"><?php if ($cimage!=0 AND (file_exists("./".$photopath.$secid1.".jpg"))) { ?><img src="<?php echo $sitepath ?>includes/image.php?image=../<?php echo $photopath ?><?php echo $secid1 ?>.jpg&amp;nwidth=400" class="bordercolor1 img-icat" alt="<?php echo $secname ?>" title="<?php echo $secdesc ?>"><?php }?></div>
<div class="boxcattitles color1"><?php echo $iconsec ?> <?php echo $secname ?></div>
<div class="boxcattext boxcattext2 color1h"><?php echo $catt ?></div>
<div class="boxsections">
<div class="titlesnav titlescat"><?php echo $sectionstext ?></div>
<?php 
$result21 = $mysqli->query("SELECT insert_id, page, name FROM categories WHERE level=1 AND insert_id>1 AND lang_id=$lang AND publish='yes' ORDER BY priority");
while($row = $result21->fetch_array()) {
$mcatid1=$row['insert_id'];
$mpage1=$row['page'];
$mcategory1=$row['name'];

$result22 = $mysqli->query("SELECT COUNT(id) FROM items WHERE category_id='$mcatid1' AND section_id='$secid1' AND lang_id=$lang AND publish='yes'");
$rows = $result22->fetch_row();
$nrrows22 = $rows[0];

$result23 = $mysqli->query("SELECT COUNT(id) FROM items WHERE category_id IN (SELECT insert_id FROM categories WHERE insert_id='$mcatid1' OR parent='$mcatid1') AND section_id>0 AND lang_id=$lang AND publish='yes'");
$rows = $result23->fetch_row();
$nrrows23 = $rows[0];

$result24 = $mysqli->query("SELECT COUNT(id) FROM items WHERE category_id IN (SELECT insert_id FROM categories WHERE parent='$mcatid1') AND section_id='$secid1' AND lang_id=$lang AND publish='yes'");
$rows = $result24->fetch_row();
$nrrows24 = $rows[0];
?>
<?php if ($nrrows23>0) {?>
<div>
<?php if ($nrrows22>0) {?>
<div class="titlescat"><a href="<?php echo $pagesection1 ?>.<?php echo $mpage1 ?>" class="color1"><?php echo $mcategory1 ?></a></div>
<?php }else{?>
<span class="titlescat color2<?php if ($nrrows24==0) { echo ' colornlight'; }?>">
<?php if ($nrrows24==0) {?><?php echo $mcategory1 ?><?php }else{?><a href="<?php echo $pagesection1 ?>.<?php echo $mpage1 ?>" class="color2"><?php echo $mcategory1 ?></a><?php } ?>
</span>
<?php } ?>
<?php 
$result26 = $mysqli->query("SELECT insert_id, page, name FROM categories WHERE parent='$mcatid1' AND lang_id=$lang AND publish='yes' ORDER BY priority");
while($row = $result26->fetch_array()) {
$mcatid2=$row['insert_id'];
$mpage2=$row['page'];
$mcategory2=$row['name'];

$result27 = $mysqli->query("SELECT COUNT(id) FROM items WHERE category_id='$mcatid2' AND section_id='$secid1' AND lang_id=$lang AND publish='yes'");
$rows = $result27->fetch_row();
$nrrows27 = $rows[0];
?>
<?php if ($nrrows27>0) {?>
<a href="<?php echo $pagesection1 ?>.<?php echo $mpage2 ?>" class="linkss color1">&raquo; <?php echo $mcategory2 ?></a>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
<?php } ?>
</div>
</div></div>  
<?php }?><div class="cleared"></div>