Page 1 of 1

select problem

Posted: Thu Dec 29, 2005 3:27 am
by php3ch0
Hi everyone and thanks in advance.

I am making a download script and I need to display all categories and sub categories in a select box so that I can move downloads put into the wrong category.

The database organises categories with id and parent id.

I am trying to create a function which will display a select box with the current category selected but showing all categories indented to make t easier to read.

This is as far as I have got please help.

Can I call a function within itself ?
Am I going about this the wrong way?

Code: Select all

function show_select($cat_id, $current_cat) {

	global $db_connect;
	global $database_db_connect;
	
	mysql_select_db($database_db_connect, $db_connect);
	$query_categories = "SELECT * FROM dload2_categories WHERE parent_id = '$cat_id'";
	$categories = mysql_query($query_categories, $db_connect) or die(mysql_error());
	$row_categories = mysql_fetch_assoc($categories);
	$totalRows_categories = mysql_num_rows($categories);
	
	if($cat_id == '0') { echo "<select name='categories' class='p'>"; }
	if($totalRows_categories <> '0') {
	do {
			
			echo "<option value='".$row_categories['id']."'";
			if($current_cat == $row_categories['id']) { echo "SELECTED"; }
			echo "> -".$row_categories['name']."</option>";
			show_select($row_categories['id'],$current_cat);
	
	} while($row_categories = mysql_fetch_assoc($categories));
	}
echo "</select>";
}

Posted: Thu Dec 29, 2005 3:57 am
by Jenk
A function that calls itself is called a recursive function.

Posted: Thu Dec 29, 2005 4:29 am
by php3ch0
Thanks

Fixed it

Code: Select all

function show_select($cat_id, $current_cat,$start,$count) {

	global $db_connect;
	global $database_db_connect;
	
	$count++;
	
	mysql_select_db($database_db_connect, $db_connect);
	$query_categories = "SELECT * FROM dload2_categories WHERE parent_id = '$cat_id'";
	$categories = mysql_query($query_categories, $db_connect) or die(mysql_error());
	$row_categories = mysql_fetch_assoc($categories);
	$totalRows_categories = mysql_num_rows($categories);
	
	if($start=="start") { echo "<select name='categories' class='p'>"; }
	if($totalRows_categories <> '0') {
	do {
			
			echo "<option value='".$row_categories['id']."'";
			if($current_cat == $row_categories['id']) { echo "SELECTED"; }
			echo ">";
			$count2 = '0';
				if ($count <> '1') {
						do { echo "&nbsp;&nbsp;&nbsp;";
							$count2++;
			 			} while ($count2 <= $count);
			 	}
			 echo "-".$row_categories['name']."</option>";
			 
			show_select($row_categories['id'],$current_cat,"stop",$count);
	
	
	} while($row_categories = mysql_fetch_assoc($categories));
	
	}
	
if($start=="start") { echo "</select>\n"; }
}