fun.maaz Posted January 6, 2012 Share Posted January 6, 2012 i have list of movies A to Z in array i can do simple sort but i want them to sort in grouping like this A [movie name], [movie name], [movie name], [movie name], B [movie name] [movie name] [movie name] [movie name] [movie name] C [movie name] [movie name] [movie name] [movie name] D [movie name] [movie name] [movie name] [movie name] and go on all movies name will be links but A,B,C,D will be remain simple how can i do this i wanna show list like this see this website please visit http://mp3hungama.com/music/genre_albums.php?id=3 thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 6, 2012 Share Posted January 6, 2012 It would be helpful for you to show your current code for us to implement something into it. But, generically speaking you would just check the first letter of each title as you process the records. If the letter is different from the last letter then display a new letter header. Although, one thing you would need to decide is if you want to show the header if there are no titles for that letter. If so, that makes the process a little more difficult, but not terribly so. EDIT: Rough example $query = "SELECT title FROM movies ORDER BY title"; $result = mysql_query($query); $current_letter = ''; while($row = mysql_fetch_assoc($result)) { $this_letter = strtoupper(substr($row['title'])); if($current_letter != $this_letter) { $current_letter = $this_letter; echo "<h1>{$current_letter}</h1>\n"; } echo "{$title}<br>\n"; } Quote Link to comment Share on other sites More sharing options...
fun.maaz Posted January 6, 2012 Author Share Posted January 6, 2012 my code is this...right now i am not using any type of db.i am just doing in simple php page. <?php class template{ function list_films_a_to_n() { return array( "A56"=>"Ai45.php", "Nomi"=>"nomi.php", "Nomi Zain"=>"nomi.php", "Nomi zain"=>"nomi.php", "Fa"=>"faa.php", "Haa"=>"ha.php", "A"=>"a.php", "Z"=>"z.php", ); } } <div class="listAtoZ"> <?php include_once("template.php"); $temp1=new template(); $list=$temp1->list_films_a_to_n(); ksort($list); echo "<table id='AtoZ' border='1px'>"; foreach($list as $keyvalue => $page) { echo "<tr>"; echo "<td><a href=$page>$keyvalue</a></td>"; echo "</tr>"; } echo "</table>"; ?> </div> problem is all my array listed with links but i want only movies with link and A,B,C,D simply right without link Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2012 Share Posted January 6, 2012 Store the first letter of the title in a variable on each iteration of a loop over the array, then when it changes, output a <tr><td> set with just the first letter. Here's an example of one way you could do it. <?php $titles = array( 'alpha.php' => 'Alpha', 'alan.php' => 'alan', 'Snake.php' => 'Snake', 'softball.php' => 'softball', 'banjo.php' => 'banjo', 'california.php' => 'california'); $first = ''; $titles = array_flip($titles); natcasesort($titles); echo "<table>\n"; foreach( $titles as $k => $v ) { echo strtolower($first) !== strtolower(substr($v, 0, 1)) ? "<tr><td>" . strtoupper(substr($v, 0, 1)) . "</td></tr>\n" : ''; echo "<tr><td>Value: $v -- Key: $k</td></tr>\n"; $first = substr($v, 0, 1); } echo "</table>\n"; Quote Link to comment Share on other sites More sharing options...
fun.maaz Posted January 6, 2012 Author Share Posted January 6, 2012 thanks man Quote Link to comment 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.