Oct 28

Category Hierarchy into Select Box

For those interested in implementing a list of category items into a select box, here is a simple method for doing so. This will require only 1 query (assuming you store the category id and the parent id in the table).

Organize your categories into a set of arrays

function OrganizeCategories()
	$refs = array();
	$list = array();
	$sql = mysql_query("SELECT id_category, id_parent, name FROM tbl_categories	ORDER BY name");
	if(mysql_num_rows($sql) > 0)  {
		while($row = mysql_fetch_assoc($sql)) {
			$thisref = &$refs[ $row['id_category'] ];

			$thisref['parent_id'] = $row['id_parent'];
			$thisref['name'] = $row['name'];

			if ($row['id_parent'] == 0) {
				$list[ $row['id_category'] ] = &$thisref;
			} else {
				$refs[ $row['id_parent'] ]['children'][ $row['id_category'] ] = &$thisref;
	return $list;

Create the option values from the organizational array

function toSelect($list, $count = 0){

  $html = "";
  $count += 2;
  $spacers = "";
  for ($i = 0; $i < $count - 2; $i++)
	$spacers .= '=';
  if (!empty($spacers))
	$spacers .= '&gt;';
  foreach ($arr as $k => $v){
    $html .= '<option>' . $spacers .' ' . $v['name'];
    if (array_key_exists('children', $v)){
      $html .= toSelect($v['children'], $count);
    $html .= '</option>'.PHP_EOL;

  return $html;

Sample PHP code using the previous functions

$categories = OrganizeCategories();
$options = toSelect($categories);

echo '<select name="cat">'.$options.'</select>';

Assuming we have a full database of categories, it will output something like this

Mar 24

Get return value from Invoke()

So you may wonder how to get a return value from a function that requires you to Invoke(). It’s actually quite simple. You’ll need another delegate type, here is an example of returning the key of a tabControls selected tab:

public string CurrentTabKey()
    if (tabControl.InvokeRequired)
        return (string)tabControl.Invoke(
          new Func<String>(() => CurrentTabKey())
        return (string)tabControl.SelectedTab.Name;

Feb 24

Add jQuery code to AJAX loaded content

You may have noticed you cannot pass jQuery functions to content loaded using AJAX technology simply. One way is to use the jQuery .live() event. This can be used with a number of handlers and events to automatically pass functions to dynamically loaded content. The example below shows how you can add fancybox to a dynamically loaded container with class name “fancify”.

$('.fancify').live('click', function(){ 				
		'width'			: '40%',
		'height'		: '70%',
		'autoScale'		: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'			: 'iframe'
	return false;

Feb 23

Open links in default browser from WebBrowser control

If your looking to use C#’s web browser control but don’t want links to open in the same browser control (i.e. open them in a real browser like Chrome or Firefox), simply change the navigating handler to process the URL and fire the link in the diagnostics (which will open the URL in the user’s default browser). Here is what I used.

private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
     e.Cancel = true;

Feb 17

My Blog

My new blog. Mainly about programming tips and tricks I have used to create several projects.

» Newer posts