PHP problems

Tindris

Seasoned Veteran
Joined
Mar 6, 2011
Messages
3,967
Reaction score
9
FP$
28,386
I am working on a dynamic navigation for my site but I can't get some of the script to work. It is probably something simple but I can't seem to work out what the problem is.

Code:
function get_menu_html( $item )
	{
		$this->html = array();
		$this->menu = $this->build_menu_array();
		$this->build_item_html( $this->menu );
		return implode( "\r\n", $this->html );
	}
}
 
$menu = new MenuBuilder();
echo '<pre>' . htmlentities( $menu->get_menu_html() ) . '</pre>';

This is the part that is throwing errors.

The error message:
Code:
Warning: Missing argument 1 for MenuBuilder::get_menu_html(), called in C:\wamp\www\creaspire\nav.php on line 161 and defined in C:\wamp\www\creaspire\nav.php on line 151

Grateful for any help!
 
Would this be of any help to you? 😕
I found it over here: http://www.copypastecode.com/33259/

Code:
<?php
 
class MenuBuilder
{
	var $conn;
	var $items;
	var $menu;
	var $html;
 
	function MenuBuilder()
	{
		$this->conn = mysqli_connect( 'localhost', 'user', 'pass', 'example' );
	}
 
	function fetch_assoc_all( $sql )
	{
		$result = mysqli_query( $this->conn, $sql );
 
		if ( !$result )
			return false;
 
		$assoc_all = array();
 
		while( $fetch = $result->fetch_assoc() )
			$assoc_all[] = $fetch;
 
		mysqli_free_result( $result );
 
		return $assoc_all;
	}
 
	function get_menu_items()
	{
		$sql = 'SELECT id, parent_id, title, link, position FROM menu_item ORDER BY parent_id, position;';
		return $this->fetch_assoc_all( $sql );
	}
 
	function build_menu_array()
	{
		$this->items = $this->get_menu_items();
 
		if ( empty( $this->items ) )
			return '';
 
		foreach ( $this->items as $item )
			$children[$item['parent_id']][] = $item;
 
		$root = array();
		$root['html_before'] = '<ul id="0">';
		$root['html_after'] = '</ul>';
 
		$parent = 0;
		$tree = array();
		$current_tree = &$tree;
		$tree_stack = $parent_stack = array();
 
		if ( empty( $children[$parent] ) )
			return '';
 
		while ( ( $option = each( $children[$parent] ) ) || ( $parent > 0 ) )
		{
			if ( $option === false )
			{
				unset( $current_tree );
				$current_tree = &$tree_stack[count($tree_stack)-1];
				array_pop( $tree_stack );
				$parent = array_pop( $parent_stack );
			}
			elseif ( empty( $children[$option['value']['id']] ) )
			{
				$data = array();
				$data['html'] = str_repeat( "\t", count( $tree_stack ) + 1 ) . '<li>' . $option['value']['title'] . '</li>';
				$current_tree[] = $data;
			}
			else
			{
				$data = array();
				$data['html_before'] = str_repeat( "\t", count( $tree_stack ) + 1 );
				$data['html_before'] .= '<li>' . $option['value']['title'] . '</li>';
				$data['html_before'] .= "\r\n" . str_repeat( "\t", count( $tree_stack ) + 1 );
				$data['html_before'] .= '<ul id="' . $option['value']['id'] . '">';
				$data['html_after'] .= str_repeat( "\t", count( $tree_stack ) + 1 ) . '</ul>';
				$data_children = array();
				$data['children'] = &$data_children;
				$current_tree[] = $data;
				$tree_stack[] = &$current_tree;
				array_push( $parent_stack, $option['value']['parent_id'] );
				unset( $current_tree );
				$current_tree = &$data_children;
				unset( $data_children );
				$parent = $option['value']['id'];
			}
		}
 
		$root['children'] = $tree;
 
		return array( $root );
	}
 
	function build_item_html( $item )
	{
		foreach ( $item as $element )
			if ( isset( $element['html'] ) )
				$this->html[] = $element['html'];
			else
			{
				$this->html[] = $element['html_before'];
				$this->build_item_html( $element['children'] );
				$this->html[] = $element['html_after'];
			}
	}
 
	function get_menu_html( $item )
	{
		$this->html = array();
		$this->menu = $this->build_menu_array();
		$this->build_item_html( $this->menu );
		return implode( "\r\n", $this->html );
	}
}
 
$menu = new MenuBuilder();
echo '<pre>' . htmlentities( $menu->get_menu_html() ) . '</pŕe>';
 
?>
 
That is the code I am using, but it is throwing errors as it is. I want to get it working so I can adapt it.
 
The key to knowing how to fix the error is this code:
Code:
Missing argument

to fix you need to define an argument for the function:
Code:
get_menu_html()

To do so just add an argument like so:
Code:
get_menu_html("tindris")
 
Seems like you're not using the $item variable at all. You might be able to just replace the function line with just this.

Code:
function get_menu_html()
 
Correct you are El Canadiano 🙂
Either way will work. 😉
 
Back
Top Bottom