TapeGun007 Posted June 7, 2010 Share Posted June 7, 2010 Ok, I've got an issue, and I'm having a really hard time solving this one. Attached is a picture, so you can see what this page does, it will explain a lot: The Project: The user can click on a letter, and all songs that start with that letter will appear. In this first example the user has clicked on the letter "B". Pretend the "[Download]" option in the picture doesn't work (it's no concern at the moment), but the [Open] does work. If a user clicks on [Open] the file will stream and open. The output is all the songs that start with the letter "b" or "B". I use this line of code to compare the strings: if (strtoupper($selection)==strtoupper($compare)){ This way it lists all files regardless of whether or not it starts with an uppercase or lowercase letter. Problem #1: As you can see in the example, it lists all files that start with a Capital "B" alphabetically and then starts over and lists all the files starting with a lowercase "b" alphabetically. Problem #2: If I convert all the filenames to uppercase, then the [Open] link no longer works, because the server is case sensitive so the filename has to be exactly as it is in the directory. So, if I converted "baby girl.mp3" to "BABY GIRL.MP3" so all the files would sort, then the open option will no longer work. Is there some simple way to get around this? Once I get past this issue, then that might solve my search box issues as well. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/204061-uppercase-and-lowercase-issues/ Share on other sites More sharing options...
kenrbnsn Posted June 7, 2010 Share Posted June 7, 2010 You haven't shown us how you're sorting the filenames, but you probably want to look at the function natcasesort. Ken Quote Link to comment https://forums.phpfreaks.com/topic/204061-uppercase-and-lowercase-issues/#findComment-1068829 Share on other sites More sharing options...
TapeGun007 Posted June 7, 2010 Author Share Posted June 7, 2010 sort ($All_Files); That's all I was using. The sort you are suggesting sorts the B's like this which is still not alphabetical: [ Download ] [ Open ] Breathe On Me60bpm.acd-bak.mp3 11.46 MB [ Download ] [ Open ] bounce.mp3 2.72 MB [ Download ] [ Open ] Blessed Be Your Name.mp3 5.97 MB [ Download ] [ Open ] blessthelordloop80.mp3 14.26 MB [ Download ] [ Open ] Bound (G).pdf 0.08 MB Quote Link to comment https://forums.phpfreaks.com/topic/204061-uppercase-and-lowercase-issues/#findComment-1068840 Share on other sites More sharing options...
kenrbnsn Posted June 7, 2010 Share Posted June 7, 2010 When I write a test script using those files in an array, using natcasesort does the right thing: <?php $ary = array('Breathe On Me60bpm.acd-bak.mp3','bounce.mp3','Blessed Be Your Name.mp3','blessthelordloop80.mp3','Bound (G).pdf'); print_r($ary); natcasesort($ary); print_r($ary); ?> produces Array ( [0] => Breathe On Me60bpm.acd-bak.mp3 [1] => bounce.mp3 [2] => Blessed Be Your Name.mp3 [3] => blessthelordloop80.mp3 [4] => Bound (G).pdf ) Array ( [2] => Blessed Be Your Name.mp3 [3] => blessthelordloop80.mp3 [1] => bounce.mp3 [4] => Bound (G).pdf [0] => Breathe On Me60bpm.acd-bak.mp3 ) How are you generating the display after using the sort? Ken Quote Link to comment https://forums.phpfreaks.com/topic/204061-uppercase-and-lowercase-issues/#findComment-1068852 Share on other sites More sharing options...
TapeGun007 Posted June 7, 2010 Author Share Posted June 7, 2010 Ken, I did the same test you just did. hehe... yes, and it sorts correctly then. Here is the entire code. Now, don't be too hard on me, I'm new to PHP and am converting from classic ASP. Keep in mind this code is set up for either a filename search OR if they click on a letter. <?php include("components/header.php"); ?> <h1>Search</h1> Currently, this search will list all files that start with a capital letter alphabetically first. Then it will list all files that do not start with a capital letter alphabetically.<p> <form action="download_search.php?type=search" method="post"> <table cellpadding="3" cellspacing="3" border="0"> <tr> <td colspan="2"> <a href="download_search.php?letter=a">A</a>- <a href="download_search.php?letter=b">B</a>- <a href="download_search.php?letter=c">C</a>- <a href="download_search.php?letter=d">D</a>- <a href="download_search.php?letter=e">E</a>- <a href="download_search.php?letter=f">F</a>- <a href="download_search.php?letter=g">G</a>- <a href="download_search.php?letter=h">H</a>- <a href="download_search.php?letter=i">I</a>- <a href="download_search.php?letter=j">J</a>- <a href="download_search.php?letter=k">K</a>- <a href="download_search.php?letter=l">L</a>- <a href="download_search.php?letter=m">M</a>- <a href="download_search.php?letter=n">N</a>- <a href="download_search.php?letter=o">O</a>- <a href="download_search.php?letter=p">P</a>- <a href="download_search.php?letter=q">Q</a>- <a href="download_search.php?letter=r">R</a>- <a href="download_search.php?letter=s">S</a>- <a href="download_search.php?letter=t">T</a>- <a href="download_search.php?letter=u">U</a>- <a href="download_search.php?letter=v">V</a>- <a href="download_search.php?letter=w">W</a>- <a href="download_search.php?letter=x">X</a>- <a href="download_search.php?letter=y">Y</a>- <a href="download_search.php?letter=z">Z</a> </td> </tr> <tr> <td><input name="searchresults" type="text" class="input" /> <input value="Submit" type="submit" /></td> </tr> </table> <p> </form> <?php $selection = $_GET['letter']; if (isset($_POST['searchresults'])){ $str = $_POST['searchresults']; } if ($handle = opendir('Music_Library')) { $y==0; /* Read the files in the directory, then store it in an array to sort it alphabetically */ $All_Files = array(); while (false !== ($file = readdir($handle))) { /* Weed out the . and .. for going up one directory, so this keeps those from showing */ if ( $file != '.' && $file != '..' ) { // Compare the first letter of the filename to the letter that was selected. $compare=substr("$file",0,1); if (strtoupper($selection)==strtoupper($compare)){ $y++; $File_Size=filesize('Music_Library/'.$file); $File_Size=$File_Size/1048576; $Total_File_Size = $Total_File_Size + $File_Size; $All_Files[$y]['FileName']=$file; $All_Files[$y]['FileSize']=number_format($File_Size,2)." MB"; } /* Check the if the string is populated, then set $match */ if (isset($str)){ $match = strpos($file,$str); }/* Set to false or it will run anyway */ else{ $match=false; } if ($match===false){} else { $y++; $File_Size=filesize('Music_Library/'.$file); $File_Size=$File_Size/1048576; $Total_File_Size = $Total_File_Size + $File_Size; $All_Files[$y]['FileName']=$file; $All_Files[$y]['FileSize']=number_format($File_Size,2)." MB"; } } } closedir($handle); // Sort the array natcasesort($All_Files); $counter==1; echo "<table border='0'>"; echo "<tr>"; echo "<td bgcolor='#333333' colspan='2' class='ContentWhite' align='center'> Controls </td><td bgcolor='#333333' class='ContentWhite' align='center' width='300'> File Name </td><td bgcolor='#333333' class='ContentWhite' align='center'> File Size </td>"; foreach ($All_Files as $tempone) { echo "<tr>"; $counter++; $bgcolor = ($counter % 2)?"#ffffff":"#d9ece7"; foreach ($tempone as $key=>$temptwo) { If ($temptwo !== end($tempone)){ /* Display the filename */ echo "<td bgcolor='".$bgcolor."'> <a href=\"download_file.php?file=$temptwo\">[ Download ]</a> </td>"; echo "<td bgcolor='".$bgcolor."'> <a href=\"http://www.sanjosechoir.com/Music_Library/".$temptwo."\" target='_new'>[ Open ]</a> </td>"; echo "<td bgcolor='".$bgcolor."'> $temptwo </td>"; } else { /* Display the size of the file */ echo "<td bgcolor='".$bgcolor."' align='right'> ".$temptwo." </td>"; } } echo "</tr>"; } echo "<tr><td colspan='3' align='right'><b>Total File Size:</b></td><td align='right'><b>".number_format($Total_File_Size,2)." MB</b></td></tr>"; echo "</table>"; } ?> <?php include("components/footer.php"); ?> On my last server, I didn't have to worry about doing anything but changing the case and doing the sort. Quote Link to comment https://forums.phpfreaks.com/topic/204061-uppercase-and-lowercase-issues/#findComment-1068860 Share on other sites More sharing options...
TapeGun007 Posted June 7, 2010 Author Share Posted June 7, 2010 Anyone else have some suggestions as to why my sorting isn't working properly as Ken suggested? Quote Link to comment https://forums.phpfreaks.com/topic/204061-uppercase-and-lowercase-issues/#findComment-1069022 Share on other sites More sharing options...
kenrbnsn Posted June 7, 2010 Share Posted June 7, 2010 The problem is that you're trying to sort a multi-dimensioned array. Here's your code with some modification: 1) Use the glob function to get all the files into a temporary array 2) sort that array 3) loop through that array to build the display. <?php include("components/header.php"); ?> <h1>Search</h1> Currently, this search will list all files that start with a capital letter alphabetically first. Then it will list all files that do not start with a capital letter alphabetically.<p> <table cellpadding="3" cellspacing="3" border="0"> <tr> <td colspan="2"> <a href="download_search.php?letter=a">A</a>- <a href="download_search.php?letter=b">B</a>- <a href="download_search.php?letter=c">C</a>- <a href="download_search.php?letter=d">D</a>- <a href="download_search.php?letter=e">E</a>- <a href="download_search.php?letter=f">F</a>- <a href="download_search.php?letter=g">G</a>- <a href="download_search.php?letter=h">H</a>- <a href="download_search.php?letter=i">I</a>- <a href="download_search.php?letter=j">J</a>- <a href="download_search.php?letter=k">K</a>- <a href="download_search.php?letter=l">L</a>- <a href="download_search.php?letter=m">M</a>- <a href="download_search.php?letter=n">N</a>- <a href="download_search.php?letter=o">O</a>- <a href="download_search.php?letter=p">P</a>- <a href="download_search.php?letter=q">Q</a>- <a href="download_search.php?letter=r">R</a>- <a href="download_search.php?letter=s">S</a>- <a href="download_search.php?letter=t">T</a>- <a href="download_search.php?letter=u">U</a>- <a href="download_search.php?letter=v">V</a>- <a href="download_search.php?letter=w">W</a>- <a href="download_search.php?letter=x">X</a>- <a href="download_search.php?letter=y">Y</a>- <a href="download_search.php?letter=z">Z</a> </td> </tr> <tr> <td> <form action="download_search.php?type=search" method="post"> <input name="searchresults" type="text" class="input" /> <input name="submit" value="Submit" type="submit" /> </form> </td> </tr> </table> <p> <?php $selection = $_GET['letter']; if (isset($_POST['searchresults'])){ $str = $_POST['searchresults']; } $All_files = array(); $hold_files = glob('Music_Library/*.*'); natcasesort($hold_files); $y = 0; foreach($hold_files as $file) { if (strtoupper($selection)==strtoupper(substr($file,0,1))){ $y++; $File_Size=filesize('Music_Library/'.$file); $File_Size=$File_Size/1048576; $Total_File_Size = $Total_File_Size + $File_Size; $All_Files[$y]['FileName']=$file; $All_Files[$y]['FileSize']=number_format($File_Size,2)." MB"; } $match = false; /* Check the if the string is populated, then set $match */ if (isset($str)){ $match = strpos($file,$str); }/* Set to false or it will run anyway */ if ($match !== false) { $y++; $File_Size=filesize($file)/1048576; $Total_File_Size += $File_Size; $All_Files[$y]['FileName']=$file; $All_Files[$y]['FileSize']=number_format($File_Size,2)." MB"; } } $counter==1; echo "<table border='0'>"; echo "<tr>"; echo "<td bgcolor='#333333' colspan='2' class='ContentWhite' align='center'> Controls </td><td bgcolor='#333333' class='ContentWhite' align='center' width='300'> File Name </td><td bgcolor='#333333' class='ContentWhite' align='center'> File Size </td>"; foreach ($All_Files as $tempone) { echo "<tr>"; $counter++; $bgcolor = ($counter % 2)?"#ffffff":"#d9ece7"; foreach ($tempone as $key=>$temptwo) { If ($temptwo !== end($tempone)){ /* Display the filename */ echo "<td bgcolor='".$bgcolor."'> <a href=\"download_file.php?file=$temptwo\">[ Download ]</a> </td>"; echo "<td bgcolor='".$bgcolor."'> <a href=\"http://www.sanjosechoir.com/Music_Library/".$temptwo."\" target='_new'>[ Open ]</a> </td>"; echo "<td bgcolor='".$bgcolor."'> $temptwo </td>"; } else { /* Display the size of the file */ echo "<td bgcolor='".$bgcolor."' align='right'> ".$temptwo." </td>"; } } echo "</tr>"; } echo "<tr><td colspan='3' align='right'><b>Total File Size:</b></td><td align='right'><b>".number_format($Total_File_Size,2)." MB</b></td></tr>"; echo "</table>"; include("components/footer.php"); ?> Note: I haven't checked the code for syntax errors. Ken Quote Link to comment https://forums.phpfreaks.com/topic/204061-uppercase-and-lowercase-issues/#findComment-1069088 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.