Jump to content

how can i do this better


yonny

Recommended Posts

I do some php  >:(, mostly with built in functions tho.

i am helping a friend redo his website, he teaches karate.

i used a blog as the engine and some css template for the pages.

menus and content are pulled from the blog mysql. it is really easy to change and update the site.

but i spent allllll day and night working on the members area of it.

i know you guys could have done it in one hour flat, perhaps sooner, but i wanted to ask you guys for pointers on how to accomplish what i did easier and cleaner.

 

I wanted to make the members area easier to maintain and update, so i decided to just scan the directories recursively for changes and create jquery menus and submenus from there. sounded pretty easy when i started.

 

I had a big-time issue integrating the menu into the existing main menu so I had to put it as a menu of its own somewhere else. Mostly due to my inability to cleanly insert the

<ul>

s needed for the submenus.

 

ended up with this


# Adults

    * Adults Orange
    * Adults Purple
    * Adults Yellow

# Dragons

    * Dragons Orange
    * Dragons Purple
    * Dragons Yellow

# Instructors

    * Instructors Black
    * Instructors Red

# Juniors

    * Juniors Orange
    * Juniors Purple

from this


[/usr/local/www/data3/testsite/members/adults/orange/images] => Array
        (
            [0] => forward bow side.jpg
            [1] => ._forward bow side.jpg
            [2] => forward bow front.jpg
            [3] => ._forward bow front.jpg
        )

    [/usr/local/www/data3/testsite/members/adults/orange/videos/flv] => Array
        (
            [0] => side kick.flv
            [1] => palm heel.flv
            [2] => inward parry.flv
            [3] => outward parry.flv
        )

    [/usr/local/www/data3/testsite/members/adults/orange/documents] => Array
        (
            [0] => doc-testdocument.txt
        )

    [/usr/local/www/data3/testsite/members/adults/orange/certificates] => Array
        (
            [0] => cert-testcertificate.txt
        )

    [/usr/local/www/data3/testsite/members/adults/purple/videos/flv] => Array
        (
            [0] => axe kick.flv
        )

    [/usr/local/www/data3/testsite/members/adults/purple/certificates] => Array
        (
            [0] => cert-testcertificate.txt
        )

    [/usr/local/www/data3/testsite/members/adults/purple/documents] => Array
        (
            [0] => doc-testdocument.txt
            [1] => ._doc-testdocument.txt
        )

    [/usr/local/www/data3/testsite/members/adults/yellow/images] => Array
        (
            [0] => horse stance.jpg
            [1] => ._horse stance.jpg
            [2] => meditation stance.jpg
            [3] => ._meditation stance.jpg
            [4] => right neutral bow.jpg
            [5] => ._right neutral bow.jpg
            [6] => attention stance.jpg
            [7] => ._attention stance.jpg
            [8] => left neutral bow.jpg
            [9] => ._left neutral bow.jpg
        )

    [/usr/local/www/data3/testsite/members/adults/yellow/videos/flv] => Array
        (
            [0] => back kick.flv
            [1] => drag step.flv
            [2] => form one.flv
            [3] => cover.flv
            [4] => front kick.flv
            [5] => downward block.flv
            [6] => hammer fist.flv
            [7] => hand sword.flv
            [8] => inward block.flv
            [9] => moving blocking set.flv
            [10] => outward block.flv
            [11] => roundhouse kick.flv
            [12] => step drag.flv
            [13] => step thru.flv
            [14] => vertical punch.flv
            [15] => switch.flv
            [16] => straight punch.flv
            [17] => upward block.flv
            [18] => back fist.flv
            [19] => blocking set.flv
            [20] => extended outward block.flv
        )

    [/usr/local/www/data3/testsite/members/adults/yellow/certificates] => Array
        (
            [0] => cert-testcertificate.txt
        )

    [/usr/local/www/data3/testsite/members/adults/yellow/documents] => Array
        (
            [0] => doc-testdocument.txt
        )

    [/usr/local/www/data3/testsite/members/adults] => Array
        (
            [0] => donotdelte.txt
        )

as you can see it is matter of creating a menu from members folders named after a class, a belt color, a type of resource, etc.

so that when there is the need to add any of them the jquery menu and submenus would update themselves. no need to mess with html, php, mysql, blog etc. just drag and drop. so to speak.

 

well here is the code i used, i big portion of it borrowed, please help me understand how to make it better. I will hit the books and stuff, I just got to know at this time. thanks in advance.

 

<?php
$basedir = "/usr/local/www/data3/testsite/members"; // directory to scan
    $filearray= array();
    $directory= array();
    $placecount = 0;
    $directory[] = "/";
    do
    {
            $dh = opendir($basedir . $directory[$placecount]);
            while (($file = readdir($dh)) !== false)
            {
                    if(filetype($basedir . $directory[$placecount] . $file) == 'dir' AND (($file != '.') AND ($file != '..')))
                    {
                            $directory[] = $directory[$placecount] ."". $file."/";
                    }
                    elseif(filetype($basedir . $directory[$placecount].$file) != 'dir')
                    {
                           $filearray[] = $directory[$placecount];//.$file; only output dirs
                    }
            }
            closedir($dh);
        $placecount +=1;
    } while($placecount != count($directory)); 
    // Sort the array
natsort($filearray);
$filearray=array_unique($filearray);
$filearray=array_values($filearray);

foreach ( $filearray as $key => $value )
{
//remove unwanted files
if(!eregi("._|jpg|doc|txt|png|avi|flv|swf|certif|images|thumb|videos",$value)):
$var2 .="|$value";
    endif;
    
} 

	$var3=explode("|", $var2);

	$count_var=sizeof($var3);
	Preg_match_all("/\/\w{2,20}\//", $var2, $matches2);// match single folders ie /folder/
	$matches2=array_unique($matches2[0]);//group
	$matches2=array_values($matches2);//reset keys
		function sortByLength($a,$b){
		return (strlen($a) - strlen($b));
 		}

		usort($matches2,'sortByLength');//sort by length
	$first_folder = end($matches2); // get the longest string

	foreach ($matches2 as $name => $folders)
		{
		$single_folders .= "aa $folders bb|";// turn into string
		} 
	$longest_folder=strlen($first_folder);
	$single_folders=substr($single_folders,0,-1);

$count_matches2=sizeof($matches2);

print"<ul>\n";
foreach($var3 as $key => $val):
		$val_to_show=trim(ucwords(str_replace("/", " ", $val)));
		if(strlen($val) > 2):
		$link .="<li><a href=\"?to_get=$val\">$val_to_show</a>\n";
		endif;
endforeach;
/*
how $link is displayed
<ul>
</li>
</ul><li><a href="?to_get=/adults/">Adults</a>
<li><a href="?to_get=/adults/orange/">Adults Orange</a>
<li><a href="?to_get=/adults/purple/">Adults Purple</a>
<li><a href="?to_get=/adults/yellow/">Adults Yellow</a>
<li><a href="?to_get=/dragons/">Dragons</a>
<li><a href="?to_get=/dragons/orange/">Dragons Orange</a>
<li><a href="?to_get=/dragons/purple/">Dragons Purple</a>
<li><a href="?to_get=/dragons/yellow/">Dragons Yellow</a>
<li><a href="?to_get=/instructors/">Instructors</a>
<li><a href="?to_get=/instructors/black/">Instructors Black</a>
<li><a href="?to_get=/juniors/">Juniors</a>
<ul>
<li><a href="?to_get=/juniors/orange/">Juniors Orange</a>
<li><a href="?to_get=/juniors/purple/">Juniors Purple</a>
<li><a href="?to_get=/juniors/yellow/">Juniors Yellow</a>
*/
	preg_match_all("/<li><a href.*to_get.*>\w{1,20}<\/a>/",$link, $single_folders_matches); //match parent subfolders
	$count_single_folder_matches=sizeof($single_folders_matches[0]);
	foreach($single_folders_matches[0] as $key => $old):
	$link1=$link;
	$new="</li> </ul> $old <ul></li>";
			//print"$link1";
			unset($link);
    $link = str_replace($old, $new, $link1);
			endforeach;
			print"$link";
print"</li></ul></li><li>";

?>

 

Link to comment
Share on other sites

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.