Jump to content

Recommended Posts

Let me start by saying this code functions on a small amount of data, but it crashes everytime on a large amount.  How can I recreate this to get me the same result without the apparent server load that causes it to stop?

 

<?php
$nav_query = mysql_query(" SELECT categories.categories_id, categories.parent_id, categories_description.categories_id, categories_description.categories_name  
FROM categories, categories_description 
WHERE categories.categories_id = categories_description.categories_id ORDER BY categories.parent_id, categories_name ASC");

$tree = "";					// Clear the directory tree
$depth = 5;					// Child level depth.
$top_level_on = 1;			// What top-level category are we on?
$exclude = array();			// Define the exclusion array
array_push($exclude, 0);	// Put a starting value in it

while ( $nav_row = mysql_fetch_array($nav_query) )
{
$goOn = 1;			// Resets variable to allow us to continue building out the tree.
for($x = 0; $x < count($exclude); $x++ )		// Check to see if the new item has been used
{
	if ( $exclude[$x] == $nav_row['categories_id'] )
	{
		$goOn = 0;
		break;				// Stop looking b/c we already found that it's in the exclusion list and we can't continue to process this node
	}
}
if ( $goOn == 1 )
{
	$tree .= $nav_row['categories_name'] . "<br>";				// Process the main tree node
	array_push($exclude, $nav_row['categories_id']);		// Add to the exclusion list
	if ( $nav_row['category_id'] < 6 )
	{ $top_level_on = $nav_row['categories_id']; }

	$tree .= build_child($nav_row['categories_id']);		// Start the recursive function of building the child tree
}
}


function build_child($oldID)			// Recursive function to get all of the children...unlimited depth
{
global $exclude, $depth;			// Refer to the global array defined at the top of this script

$child_query = mysql_query("SELECT * FROM categories WHERE parent_id=" . $oldID);

while ( $child = mysql_fetch_array($child_query) )
{
	if ( $child['categories_id'] != $child['parent_id'] )
	{
		for ( $c=0;$c<$depth;$c++ )			// Indent over so that there is distinction between levels
		{ $tempTree .= " "; }
		$tempTree .= "- " . $child['categories_id'] . "<br>";
		$depth++;		// Incriment depth b/c we're building this child's child tree  (complicated yet???)
		$tempTree .= build_child($child['categories_id']);		// Add to the temporary local tree
		$depth--;		// Decrement depth b/c we're done building the child's child tree.
		array_push($exclude, $child['categories_id']);			// Add the item to the exclusion list
	}
}

return $tempTree;		// Return the entire child tree
}

echo $tree;

?>

 

I have the sql structure if necessary.  Please help!

ok.. PHP can't handle excessive amounts of data being stored in it.. so if you have a variable with tons and tons of data, php will produce a fatal error..

 

what you CAN do, is OUTPUT the data, or store it within a text file, until you're done processing the data, unloading the variable regularly during the process.. then output the contents of the file where it belongs, and that should avoid the error, although I'm sure there are OTHER, BETTER ways.. that is the best way I could think of :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.