Newbie Code Streamlining help

Hello All

Newbie to both PHP and the Forum.

I have two functions that are almost the sanme with a few key differences. Now I am new to PHP but I am sure that this would be considered inefficient. So I was wondering if there was any way in which I could streamline these functions somehow. The two functions are as follows:


function jplayer_setup()
{	
	$directory_url = check_path();
	
	$folders = access_directory($directory_url);
	
	$folder_count = count($folders);
	
	$count = "1";
	
	for ($i = 0; $i < $folder_count; $i++)
	{
		$files = access_directory($directory_url.$folders[$i]);
		
		$file_count - count($files);
		
		for ($x = 0; $x < $folder_count; $x++)
		{
			$count_index = ($count == "1" ? "" : $count);
			
			jplayer_script();
			
			$count = $count + 1;
		}
		unset($files);
	}
}



function list_samples()
{	
	$directory_url = check_path();
	
	$folders = access_directory($directory_url);
	
	$folder_count = count($folders);
	
	$count = "1";
	
	for ($i = 0; $i < $folder_count; $i++)
	{
		print_headers($folders[$i]);
			
		$files = access_directory($directory_url.$folders[$i]);
		
		$file_count = count($files);
			
		for ($x = 0; $x < $file_count; $x++)
		{
			
			$count_index = ($count == "1" ? "" : $count);
			
			$file_name = str_replace(".mp3", "", $files[$x]);
			
			print_samples($file_name, $count_index);
			
			$count = $count + 1;
		}
			
		unset($files);			
	}
}

In order that you might better understand the context of it the whole file looks like this:



<?php

function access_directory($directory_url)
{
	$excludes = array(".","..",".DS_Store",".svn");

	$directory = @opendir($directory_url) or die("Unable to open $directory_url");
	
	$array = get_files($directory);

	closedir($directory);
	
	sort($array);
	
	return $array;
}



function build_menu($directory_url)
{	
	$array = access_directory($directory_url);
	
	$item_count = count($array);
	
	for ($i = 0; $i < $item_count; $i++)
	{
		print "<a href='index.php?path=".$directory_url.$array[$i]."/"."'>$array[$i]</a>";
	}
	
}



function check_path()
{
	$directory_url = ($_GET['path'] == "" ? "audio_files/voices/female/" : $_GET['path']);
	
	return $directory_url;
}



function get_files($directory)
{
	$excludes = array(".","..",".DS_Store",".svn");
	
	while ($file = readdir($directory))
	{
		if (!in_array("$file", $excludes))
		{
			$array[] = $file;
		}
	}
	return $array;
}



function jplayer_setup()
{	
	$directory_url = check_path();
	
	$folders = access_directory($directory_url);
	
	$folder_count = count($folders);
	
	$count = "1";
	
	for ($i = 0; $i < $folder_count; $i++)
	{
		$files = access_directory($directory_url.$folders[$i]);
		
		$file_count - count($files);
		
		for ($x = 0; $x < $folder_count; $x++)
		{
			$count_index = ($count == "1" ? "" : $count);
			
			jplayer_script();
			
			$count = $count + 1;
		}
		unset($files);
	}
}



function list_samples()
{	
	$directory_url = check_path();
	
	$folders = access_directory($directory_url);
	
	$folder_count = count($folders);
	
	$count = "1";
	
	for ($i = 0; $i < $folder_count; $i++)
	{
		print_headers($folders[$i]);
			
		$files = access_directory($directory_url.$folders[$i]);
		
		$file_count = count($files);
			
		for ($x = 0; $x < $file_count; $x++)
		{
			
			$count_index = ($count == "1" ? "" : $count);
			
			$file_name = str_replace(".mp3", "", $files[$x]);
			
			print_samples($file_name, $count_index);
			
			$count = $count + 1;
		}
			
		unset($files);			
	}
}

?>

Any help would be greatly appreciated.

Thank You.

D :slight_smile: