Jump to content

[SOLVED] Sorting


spider22

Recommended Posts

Hello,

 

I am trying to build a page like this ...

http://www.indiafm.com/movies/musicreviewslist.html

 

Here is my code

 

<?php

// Make a MySQL Connection

 

 

// Get all the data from the "example" table

$result = mysql_query("SELECT * FROM `jos_content` WHERE catid = '22' ORDER BY title")

or die(mysql_error());

 

echo "<table border='1'>";

echo "<tr> <th>Drama Review</th> <th>Hits</th> </tr>";

// keeps getting the next row until there are no more to get

while($row = mysql_fetch_array( $result )) {

// Print out the contents of each row into a table

echo "<tr><td>";

echo $row['title'];

echo "</td><td>";

echo $row['hits'];

echo "</td></tr>";

}

 

echo "</table>";

?>

 

How can I sort this like "A" title come in first then <BR> then list all "B" titles and so on...

 

Please help

thanks

Link to comment
Share on other sites

<?php
// Make a MySQL Connection


// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM `jos_content` WHERE catid = '22' ORDER BY title")
or die(mysql_error());

echo "<table border='1'>";
echo "<tr> <th>Drama Review</th> <th>Hits</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
   sort($row);
   // Print out the contents of each row into a table
   echo "<tr><td>";
   echo $row['title'];
   echo "</td><td>";
   echo $row['hits'];
   echo "</td></tr>";
}

echo "</table>";
?>

Link to comment
Share on other sites

@atlanta, he's talking about any title starting with "a" first, etc...

 

Try this.

 

<?php
// Make a MySQL Connection


// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM `jos_content` WHERE catid = '22' ORDER BY title ASC")
or die(mysql_error());

echo "<table border='1'>";
echo "<tr> <th>Drama Review</th> <th>Hits</th> </tr>";
// keeps getting the next row until there are no more to get
$last_letter = null;
while($row = mysql_fetch_array( $result )) {

$current_letter = strtolower($row['title'][0]);
if($current_letter != $last_letter){//this means the first letter changed
echo "</table><table>";
}

   // Print out the contents of each row into a table
   echo "<tr><td>";
   echo $row['title'];
   echo "</td><td>";
   echo $row['hits'];
   echo "</td></tr>";

$last_letter = $current_letter;//always change this so it's based on the previous
}

echo "</table>";
?>

 

The idea is similar to alternating colors. Create a "break" each time the first letter changes (compared to the last result).

Link to comment
Share on other sites

You could use range() in this.

 

$letters = range('a','z');
$numbers = range('0','9');

foreach($letters AS $l){
$sql = "SELECT * FROM `table` WHERE `field` LIKE '$l%' ORDER BY field DESC";
$res = mysql_query($sql) or die(mysql_error());

//while statement
}

# numbers would be the same thing

Link to comment
Share on other sites

You could use range() in this.

 

$letters = range('a','z');
$numbers = range('0','9');

foreach($letters AS $l){
$sql = "SELECT * FROM `table` WHERE `field` LIKE '$l%' ORDER BY field DESC";
$res = mysql_query($sql) or die(mysql_error());

//while statement
}

# numbers would be the same thing

 

This is very inefficient... For one, that's 26 queries on who knows how big the results returned can be.... However, I just tested the following code, it works for what atlanta is looking for...

 

 

<?php
// Make a MySQL Connection


// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM `jos_content` WHERE catid = '22' ORDER BY title ASC")
or die(mysql_error());

echo "<table border='1'>";
echo "<tr> <th>Drama Review</th> <th>Hits</th> </tr>";
// keeps getting the next row until there are no more to get
$last_letter = null;
while($row = mysql_fetch_array( $result )) {

$current_letter = strtolower($row['title'][0]);
if($current_letter != $last_letter){//this means the first letter changed
echo "</table><table>";
}

   // Print out the contents of each row into a table
   echo "<tr><td>";
   echo $row['title'];
   echo "</td><td>";
   echo $row['hits'];
   echo "</td></tr>";

$last_letter = $current_letter;//always change this so it's based on the previous
}

echo "</table>";
?>

Link to comment
Share on other sites

kratsg :

 

very good...

 

is it possible to display letter "A" or ?? whenever "Create a "break" each time the first letter changes " occurs

 

thanks

 

That's probably the easiest ever.. o_O Think about it, each time the letter change occurs, we want to display the new letter at the top of the table..

 

<?php
// Make a MySQL Connection


// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM `jos_content` WHERE catid = '22' ORDER BY title ASC")
or die(mysql_error());

echo "<table border='1'><tr><th>Drama Review</th><th>Hits</th></tr><tr><td colspan='2'><center>A</center></td></tr>";
// keeps getting the next row until there are no more to get
$last_letter = null;
while($row = mysql_fetch_array( $result )) {

$current_letter = strtolower($row['title'][0]);
if($current_letter != $last_letter){//this means the first letter changed
echo "</table>";
echo "<table border='1'><tr><th>Drama Review</th><th>Hits</th></tr><tr><td colspan='2'><center>$current_letter</center></td></tr>";
}

   // Print out the contents of each row into a table
   echo "<tr><td>";
   echo $row['title'];
   echo "</td><td>";
   echo $row['hits'];
   echo "</td></tr>";

$last_letter = $current_letter;//always change this so it's based on the previous
}

echo "</table>";
?>

 

Now, this code has a few assumptions (to make it as simple as possible). You will always have a movie title that starts with 'A' so it populates the 'A' table first, then it should do the rest just as easily.

Link to comment
Share on other sites

kratsg :

 

very good...

 

is it possible to display letter "A" or ?? whenever "Create a "break" each time the first letter changes " occurs

 

thanks

 

That's probably the easiest ever.. o_O Think about it, each time the letter change occurs, we want to display the new letter at the top of the table..

 

<?php
// Make a MySQL Connection


// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM `jos_content` WHERE catid = '22' ORDER BY title ASC")
or die(mysql_error());

echo "<table border='1'><tr><th>Drama Review</th><th>Hits</th></tr><tr><td colspan='2'><center>A</center></td></tr>";
// keeps getting the next row until there are no more to get
$last_letter = null;
while($row = mysql_fetch_array( $result )) {

$current_letter = strtolower($row['title'][0]);
if($current_letter != $last_letter){//this means the first letter changed
echo "</table>";
echo "<table border='1'><tr><th>Drama Review</th><th>Hits</th></tr><tr><td colspan='2'><center>$current_letter</center></td></tr>";
}

   // Print out the contents of each row into a table
   echo "<tr><td>";
   echo $row['title'];
   echo "</td><td>";
   echo $row['hits'];
   echo "</td></tr>";

$last_letter = $current_letter;//always change this so it's based on the previous
}

echo "</table>";
?>

 

Now, this code has a few assumptions (to make it as simple as possible). You will always have a movie title that starts with 'A' so it populates the 'A' table first, then it should do the rest just as easily.

kratsg:

 

"A" is coming twice ... any ideas?

 

thanks

To display letter in Caps .. is it possible to use

 

strtoupper($..)

 

?

 

thanks

 

Ok, let's deal with all of this at once! ucfirst() is a shorter script, and works a lot better since it only has to capitalize the FIRST character, simple enough.

 

("A" is showing twice because of my mistake, sorry)

 

<?php
// Make a MySQL Connection


// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM `jos_content` WHERE catid = '22' ORDER BY title ASC")
or die(mysql_error());

echo "<table border='1'><tr><th>Drama Review</th><th>Hits</th></tr>";
// keeps getting the next row until there are no more to get
$last_letter = null;
while($row = mysql_fetch_array( $result )) {

$current_letter = strtolower($row['title'][0]);
if($current_letter != $last_letter){//this means the first letter changed
echo "<tr><td colspan='2' style='background-color:black;color:white;'><center><b>A</b></center></td></tr>";
}

   // Print out the contents of each row into a table
   echo "<tr><td>";
   echo $row['title'];
   echo "</td><td>";
   echo $row['hits'];
   echo "</td></tr>";

$last_letter = $current_letter;//always change this so it's based on the previous
}

echo "</table>";
?>

 

Try that. Instead of splitting it up by tables, we'll use the 'A' , 'B'  as divider rows (black background, white text on those).

Link to comment
Share on other sites

Forgot to change 'A' into the variable... xD

 

<?php
// Make a MySQL Connection


// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM `jos_content` WHERE catid = '22' ORDER BY title ASC")
or die(mysql_error());

echo "<table border='1'><tr><th>Drama Review</th><th>Hits</th></tr>";
// keeps getting the next row until there are no more to get
$last_letter = null;
while($row = mysql_fetch_array( $result )) {

$current_letter = strtolower($row['title'][0]);
if($current_letter != $last_letter){//this means the first letter changed
echo "<tr><td colspan='2' style='background-color:black;color:white;'><center><b>".ucfirst($current_letter)."</b></center></td></tr>";
}

   // Print out the contents of each row into a table
   echo "<tr><td>";
   echo $row['title'];
   echo "</td><td>";
   echo $row['hits'];
   echo "</td></tr>";

$last_letter = $current_letter;//always change this so it's based on the previous
}

echo "</table>";
?>

Link to comment
Share on other sites

Hmm, that's really not the BEST application of it. Why? Because you don't have an even amount for each letter... If you wanted it like the website, do separate tables for each (which is hugely complicated, but the following code should work for that method.)

 

This took me about 5 minutes to think it through, but it SHOULD work. It uses tables nested in tables in order to achieve the desired two column effect o_o

 

<?php
// Make a MySQL Connection


// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM `jos_content` WHERE catid = '22' ORDER BY title ASC")
or die(mysql_error());

$table_start = "<table border='1'><tr><th>Drama Review</th><th>Hits</th></tr>";//so we don't have to repeat...

echo "<table border=0 cellspacing=0 cellpadding=0>";

// keeps getting the next row until there are no more to get
$last_letter = null;
$cell_count = 1;//2 cells per row
while($row = mysql_fetch_array( $result )) {

$current_letter = strtolower($row['title'][0]);
if($current_letter != $last_letter){//this means the first letter changed
echo "</table></td>";//always end

if(!$cell_count%2){//this means we need a new row
echo "</tr><tr>";
}

echo "<td>".$table_start."<tr><td colspan='2' style='background-color:black;color:white;'><center><b>".ucfirst($current_letter)."</b></center></td></tr>";
$cell_count++;
}

   // Print out the contents of each row into a table
   echo "<tr><td>";
   echo $row['title'];
   echo "</td><td>";
   echo $row['hits'];
   echo "</td></tr>";

$last_letter = $current_letter;//always change this so it's based on the previous

}

echo "</table></td></tr></table>";
?>

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.