The close of the if, div, and list item also needs to be moved or the formatting of the second and third rows moves outside the container. Here's the correct code:
<?php
// test if sidebox should display
if (EZPAGES_STATUS_SIDEBOX == '1' or (EZPAGES_STATUS_SIDEBOX== '2' and (strstr(EXCLUDE_ADMIN_IP_FOR_MAINTENANCE, $_SERVER['REMOTE_ADDR'])))) {
if (isset($var_linksList)) {
unset($var_linksList);
} ?>
<li class="nav-item dropdown d-lg-none">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<?php echo BOX_HEADING_EZPAGES; ?>
</a>
<div class="dropdown-menu mb-2" aria-labelledby="navbarDropdown">
<?php
echo '<ul class="m-0 p-0">';
$page_query = $db->Execute("select * from " . TABLE_EZPAGES . " where status_sidebox = 1 and sidebox_sort_order > 0 order by sidebox_sort_order, pages_title");
if ($page_query->RecordCount()>0) {
$title = BOX_HEADING_EZPAGES;
$box_id = 'ezpages';
$rows = 0;
while (!$page_query->EOF) {
$rows++;
$page_query_list_sidebox[$rows]['id'] = $page_query->fields['pages_id'];
$page_query_list_sidebox[$rows]['name'] = $page_query->fields['pages_title'];
$page_query_list_sidebox[$rows]['altURL'] = "";
switch (true) {
// external link new window or same window
case ($page_query->fields['alt_url_external'] != ''):
$page_query_list_sidebox[$rows]['altURL'] = $page_query->fields['alt_url_external'];
break;
// internal link new window
case ($page_query->fields['alt_url'] != '' and $page_query->fields['page_open_new_window'] == '1'):
$page_query_list_sidebox[$rows]['altURL'] = (substr($page_query->fields['alt_url'],0,4) == 'http') ?
$page_query->fields['alt_url'] :
($page_query->fields['alt_url']=='' ? '' : zen_href_link($page_query->fields['alt_url'], '', ($page_query->fields['page_is_ssl']=='0' ? 'NONSSL' : 'SSL'), true, true, true));
break;
// internal link same window
case ($page_query->fields['alt_url'] != '' and $page_query->fields['page_open_new_window'] == '0'):
$page_query_list_sidebox[$rows]['altURL'] = (substr($page_query->fields['alt_url'],0,4) == 'http') ?
$page_query->fields['alt_url'] :
($page_query->fields['alt_url']=='' ? '' : zen_href_link($page_query->fields['alt_url'], '', ($page_query->fields['page_is_ssl']=='0' ? 'NONSSL' : 'SSL'), true, true, true));
break;
}
// if altURL is specified, use it; otherwise, use EZPage ID to create link
$page_query_list_sidebox[$rows]['link'] = ($page_query_list_sidebox[$rows]['altURL'] =='') ?
zen_href_link(FILENAME_EZPAGES, 'id=' . $page_query->fields['pages_id'] . ($page_query->fields['toc_chapter'] > 0 ? '&chapter=' . $page_query->fields['toc_chapter'] : ''), ($page_query->fields['page_is_ssl']=='0' ? 'NONSSL' : 'SSL')) :
$page_query_list_sidebox[$rows]['altURL'];
$page_query_list_sidebox[$rows]['link'] .= ($page_query->fields['page_open_new_window'] == '1' ? '" target="_blank' : '');
$page_query->MoveNext();
}
$title_link = false;
$var_linksList = $page_query_list_sidebox;
}
for ($i=1, $n=sizeof($var_linksList); $i<=$n; $i++) {
echo '<a class="dropdown-item" href="' . $var_linksList[$i]['link'] . '">' . $var_linksList[$i]['name'] . '</a>';
} // end FOR loop
echo '</ul>';
?>
</div>
</li>
<?php } // test for display ?>