今天早上我的大脑似乎不想工作..我有以下数组:
private $nav_pages = [['0', 'Home', 'home'],
['0', 'About Us', 'about'],
['0', 'Our Services', 'services'],
['1', 'PROCURE TO PAY (P2P)', 'procure'],
['1', 'ORDER TO CASH (02C)', 'cash'],
['1', 'GENERAL ACCOUNTING', 'general'],
['2', 'Charting of Accounts', 'charting'],
['2', 'General Ledger Maintenance', 'maintenance'],
['2', 'Accounts Receivable Services', 'receivable'],
['2', 'Accounts Payable Services', 'payable'],
['2', 'Reconciliation of Bank Accounts and Credit Cards', 'reconciliation'],
['2', 'Preparing Financial Statements', 'statements'],
['2', 'Payroll Processing', 'payroll'],
['1', 'CLOSING & REPORTING', 'closing'],
['0', 'How It Works', 'how-it-works'],
['0', 'Contact Us','contact'],
['0', 'Careers', 'careers']];
然后将其馈送到一个 php 页面,该页面旨在布置一个多层纯 css 下拉菜单.. php 页面代码是:
<ul class="<?php echo $ul_class; ?>">
<?php $this_layer = 0;
// place array content into variables
foreach ($links as $link) {
$item_layer = $link[0];
$item_string = $link[1];
$item_link = $link[2];
if ($item_layer > $this_layer) { ?>
<ul>
<?php $this_layer++; }
elseif ($item_layer == $this_layer) { ?>
<li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
</li>
<?php }
elseif ($item_layer < $this_layer) { ?>
</li></ul>
<?php $this_layer--; } ?>
<?php } ?>
但是,输出不正确,因为上面的代码总是在第二层准备好关闭之前关闭包含第二层的列表项。如果这有意义..
<ul class="pages_nav">
<li class="home">
<a class="home"
href="/home">
Home
</a>
</li>
<li class="about">
<a class="about"
href="/about">
About Us
</a>
</li>
<li class="services">
<a class="services"
href="/services">
Our Services
</a>
</li>
<ul>
<li class="cash">
<a class="cash"
href="/cash">
ORDER TO CASH (02C)
</a>
</li>
<li class="general">
<a class="general"
href="/general">
GENERAL ACCOUNTING
</a>
</li>
<ul>
<li class="maintenance">
<a class="maintenance"
href="/maintenance">
General Ledger Maintenance
</a>
</li>
<li class="receivable">
<a class="receivable"
href="/receivable">
Accounts Receivable Services
</a>
</li>
<li class="payable">
<a class="payable"
href="/payable">
Accounts Payable Services
</a>
</li>
<li class="reconciliation">
<a class="reconciliation"
href="/reconciliation">
Reconciliation of Bank Accounts and Credit Cards
</a>
</li>
<li class="statements">
<a class="statements"
href="/statements">
Preparing Financial Statements
</a>
</li>
<li class="payroll">
<a class="payroll"
href="/payroll">
Payroll Processing
</a>
</li>
</li></ul>
</li></ul>
<li class="contact">
<a class="contact"
href="/contact">
Contact Us
</a>
</li>
<li class="careers">
<a class="careers"
href="/careers">
Careers
</a>
</li>
解决方案:
<ul class="<?php echo $ul_class; ?>">
<?php $this_layer = 0;
// place array content into variables
foreach ($links as $link) {
$item_layer = $link[0];
$item_string = $link[1];
$item_link = $link[2];
// check if link needs to be manipulated
switch($do) {
case 'strtolower':
$item_string = strtolower($item_string);
break;
case 'strtoupper':
$item_string = strtoupper($item_string);
break;
} ?>
<?php if ($item_layer > $this_layer) { ?>
<ul>
<li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php $this_layer++; }
elseif ($item_layer == $this_layer) { ?>
</li><li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php }
elseif ($item_layer < $this_layer) { ?>
</li></ul></li><li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php $this_layer--; } ?>
<?php } ?>