Jump to content


Photo

Help with arrays


  • Please log in to reply
2 replies to this topic

#1 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 06 July 2006 - 09:19 PM

Hi guys, ive been dreading doing this for awhile, but finally got to it. I need to make a drop down box similar to IPB's "move topic to this forum". It will contain the category name using the optgroup tag, then all forums, and under each forum will be their subforums, if any. I figured to save processing time, i should do 1 database query, drop the results into an array, ksort it, and then print out the sectiosn.

Result should look like this:
Category
-Forum
--Sub Forum
--Sub Forum
Category
-Forum

Variables:
$categories[$cat_order] = array('catid' => '$cat_id', 'catname' => '$cat_name');
$categories[$forum_cat_id][$forum_order] = array('forumid' => '$forum_id', 'subforumid' => '$forum_subforum_id', 'forumname' => '$forum_name');

Info:
The value of $forum_subforum_id is the same as the $forum_id that it belongs to.

Code so far: (I have no idea if this will work, its a mock-up)
<?
	$categories = array();
	$forums = array();
	
	// Pull forumss from DB
	$sql = "SELECT * FROM forums";
	$result = mysql_query($sql);
	
	if (mysql_num_rows($result) == 0) { echo "no results"; }
	else {
		while ($row = mysql_fetch_array($result)) {
			extract($row);
			$forums[$forums_cat_id][$forums_order] = array('forumid' => '$forums_id', 'forumsubforumid' => '$forums_subforum_id', 'forumname' => '$forums_name');
		}
	}
	mysql_free_result($result);
	
	// Pull categories from DB
	$sql = "SELECT * FROM categories";
	$result = mysql_query($sql);
	
	if (mysql_num_rows($result) == 0) { echo "no results"; }
	else {
		while ($row = mysql_fetch_array($result)) {
			extract($row);
			$categories[$cat_order] = $cat_name;
			ksort($forums[$cat_id]); // Sort forums for this category by order
		}
		
		ksort($categories); // Sort categories by order
	}
	mysql_free_result($result);	
?>

Can someone help me create this? I'm really unsure as to how to get this to work right with assigning variables as arrays, i try not to use them at all

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 06 July 2006 - 10:58 PM

Assuming you data structure is

[pre]
categories            forum
----------            -----------
cat_id    <---------  cat_id
cat_name        +-->  forum_id
                |    forum_name
                +---  forum_subforum_id[/pre]

Try
<?php
include '../test/db.php';
echo "<SELECT name='forum'>\n";
$res1 = mysql_query("SELECT category_id, name from categories ORDER BY name") or die(mysql_error());
while (list($cid, $cname) = mysql_fetch_row($res1)) {
	echo "<OPTGROUP label='$cname'>\n";
	echo forumOptions($cid);
	echo "</OPTGROUP>\n";
}

function forumOptions($catid, $parent=0, $level=0) {
	$sql = "SELECT forum_id, forum_name
			FROM forums 
			WHERE forum_subforum_id = $parent
				AND category_id = $catid";
	$res = mysql_query($sql) or die(mysql_error());
	$str = '';
	while (list ($fid, $fname) = mysql_fetch_row($res)) {
		 $indent = str_repeat('- ', $level+1); 
		 $str .= "<OPTION value='$fid'>$indent $fname</option>\n";
		 $str .= forumOptions($catid, $fid, $level+1);
	}
	return $str;
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 07 July 2006 - 03:03 PM

Thanks a ton, it works perfectly

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users