• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP.. help?

Robert

NLC
NLC
I'm trying to find out how to develop a script in php to generate a menu for me. I want the menu to have top level categories and then sub-categories... something like this:

Category 1
- Subcategory 1 under Category 1
- Subcategory 2 under Category 1
- Subcategory 3 under Category 1
Category 2
- Subcategory 1 under Category 2
- Subcategory 2 under Category 2
- Subcategory 3 under Category 2

And then as they add a top level category, it gets added to the menu automatically and if they add a subcategory to the top level, it gets added automatically and so on.

I'll be using MySQL to store the values. I'm looking for the script that can do this and then I can customize it to my needs. No javascript wanted. I just want php to output the HTML code.

Thanks!
 
Are you confident that if I spend some time writing a class to do it you can utilize all it's functions properly, or do you want lots of procedural code snippets to do the job ?
 
I'm pretty confident that I can utilize it properly without the need of procedural code snippets. I'm amazed I can't find any examples online.
 
Last edited:
not really complicated, by default when I open an ide I'll start using php5 keywords that dont exist in php4, no problem I'll just make it work for 4 ....
 
I have done this, I just didn't get to a machine to test it yet, I'll post an examples and readme later this afternoon ... along with a sql structure .....

PHP:
<?php

if( !defined( 'MYSQL_HOSTNAME' ) ) 	define( 'MYSQL_HOSTNAME',			'localhost' );
if( !defined( 'MYSQL_USERNAME' ) ) 	define( 'MYSQL_USERNAME',			'username' );
if( !defined( 'MYSQL_PASSWORD' ) ) 	define( 'MYSQL_PASSWORD',			'password' );
if( !defined( 'MYSQL_DATABASE' ) ) 	define( 'MYSQL_DATABASE',			'database' );
if( !defined( 'MYSQL_TABLE' ) )		define( 'MYSQL_TABLE',				'category' );
	
class category
{
	var $link ;
	
	function category( )
	{
		if( !( $this->link = mysql_connect( MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD ) ) )
		{
			die( sprintf( 'Cannot connect to mysql @ %s with %s and supplied password', MYSQL_HOSTNAME, MYSQL_USERNAME ) );
		}
		elseif( !mysql_select_db( MYSQL_DATABASE ) )
		{
			die( sprintf( 'Cannot select %s @ %s', MYSQL_DATABASE, MYSQL_HOSTNAME ) );
		}
	}
	function add( $name, $subcat = 0 )
	{
		if( $name != "" ) return mysql_query( sprintf( 'INSERT INTO `%s` VALUES( "%s", "", %d )', MYSQL_TABLE, $name, $subcat ), $this->link );
	}
	function del( $id )
	{
		if( ( $result = mysql_query( sprintf( 'DELETE FROM `%s` WHERE id = %d LIMIT 1', MYSQL_TABLE, $id ), $this->link ) ) )
		{
			return mysql_num_rows( $result );
		}
	}
	function get( $id )
	{
		if( ( $result = mysql_query( sprintf( 'SELECT * FROM `%s` WHERE id = %d LIMIT 1', MYSQL_TABLE, $id ), $this->link ) ) )
		{
			return mysql_fetch_assoc( $result );
		}
	}
	function namebyid( $id )
	{
		if( ( $result = mysql_query( sprintf( 'SELECT * FROM `%s` WHERE id = %d', MYSQL_TABLE, $id ), $this->link ) ) )
		{
			return mysql_result( $result, 0, 'name' );
		}
	}
	function idbyname( $name )
	{
		if( ( $result = mysql_query( sprintf( 'SELECT * FROM `%s` WHERE name = "%s"', MYSQL_TABLE, $name ), $this->link ) ) )
		{
			return mysql_result( $result, 0, 'id' );
		}
	}
	function cats( )
	{
		if( ( $result = mysql_query( sprintf( 'SELECT * FROM `%s` WHERE subcat = 0', MYSQL_TABLE ), $this->link ) ) )
		{
			$return = array( );
			
			while( $assoc = mysql_fetch_assoc( $result ) )
			{
				if( is_array( $assoc ) )
				{
					$return[ ] = $assoc ;
				}
			}
			
			return $return ;
		}
	}
	function subs( $cat )
	{
		if( ( $result = mysql_query( sprintf( 'SELECT * FROM `%s` WHERE subcat = %d', MYSQL_TABLE, $cat ), $this->link ) ) )
		{
			$return = array( );
			
			while( $assoc = mysql_fetch_assoc( $result ) )
			{
				if( is_array( $assoc ) )
				{
					$return[ ] = $assoc ;
				}
			}
			
			return $return ;
		}
	}
}
?>
 
Back
Top