...

View Full Version : Loop not Looping!



Arcticwarrio
07-22-2012, 07:20 PM
anyone got any ideas why this is happening?

it's building a menu with data from mysql, the 1st menu works but the 2nd is missing its sub menus.

Code:


<?PHP
include ("includes/session.php");
include ("includes/db.php");
include ("includes/functions.php");

$Q1 = Q("SELECT * FROM `menu` WHERE `MenuGroup` = 1");
$Q2 = Q("SELECT * FROM menu");
$Q3 = Q("SELECT * FROM menu");

$i = 0;
$Content .= '<ul id="css3menu1" class="topmenu">
';
while ($M1 = mysql_fetch_array($Q1)){
$Content .= '<li class="';
if ($i == 0){$Content .= 'topfirst';$i++;}
elseif ($i == mysql_num_rows($Q1)){$Content .= 'toplast';$i++;}
else {$Content .= 'topmenu';$i++;}
$Content .= '"><a href="#" style="height:18px;line-height:18px;"><span>'.$M1['MenuNumber'].' '.$M1['MenuName'].'</span></a>';

$Sub = 0;
while ($M3 = mysql_fetch_array($Q3)){
if ($M3['MenuParent'] != null){$Sub++;}
}
mysql_data_seek($Q3, 0);
if ($Sub == 0){
$Content .= '</li>
';
}else{
$Sub = 0;
$Content .= '<ul>';
while ($M3 = mysql_fetch_array($Q3)){
if ($M3['MenuParent'] == $M1['MenuID']){
$Content .= '
<li><a href="#">'.$M3['MenuNumber'].' '.$M3['MenuName'].'</a>';
$Sub = 0;


while ($M2 = mysql_fetch_array($Q2)){
if ($M2['MenuParent'] != null){$Sub++;}
}
mysql_data_seek($Q2, 0);
if ($Sub == 0){
$Content .= '</li>
';
}else{
$Sub = 0;
$Content .= '<ul>';
while ($M2 = mysql_fetch_array($Q2)){
if ($M2['MenuParent'] == $M3['MenuID']){
$Content .= '
<li><a href="#">'.$M2['MenuNumber'].' '.$M2['MenuName'].'</a>';
$Sub = 0;
}
}
mysql_data_seek($Q2, 0);
$Content .= '</ul></li>
';
}





}
}
mysql_data_seek($Q3, 0);
$Content .= '</ul></li>
';
}
}
$Content .= '</ul>';

include ("template.php");

?>


result:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Language" content="en-gb" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PROJECT_X - </title>
<link rel="stylesheet" type="text/css" href="Style/Style.css" />
</head>
<body >
<div class="AligtCenter Message"></div>
<div class="content"><ul id="css3menu1" class="topmenu">
<li class="topfirst"><a href="#" style="height:18px;line-height:18px;"><span>0 Menu</span></a><ul>
<li><a href="#">1 General Company Info</a><ul>
<li><a href="#">1 Company Config</a>
<li><a href="#">2 Software Config</a>
<li><a href="#">3 Personalise</a></ul></li>

<li><a href="#">2 Central Info</a><ul>
<li><a href="#">1 Maintain Accounting Period Dates</a>
<li><a href="#">2 Maintain Country Codes</a></ul></li>

<li><a href="#">3 VAT Rates and Reporting</a><ul>
<li><a href="#">1 Maintain VAT Rates</a>
<li><a href="#">2 Print VAT Analysis Report</a>
<li><a href="#">3 VAT Return Manager</a>
<li><a href="#">4 Enter / Change VAT Scale Charges</a>
<li><a href="#">5 Edit EC Scales List</a>
<li><a href="#">6 EC Sales List Manager</a>
<li><a href="#">7 Intrastat Submission Manager</a>
<li><a href="#">8 Print Reverse Charge Scales List</a>
<li><a href="#">9 Print VAT Pre-Inspection Report</a></ul></li>

<li><a href="#">4 User Password Maintenance</a><ul>
<li><a href="#">1 Maintain Users</a>
<li><a href="#">2 View Current Logins</a></ul></li>

<li><a href="#">5 General Operations and Utilities</a><ul>
<li><a href="#">1 Maintain Default Distributions</a>
<li><a href="#">3 Change Nominal Account Number</a>
<li><a href="#">4 Change Customer / Supplier Code</a>
<li><a href="#">5 Change Product Code</a>
<li><a href="#">6 Change Group Code</a>
<li><a href="#">7 Change Delivery Codes</a>
<li><a href="#">8 Change Product Group Allocation</a>
<li><a href="#">9 Print System Audit / VAT Audit Log</a></ul></li>

<li><a href="#">6 Foreign Currency</a><ul>
<li><a href="#">1 Maintain Foreign Currency Rates</a></ul></li>

<li><a href="#">7 Interest Rates</a><ul>
<li><a href="#">1 Maintain Interest Rates</a></ul></li>

<li><a href="#">9 End of period programs</a><ul>
<li><a href="#">1 Ledgers and Cash Book Period Close</a>
<li><a href="#">9 Change Ledger Date</a></ul></li>
</ul></li>
<li class="topmenu"><a href="#" style="height:18px;line-height:18px;"><span>1 Nominal</span></a><ul>
<li><a href="#">1 Nominal account</a><ul></ul></li>

<li><a href="#">2 enter / change journals</a><ul></ul></li>

<li><a href="#">3 Reporting</a><ul></ul></li>

<li><a href="#">4 Report Generator</a><ul></ul></li>

<li><a href="#">8 Utilities</a><ul></ul></li>

<li><a href="#">9 End of Period programs</a><ul></ul></li>
</ul></li>
</ul></div>

</body>

</html>

Arcticwarrio
07-22-2012, 10:09 PM
LMAO!

the code is fine, the sub menu items wernt in the database yet lol...

a little tidy up on the code and a 3 level menu is finished!

Here if you'd like it


$Q1 = Q("SELECT * FROM `menu` WHERE `MenuGroup` = 1");
$Q2 = Q("SELECT * FROM menu");
$Q3 = Q("SELECT * FROM menu");

$i = 0;
$Content .= '<ul id="css3menu1" class="topmenu">
';
while ($M1 = mysql_fetch_array($Q1)){
$Content .= '<li class="';
if ($i == 0){$Content .= 'topfirst';$i++;}
elseif ($i+1 == mysql_num_rows($Q1)){$Content .= 'toplast';$i++;}
else {$Content .= 'topmenu';$i++;}
$Content .= '"><a href="#" style="height:18px;line-height:18px;"><span>'.$M1['MenuNumber'].' '.$M1['MenuName'].'</span></a>';

$Sub = 0;
while ($M3 = mysql_fetch_array($Q3)){
if ($M3['MenuParent'] != null){$Sub++;}
}
mysql_data_seek($Q3, 0);
if ($Sub == 0){
$Content .= '</li>
';
}else{
$Sub = 0;
$Content .= '<ul>';
while ($M3 = mysql_fetch_array($Q3)){
if ($M3['MenuParent'] == $M1['MenuID']){
$Content .= '
<li><a href="#">'.$M3['MenuNumber'].' '.$M3['MenuName'].'</a>';
$Sub = 0;


while ($M2 = mysql_fetch_array($Q2)){
if ($M2['MenuParent'] == $M3['MenuID']){$Sub++;}
}
mysql_data_seek($Q2, 0);
if ($Sub == 0){
$Content .= '</li>
';
}else{
$Sub = 0;
$Content .= '<ul>';
while ($M2 = mysql_fetch_array($Q2)){
if ($M2['MenuParent'] == $M3['MenuID']){
$Content .= '
<li><a href="#">'.$M2['MenuNumber'].' '.$M2['MenuName'].'</a></li>';
$Sub = 0;
}
}
mysql_data_seek($Q2, 0);
$Content .= '</ul></li>
';
}





}
}
mysql_data_seek($Q3, 0);
$Content .= '</ul></li>
';
}
}
$Content .= '</ul>';

connormcwood
07-22-2012, 10:24 PM
*snip*
Isn't
mysql_data_seek discouraged. Isn't it better to use
mysqli_data_seek(). I dont really know if there is a difference I just read up on it:)

Arcticwarrio
07-22-2012, 10:48 PM
Isn't
mysql_data_seek discouraged. Isn't it better to use
mysqli_data_seek(). I dont really know if there is a difference I just read up on it:)

im using data seek to reset the query without querying it again (less server load)

probably, its just a newer language.


The mysqli extension, or as it is sometimes known, the MySQL improved extension, was developed to take advantage of new features found in MySQL systems versions 4.1.3 and newer. The mysqli extension is included with PHP versions 5 and later.

The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:

Object-oriented interface
Support for Prepared Statements
Support for Multiple Statements
Support for Transactions
Enhanced debugging capabilities
Embedded server support



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum