6 Replies - 1372 Views - Last Post: 01 December 2008 - 12:08 AM Rate Topic: -----

#1 Gluebag   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-November 08

Help setting up php to refine results on opendir via A-F, G-M etc.

Posted 29 November 2008 - 01:29 PM

I'm trying to setup an A-F button on my site, that basically opens a directory (/SheetMusic) and then echo's all files that begin with the letter A-F. I tried coming up with the code and just got lost, I finally thought I had reasonable code for just an A button, to at least start somewhere..., but it failed me as well. My experience in php is very minimal :(, so my code may make you "facepalm"
To show you what I mean:

Main Page

Location of A-F button

(This page contains the form with the button:)
<form action="ArtistSearch.php" method="get">

	<input type="hidden" name="A-FButton" value="a"></input>

	<input type="submit" value="A-F"></input>

	</form>



As you can see I have it set to just send the value of "a" to ArtistSearch.php, because I have NOO clue how to setup the A-F, so I just wanted to see if I could first code the value="a".

This is the code for ArtistSearch.php:
Like I said I tried, I could not figure out the correct code to do A-F, so this is just for A, if you could help me with A-F, it would be GREATLY appreciated!

<?php

$a = $_GET["A-FButton"];

$fl = opendir('/var/www/SheetMusic/');

while (false!==($file=readdir($fl)))

{

	$tfile = $file;

	$file = strtolower($file);

	$s = strtolower($a);

	if (substr("$file",1)==substr("$s",1))

	{

	//Where I Echo the Results
	echo ("$file");

	}

}

?>



After doing all this, and thinking it was correct, it just echo's "." which... I cannot figure out for the life of me. Once again, any help is appreciated!

This post has been edited by Gluebag: 29 November 2008 - 01:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help setting up php to refine results on opendir via A-F, G-M etc.

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Help setting up php to refine results on opendir via A-F, G-M etc.

Posted 29 November 2008 - 02:39 PM

This code will list all files whose first letter comes before 'g'
<?php
$cwd = getcwd();
$filenames = scandir($cwd);
$i = 1;
do {
$i=$i+1;
echo $filenames[$i]."<br>";
} while (substr($filenames[$i],1,1) < "g");
?>


Was This Post Helpful? 0
  • +
  • -

#3 Gluebag   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-November 08

Re: Help setting up php to refine results on opendir via A-F, G-M etc.

Posted 29 November 2008 - 03:33 PM

View PostCTphpnwb, on 29 Nov, 2008 - 01:39 PM, said:

This code will list all files whose first letter comes before 'g'
<?php
$cwd = getcwd();
$filenames = scandir($cwd);
$i = 1;
do {
$i=$i+1;
echo $filenames[$i]."<br>";
} while (substr($filenames[$i],1,1) < "g");
?>



You my friend, are amazing :D,
ThankS !
Was This Post Helpful? 0
  • +
  • -

#4 Gluebag   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-November 08

Re: Help setting up php to refine results on opendir via A-F, G-M etc.

Posted 30 November 2008 - 03:47 PM

Uggggg, I tried using what you wrote, but I think it was looping until the ASCII value of "g" was reached, not the alphabetical, so I wrote this up, and it works, only the echo'd results weren't alphabetized, so I tried storing the results in an array, then doing the "sort" function, and it outputed the results as just the word "Array".

Example: http://popsheets.com...rch.php?val=a-f

Here is the code for it :
(And as always, help is GREATLY appreciated!)

<?php
$path = "/var/www/SheetMusic";

$alph = "abcdefghijklmnopqrstuvwxyz";

$s = $_GET["val"];

$folder = opendir($path);
$paths = array();

echo("<ul>");
while (false!==($fl = readdir($folder)))

{

   $fll = strtolower($fl);
   			
	if ($s=="a-f")

	{
		$paths[]="$fll";
		sort($paths);

		for ($cur = 0;$cur<=5;$cur++)

		{

			if (substr($fll,0,1)==substr($alph,$cur,1))

			{
			echo("<li>$paths</li>");	

			}


		}

	}

}
closedir($folder);
echo("</ul>");

?>


Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Help setting up php to refine results on opendir via A-F, G-M etc.

Posted 30 November 2008 - 04:14 PM

I tried not to change your code too much.
<?php
$path = getcwd(); //"/var/www/SheetMusic";

$alph = "abcdefghijklmnopqrstuvwxyz";

$s = $_GET["val"];

$folder = opendir($path);
$paths = array();

echo("<ul>");
while ($fl = readdir($folder))
{
	  $fll = strtolower($fl);

	 	 if ($s=="a-f")
		 	 {
	 	 	 for ($cur = 0;$cur<=5;$cur++)
	 	 	 	 {
	 	 	 	 if (substr($fll,0,1)==substr($alph,$cur,1))
 	 	 	 	 	 {
		 	 	 	 $paths[]="$fll";
					 }
	 	 	 	 }
	 	 }
}
sort($paths);
closedir($folder);
for ($i=0; $i< count($paths); $i++)
	{
	echo $paths[$i]."<br>";
	}
echo("</ul>");
?>

Was This Post Helpful? 0
  • +
  • -

#6 Gluebag   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-November 08

Re: Help setting up php to refine results on opendir via A-F, G-M etc.

Posted 30 November 2008 - 04:24 PM

You're a saint man.... Thanks so much, so far you've been the only one to help me find the solutions to all my problems :D
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Help setting up php to refine results on opendir via A-F, G-M etc.

Posted 01 December 2008 - 12:08 AM

Glad to help.

Here's a version that let's you choose from four parts of the alphabet. It converts everything to caps because php has some weird ideas about sorting:
<?php
$posted = array(0,1,2,3);
if (isset($_POST['state']))
	{
	$x = (int)$_POST['state'];
	switch ($x)
		{
		case 0:
		$ch0 = 'checked="checked"';
		$A = "A";
		$Z = "F";
		break;
		case 1:
		$ch1 = 'checked="checked"';
		$A = "G";
		$Z = "L";
		break;
		case 2:
		$ch2 = 'checked="checked"';
		$A = "M";
		$Z = "R";
		break;
		case 3:
		$ch3 = 'checked="checked"';
		$A = "S";
		$Z = "Z";
		break;
		}
	$output = "<b>".$A." - ".$Z."</b><br>";
	$cwd = getcwd();	
	$filenames = scandir($cwd);
	for ($i=0;$i<count($filenames);$i++)
		{
		$filenames[$i]=strtoupper($filenames[$i]);
		}
	sort($filenames);
	$i = 0;
	$firstletter = substr($filenames[$i],0,1);
	while ($firstletter <= $Z && $i <= count($filenames))
		{
		if ($firstletter >= $A)
			{
			 $output .= $filenames[$i]."<br>";
			}
		$i=$i+1;
		$firstletter = substr($filenames[$i],0,1);
		}		
}
?>

<form method="post" action="<?php echo $php_self ?>"> 
<input type="radio" name="state" value="<?php echo $posted[0] ?>" <?php echo $ch0; ?> onclick="this.form.submit();">A-F</input> 
<input type="radio" name="state" value="<?php echo $posted[1] ?>" <?php echo $ch1; ?> onclick="this.form.submit();" >G-L</input>
<input type="radio" name="state" value="<?php echo $posted[2] ?>" <?php echo $ch2; ?> onclick="this.form.submit();" >M-R</input>
<input type="radio" name="state" value="<?php echo $posted[3] ?>" <?php echo $ch3; ?> onclick="this.form.submit();" >S-Z</input>
</form>
<?php
echo $output;
?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1