Jump to content

help me to sort my list in grouping


fun.maaz

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/254493-help-me-to-sort-my-list-in-grouping/
Share on other sites

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";
}

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

 

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";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.