Code logic not computing correct data - php

I'm desperately hoping someone can assist me with this error. Admittedly, I'm a novice and I've tried for literally 60+ hours to fix this but I just can't get there.
Right, here goes.
I have a football league table of 20 teams. It works, it's great, happy days. However, I needed to reduce it to 12 teams. A problem because I didn't create the website, someone else did a while back.
So I've needed to amend a table with 20, to 12. That's all.
Daunted, I rolled up my sleeves scoured the code and located this bit;
<?php
$query = $SITE -> Query ( 'SELECT a.*, b.name, b.colour, b.txtcolour,
(sum((a.wins*3) + (a.draws*1) - a.pts)) as points,
(sum(a.wins + a.losses + a.draws)) as pld,
(sum(a.glsfor - a.glsaga)) as gd
FROM got_standings a
inner join got_clubs b on (a.cid = b.id)
WHERE a.season = "'.$url[2].'"
GROUP BY a.cid
ORDER BY points DESC, gd DESC, glsfor DESC' );
if ($SITE -> Count ($query) == 0) {
$loop = 0;
while($loop <= 19) {
++$loop;
?>
After some trial and error in those 60 hours I've been trying to resolve, I changed;
while($loop <= 19)
to;
while($loop <= 11)
And low and behold, the system now prompted me to enter 12 teams into a league table, as oppose to 20. Superb. Or so I thought...
When I first enter the data into the table and 'submit', it works - as it did perfectly when 20 teams where listed before the above change. However, when I 'update'/make any continual changes to the league table, it also calculates the previous
So for 'Port Talbot Town' I enter "1 Win", "1 Draw", "1 Loss", "5 Goals Scored" "5 Goals Conceded"
And it works. It calculates 3 games played and 4 points (3 for a win, 1 for a draw, 0 for a loss)as shown here.
BUT, when I then enter other information, so entering the same information for 'Aberystwyth Town'and 'update', the information for the previous data (Port Talbot Town) is calculated again?as shown here.
I'm at complete loss. I'd be incredibly grateful and appreciative for any assistance.
The entire code is pasted at the bottom of this.
Please guys! Help an idiot out! 8)
<?php
}
} else if ($url[3] == 'league') {
if ($url[4] == 'standings') {
?>
<ul class="buttonbar">
<li class="button">Add Fixture</li>
<li class="button">Standings</li>
</ul>
<form action="<?=$CONF['site']; ?>mcms/applications/fixtures/submit.php?do=fixtures&action=standings&season=<?=$url
[2]; ?>&comp=<?=$url[3]; ?>" method="post" name="standings">
<div class="table">
<div class="th">Welsh Premier League Standings</div>
<ul class="tr shadow">
<li class="td-squadno">Pos.</li>
<li class="td4" style="width: 170px;">Team</li>
<li class="td4">PLD</li>
<li class="td4">W</li>
<li class="td4">D</li>
<li class="td4">L</li>
<li class="td4">+</li>
<li class="td4">-</li>
<li class="td4">PTS</li>
</ul>
<?php
$query = $SITE -> Query ( 'SELECT a.*, b.name, b.colour, b.txtcolour,
(sum((a.wins*3) + (a.draws*1) - a.pts)) as points,
(sum(a.wins + a.losses + a.draws)) as pld,
(sum(a.glsfor - a.glsaga)) as gd
FROM got_standings a
inner join got_clubs b on (a.cid = b.id)
WHERE a.season = "'.$url[2].'"
GROUP BY a.cid
ORDER BY points DESC, gd DESC, glsfor DESC' );
if ($SITE -> Count ($query) == 0) {
$loop = 0;
while($loop <= 11) {
++$loop;
?>
<ul class="tr" id="team_<?=$loop; ?>">
<li class="td-squadno"><?=$loop; ?></li>
<li class="td2" style="width: 170px;">
<?php
echo ( '
<select name="cid[]">
' );
$sql = $SITE -> Query ( 'SELECT id, name FROM got_clubs WHERE country = "Wales" ORDER BY name ASC' );
while($club = $SITE -> FetchArray($sql)) {
echo ( '<option value="'.$club['id'].'"' );
if ($club['id'] == '1') echo ( ' SELECTED' );
echo ( '>'.$club['name'].'</option>' );
}
echo ( '
</select>
' );
?>
</li>
<li class="td4"></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=wins[]]').val(parseInt($('ul#team_<?
=$loop; ?> li.td4 input[name=wins[]]').val())+1); return false;">+</a> <input type="text" name="wins[]" value="0" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=draws[]]').val(parseInt($('ul#team_<?
=$loop; ?> li.td4 input[name=draws[]]').val())+1); return false;">+</a> <input type="text" name="draws[]" value="0" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=losses[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=losses[]]').val())+1); return false;">+</a> <input type="text" name="losses[]"
value="0" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=glsfor[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=glsfor[]]').val())+1); return false;">+</a> <input type="text" name="glsfor[]"
value="0" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=glsaga[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=glsaga[]]').val())+1); return false;">+</a> <input type="text" name="glsaga[]"
value="0" /></li>
<li class="td4">- <input type="text" name="pts[]" /></li>
</ul>
<?php
}
} else {
$loop = 0;
while($row = $SITE -> FetchArray($query)) {
++$loop;
?>
<ul class="tr" id="team_<?=$loop; ?>">
<li class="td-squadno"><?=$loop; ?></li>
<li class="td-club" style="width: 160px; margin-left: 10px; background-color: #<?=$row['colour']; ?>; color: #<?
=$row['txtcolour']; ?>;"><?=$row['name']; ?><input type="hidden" name="cid[]" value="<?=$row['cid']; ?>" /></li>
<li class="td4"><?=$row['pld']; ?></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=wins[]]').val(parseInt($('ul#team_<?
=$loop; ?> li.td4 input[name=wins[]]').val())+1); return false;">+</a> <input type="text" name="wins[]" value="<?=$row
['wins']; ?>" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=draws[]]').val(parseInt($('ul#team_<?
=$loop; ?> li.td4 input[name=draws[]]').val())+1); return false;">+</a> <input type="text" name="draws[]" value="<?=$row
['draws']; ?>" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=losses[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=losses[]]').val())+1); return false;">+</a> <input type="text" name="losses[]"
value="<?=$row['losses']; ?>" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=glsfor[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=glsfor[]]').val())+1); return false;">+</a> <input type="text" name="glsfor[]"
value="<?=$row['glsfor']; ?>" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=glsaga[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=glsaga[]]').val())+1); return false;">+</a> <input type="text" name="glsaga[]"
value="<?=$row['glsaga']; ?>" /></li>
<li class="td4"><?=$row['points']; ?> (- <input type="text" name="pts[]" value="<?=$row['pts']; ?>" />)</li>
</ul>
<?php
}
}
?>
<div class="tf"><input type="submit" value="Update Standings" /> or <a href="<?=$CONF['site']; ?>sitecontrol/fixtures/<?
=$url[2].'/'.$url[3]; ?>">Cancel</a></div>
</div>
</form>
<p>Do not use the points column for anything other than points deductions. If a points deduction is carried out by the
Premier League, use the input box to enter how many points have been taken away, e.g. Portsmouth in 2009/10 would have
"9" in their points input box.</p>
<p>Matches played and points columns will update automatically. The position of each team in the league table will also be
automatically updated.</p>
<?php
} else {
?>
<ul class="buttonbar">
<li class="button">Add Fixture</li>
<li class="button">Standings</li>
</ul>
<div class="table">
<div class="th">Premier League Fixtures</div>
<?php
$query = $SITE -> Query ( '
SELECT a.id, a.rid, home, away, homescore, awayscore, date, b.name as hometeam, c.name as awayteam FROM
got_fixtures a
inner join got_clubs b on (a.home = b.id)
inner join got_clubs c on (a.away = c.id)
WHERE a.cid="1" AND a.season = "'.$url[2].'" AND (a.home = "1" OR a.away = "1")
ORDER BY date ASC' );
while($row = $SITE -> FetchArray($query)) {
?>
<ul class="tr">
<li class="td-squadno"><?=$row['rid']; ?></li>
<li class="td-home"><?=$row['hometeam']; ?></li>
<li class="td-score"><?=$row['homescore']; ?> - <?=$row['awayscore']; ?></li>
<li class="td-away"><?=$row['awayteam']; ?></li>
<li class="td-kickoff"><?=date('D. jS F y, H:i', $row['date']); ?></li>
<li class="td-end"><a href="<?=$CONF['site']?>sitecontrol/fixtures/<?=$url[2]; ?>/edit/<?=$url[3]; ?>/<?=$row['id'];
?>">Edit</a> or <a href="<?=$CONF['site']; ?>mcms/applications/fixtures/submit.php?do=fixtures&action=delete&id=<?
=$row['id']; ?>&comp=<?=$url[3]; ?>&season=<?=$url[2]; ?>">Remove</a></li>
</ul>
<?php
}
?>
<div class="tf"></div>
</div>

Sounds like it's not inserting it correctly into the database. Look for a SQL query that has INSERT or UPDATE in it.
The SQL in the code above is displaying data from the database, not adding to it. Your problem will be elsewhere. As the while loop pulling from the database was hard coded with the value 19, then the update code will probably be as well.
Also login to your hosting account, go to PhpMyAdmin, and have a look at the database itself. See how that is set up.

Related

Checking in_array value

The Image above is what I want to achieve, the only problem is am echoing subjects and topics where the topics go under the subjects, but I want to only echo say English and all topics under English will go under English. But what I have is every topic comes with its own Subjects. Thus from the Image Two English Language dropdowns.
Here is the code.
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2'";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
while($row=$result->fetch()){
if(!in_array('subject', $row['subject']){
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<ul class="list-unstyled">
<li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
</li>
</ul>
</div>
</li>
<?php
} else {
echo ""
}
}
?>
This should output the subjects and topics and allows for multiple subjects per topic. Also, the echo "" in the else is un-needed.
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2' ORDER BY subject, topic";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
$lastSubject = '';
while($row=$result->fetch()){
if(!in_array('subject', $row['subject']) {
if($lastSubject != $row['subject']) {
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<?php
}
?>
<ul class="list-unstyled">
<li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
</li>
</ul>
<?php
if($lastSubject != $row['subject']) {
?>
</div>
<?php
$lastSubject = $row['subject'];
}
?>
</li>
<?php
}
}
?>

Load data into a html class

I am retrieving data from mysql and need to display the result in a specific class. I am using jquery to update every 10 seconds and this is working ok. Where I am getting stuck is getting that data into a specific class: actions.
I would be grateful if someone could point me in the right direction? php or jquery will be acceptable. Many thanks
$sql= mysqli_query($conn,"SELECT count(*) as total FROM act WHERE new = '1'");
$rows = mysqli_fetch_assoc($sql);
$num = $rows['total'];
$ni = $num;
if($ni < 1) {
$ni = '0';
} else {
echo $ni; <--- NEED TO LOAD RESULT IN ACTIONS CLASS
}
Example html from header.php
<li>
Boxes <span class="drop-icon">▸</span> <label class="drop-icon" for="sm4" title="Toggle Drop-down">▾</label>
<input id="sm4" type="checkbox">
<ul class="sub-menu">
<li>
New Intake <span style="float: right;" class="notification ni"><?php echo $ni_num; ?></span>
</li>
<li>
Retrievals <span style="float: right;" class="notification retrievals"><?php echo $brtv_num; ?></span>
</li>
<li>
Returns <span style="float: right;" class="notification returns"><?php echo $brtn_num; ?></span>
</li>
<li>
Destructions <span style="float: right;" class="notification destructions"><?php echo $bdstr_num; ?></span>
</li>
<li>
Permanent Retrieval <span style="float: right;" class="notification pretrieval"><?php echo $prtv_num; ?></span>
</li>
</ul>
</li>
Example from loadActions.php
$sql= mysqli_query($conn,"SELECT count(*) as total FROM act WHERE new = '1'");
$rows = mysqli_fetch_assoc($sql);
$num = $rows['total'];
$ni = $num;
if($ni < 1) {
$ni = '0';
} echo $ni;
$nisql= mysqli_query($conn,"SELECT count(*) as intake FROM act WHERE activity='New Intake' AND new = '1'"); // provide db connection object as first parameter
$ni_row = mysqli_fetch_assoc($nisql);
$ninum = $ni_row['intake'];
//echo $num;
$ni_num = $ninum;
if($ni_num < 1) {
$ni_num = '0';
} echo $ni_num;
$brtvsql= mysqli_query($conn,"SELECT count(*) as brtv FROM act WHERE activity='Box Retrieval' AND new = '1'"); // provide db connection object as first parameter
$brtv_row = mysqli_fetch_assoc($brtvsql);
$brtvnum = $brtv_row['brtv'];
//echo $num;
$brtv_num = $brtvnum;
if($brtv_num < 1) {
$brtv_num = '0';
} echo $brtv_num;
$brtnsql= mysqli_query($conn,"SELECT count(*) as brtn FROM act WHERE activity='Box Return' AND new = '1'"); // provide db connection object as first parameter
$brtn_row = mysqli_fetch_assoc($brtnsql);
$brtnnum = $brtn_row['brtn'];
//echo $num;
$brtn_num = $brtnnum;
if($brtn_num < 1) {
$brtn_num = '0';
} echo $brtn_num;
Please note, I am NOT a PHP programmer so you will have to do some work yourself
Take below as pseudo code and ask another PHP question if the PHP does not make sense.
Have ONE request
select activity,
count(*) total,
sum(activity = 'New Intake') intakeCount,
sum(activity = 'Box Retrieval') boxCount,
...
from act WHERE new = '1'
if ($rec["total"] == 0) {
echo '{ "total" : 0 }';
die 0;
}
$res = array(
"total" => $rec["total"],
"ni" => $rec["intakeCount"],
"retrievals" => $rec["boxCount"],
...
);
echo json_encode($res);
Result should be
{ "total" : 24,
"ni" : 14,
"retrievals" : 9,
....
}
Then you can do
function getBoxes() {
$.get('/domain/admin/loadActions.php', function(data) {
processData(data);
setTimeout(getBoxes, 15000);
});
}
function processData(data) {
for (key in data) {
console.log(key,data[key])
$("." + key).text(data[key]);
}
}
$(function() { // page load
// testing - remove this when running the getBoxes():
processData({
"total": 24,
"ni": 14,
"retrievals": 9
});
// getBoxes(); // remove comment when tested
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li>
Boxes <span class="drop-icon">▸</span> <label class="drop-icon" for="sm4" title="Toggle Drop-down">▾</label>
<input id="sm4" type="checkbox">
<ul class="sub-menu">
<li>
New Intake <span style="float: right;" class="notification ni"></span>
</li>
<li>
Retrievals <span style="float: right;" class="notification retrievals"></span>
</li>
<li>
Returns <span style="float: right;" class="notification returns"></span>
</li>
<li>
Destructions <span style="float: right;" class="notification destructions"></span>
</li>
<li>
Permanent Retrieval <span style="float: right;" class="notification pretrieval"></span>
</li>
</ul>
</li>
<div class="total"></div>

Dynamic active class with variables php

I want to add class= "active" to my link using php but the challenge am having is that my links have variables attached to them .php?foobar=somevariable. so, some how the class active is being ignored. Here is my snippet
<?php
//we need to set a var to find what page we are on
$pageOn2 = basename($_SERVER['PHP_SELF']);
?>
And below is my html
<li class="<?php if($pageOn2 == 'regionalsummits.php?summit=eastern'){?> current-menu-item <?php }?>">
Eastern Region
</li>
<li class="<?php if($pageOn2 == 'regionalsummits.php?summit=southern'){?> current-menu-item<?php }?>">
South Western Region
</li>
<li class="<?php if($pageOn2 == 'regionalsummits.php?summit=western'){?> current-menu-item<?php }?>">
Western Region
</li>
Any help on where am goinh wrong
try saving paramater in variable and then check
<?php
$variable = $_GET['foobar'];
?>
<li class="<?php if($variable == 'eastern'){ echo 'current-menu-item'; } ?>"> Eastern Region </li>
<li class="<?php if($variable == 'southern'){ echo 'current-menu-item'; }?>"> South Western Region </li>
<li class="<?php if($variable == 'western'){ echo 'current-menu-item'; } ?>"> Western Region </li>
$pageOn2 = basename($_SERVER['PHP_SELF']); in this line of code the variable $pageOn2 will recieve the value regionalsummits.php it will not get the query string cuz basename($_SERVER['PHP_SELF']); this will not return the query string
so you can do this like:
$pageOn2 = basename($_SERVER['PHP_SELF'])."?".$_SERVER['QUERY_STRING'];
Try in this way ,you can do it some other way .
I have added a snippet for the better understanding.
<?php
$pageOn2 = $_SERVER["QUERY_STRING"];
?>
<li class="<?php if ($pageOn2 == 'summit=eastern') { ?> current-menu-item <?php } ?>">
Eastern Region
</li>
<li class="<?php if ($pageOn2 == 'summit=southern') { ?> current-menu-item<?php } ?>">
South Western Region
</li>
<li class="<?php if ($pageOn2 == 'summit=western') { ?> current-menu-item<?php } ?>">
Western Region
</li>
<style>
.current-menu-item
{
background-color:red;
}
</style>

show the total count value at beginning

I have no experience in php but i am facing a problem in php.
i have a php code like this:
<div>
<ul>
<li><input type="checkbox" name="checkAll"
id="selectAll" class="checkbox"> Select All </li>
<?php
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases"
class="checkbox"
value="<?php echo $testcase->id; ?>">
<?php echo $testcase->name; ?>
<?php $count++;?>
</li>
<?php
}
?>
<li> <?php echo "Total Test Case COunt: $count";?> </li>
</ul>
But i want to print "Total Test Case count" at beginning list. How i can do this?
Put a count($caseList) at the top.
<div>
<ul>
<li><input type="checkbox" name="checkAll" id="selectAll" class="checkbox"> Select All </li>
<li> <?php echo "Total Test Case COunt: " . count($caseList);?> </li>
<?php
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases" class="checkbox" value="<?php echo $testcase->id; ?>"><?php echo $testcase->name; ?><?php $count++;?></li>
<?php
}
?>
</ul>
You can try to get the length of the array without a counter using the count function.
$count = count($caseList);
So your final code could be like
div>
<ul>
<li><input type="checkbox" name="checkAll" id="selectAll" class="checkbox"> Select All </li>
<?php
$total = count($caseList);
echo "Total Test Case Count: $total"
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases" class="checkbox" value="<?php echo $testcase->id; ?>"><?php echo $testcase->name; ?><?php $count++;?></li>
<?php
}
?>
</ul>
you need to move increment from "li" tag
<div>
<ul>
<li><input type="checkbox" name="checkAll"
id="selectAll" class="checkbox"> Select All </li>
<?php
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases"
class="checkbox"
value="<?php echo $testcase->id; ?>">
<?php echo $testcase->name; ?>
</li>
<?php
$count++;
}
?>
<li> <?php echo "Total Test Case COunt: $count";?> </li>
</ul>

CSS Dropdown menu through PHP, logic defeats me

My brain doesn't seem to want to work this morning.. i have the following array:
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']];
This is then fed to a php page which is meant to lay out a multi-layered pure css drop down menu.. the php page code is:
<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 } ?>
The output is not correct however as the above code always closes the list item containing the second layer before the second layer is ready to be closed. if that makes sense..
<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>
SOLUTION:
<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 } ?>
Never close the the li tag, close it when you add something
foreach ($links as $link) {
if ($item_layer > $this_layer) {
echo '<ul><li> ......';
this_layer++;
}elseif ($item_layer == $this_layer) {
echo '</li><li>......';
}elseif ($item_layer < $this_layer) {
echo '</li></ul><li>......';
$this_layer--;
}
}
Last you probably need to add a echo '</li></ul>' outside of the foreach to close everything
Had to rework the code a little, usually i organize the php menu structure in tree form makes it easier to parse into html menus, but here it is:
<ul class="<?php echo $ul_class; ?>">
<?php
$this_layer = 0;
$closeit = false;
// place array content into variables
foreach ($nav_pages as $link) {
$item_layer = $link[0];
$item_string = $link[1];
$item_link = $link[2];
if ($item_layer > $this_layer) {
// Changing level, dont close the previous li
?><ul><?php
$closeit = false;
$this_layer++;
}
if ($item_layer == $this_layer) {
// Same level, check if you need to close previous li
if ($closeit) {
?></li><?php
}
// Render the li
?>
<li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php
// Close it on next loop
$closeit = true;
}
elseif ($item_layer < $this_layer) {
// Changing layer back down, close the previous li and the current level ul
?>
</li>
</ul>
<?php
$this_layer--;
}
}
// Finished loop, there should still be a li waiting to be closed
if ($closeit) {
?></li><?php
}
// Close menu
?>
</ul>
Should be working, let me know if it isnt
Suggested php menu structure:
$menu = array(
array(
"url"=>"/place.php",
"text"=>"Go to place"
),
array(
"url"=>"/place2.php", // not necessary
"text"=>"with submenu",
"children"=>array(
array(
"url"=>"/placewithinplace2.php",
"text"=>"submenu item"
)
)
)
);

Categories