Jump to content

PHP ODBC (Foreach/While Loops) Problem

Featured Replies

I have an ANNOYING problem in PHP - I'm SURE it's something I'm missing, but if anyone can help, PLEASE.

 

I'm dying here. :)

 

I've done a DB (it's odbc TEMPORARILY.. when i get my mysql server it'll change, but since that's not the problem it doesnt really matter) that contain the Menu items on the left-hand side. The thing I wanna do is that the code will read the db and compile the Menu with its submenu items. Why? 'cause i want the company to be able to add menu items and submenu items without bothering me, that's why :P

 

In anycase -- it's only HALF working, and I don't know WHY.

 

here's my code:

 


     <?php
//get the menu:
//-------------

//connect to database 
$connectionstring = odbc_connect("schotDB", "", ""); 
//SQL query 
$Query = "SELECT * FROM MENU WHERE Classification='upper';"; 
//execute query 
$queryexe = odbc_do($connectionstring, $Query); 

while ($Row = odbc_fetch_array($queryexe))
{
	//put into array, to extract later:
	$UpperMNUItems[]=odbc_result($queryexe,"CatName");
}
$MNUUpperCntr=1; //so the DIV parts would get right

foreach ($UpperMNUItems as $MNUItem)
{
//upper subj:
print "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
print "<tr>";
//Category Name -- UPPER MENU ITEM
print "<td><a href='javascript: DisplayI(this." . $MNUUpperCntr . ")'".odbc_result($queryexe,"CatName")."</a></td>"; 
print "</tr>";
print "</table>";

//now check if there is anything in the SUB menu:
//----------------------------------------------------------------		
//SQL query 
$Query = "SELECT * FROM MENU WHERE Classification='".$MNUItem."';"; //extract the submenu items that are related to the UPPER menu item.
//execute query 
$queryexe = odbc_do($connectionstring, $Query); 

if ($queryexe) //there ARE sub menu items:
{
	print("<DIV ID='" . $MNUUpperCntr ."'");
	print(" style='display:none;' class=NoMargins>");
	print("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");

while ($Row = odbc_fetch_array($queryexe))
	{	
	print "<tr bgcolor='#006699'>";
	if (odbc_result($queryexe,"URL")=="")
		{
		print "	<td align='center'>" . odbc_result($queryexe,"CatName") . "</td>\n";
		}
		else
		{
		print "<td><a href='".odbc_result($queryexe,"URL")."'>".odbc_result($queryexe,"CatName")."</a><td>\n";
		}
		print "</tr>";
	}		
	print "</table>";
	print "</DIV>";
}

//----------------------------------------------------------------		

$MNUUpperCntr++;
}
?>

 

and the page is here: http://moriel.schottlender.com/NewSchottlenderSite/index.php

 

For some ODD reason it doesnt extract all the UPPER menu items AND it mixes up the upper ones with the subitems ... :\ It's a PHP mess, in short. and I REALLY don't see why, the code theoretically is okay...

 

HELP. Seriously. :help:

 

~moo

  • Author

Okay, thanks guys I've solved it.

I have no idea how, I just did it all over again - SLOWLY - with debugging practically every line - and it WORKS.

 

Yey.

 

If you're interrested in my solution, btw, let me know, I'll paste it here or something.

 

~moo

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.