Jump to content

can I sort records in a table when the sort requested is not alphabetical?


JTapp

Recommended Posts

It's quite possible that this is impossible.. but here goes...

 

I have some personnel data that is being reported in a table and in that table I have a field that holds a person's "title". 

Let's say the returned "title" results are "President", "Vice President", "Assistant To The President" and "Assistant to the Vice President".  There are not more than four of them.  Does anybody have any idea if I could sort the returned results based on the above heirarchy - which is not alphabetical?

 

Here is my code:

 

echo "<center>\n";

echo "<H2>Roster</H2>\n";

echo "<table border='1'>

  <tr>

<th>Title</th>

<th>First Name</th>

<th>Last Name</th>

 

</tr>";

 

if (mysql_num_rows($query)) {

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

$variable1=$row["title"];

$variable2=$row["firstname"];

$variable3=$row["lastname"];

       

//table layout

       

print("<tr>");

echo "<tr align=\"center\" bgcolor=\"#EFEFEF\">\n";

echo "<td class=\"td_id\">$variable1</td>\n";

echo "<td class=\"td_id\">$variable2</td>\n";

echo "<td class=\"td_id\">$variable3</td>\n";

print("</tr>");

 

Thanks for looking at it!

Link to comment
Share on other sites

Try this, it's kind of redundant but should work in your situation:

But this will only output for rows those 4 fields:

<?php
if (mysql_num_rows($query)) {
   ranksort("President");
   ranksort("Vice President");
   ranksort("Assistant To The President");
   ranksort("Assistant to the Vice President");
}
function ranksort($level) {
       while ($row = mysql_fetch_array($query)) {
          if($row['title'] == $level) {
              $variable1=$row["title"];
              $variable2=$row["firstname"];
              $variable3=$row["lastname"];
       
              //table layout
       
              print("<tr>");
              echo "<tr align=\"center\" bgcolor=\"#EFEFEF\">\n";
              echo "<td class=\"td_id\">$variable1</td>\n";
              echo "<td class=\"td_id\">$variable2</td>\n";
              echo "<td class=\"td_id\">$variable3</td>\n";
              print("</tr>");
         }
       }
       return true;
}?>

Link to comment
Share on other sites

DarkerAngel -

I tried to plug it into my existing code but I got an error message.. here is the entire code with the query.. maybe it makes a difference to your suggestion?

 

<?php

//query details table begins

$query = mysql_query("SELECT tblLodges.strLodgeName, tblLodges.intLodgeNumber, tblLodges.strDistrictName, tblLodges.strLodgeMailingCity, tblLodges.strLodgeMailingPostCode, tblLodges.strLodgeCounty, tblOfficers.lngLodgeID, tblOfficers.Title, tblOfficers.strFirstName, tblOfficers.strLastName, tblOfficers.BusinessPhone, tblOfficers.PersEmail FROM tblLodges LEFT JOIN tblOfficers ON tblLodges.lngLodgeID = tblOfficers.lngLodgeID WHERE tblLodges.intLodgeNumber=$id LIMIT 0, 50")or die(mysql_error());

 

echo "<center>\n";

echo "<H2>Roster</H2>\n";

echo "<table border='1'>

  <tr>

<th>Office Number</th>

<th>Title</th>

<th>First</th>

<th>Last</th>

<th>Email</th>

<th>Phone</th>

 

</tr>";

 

if (mysql_num_rows($query)) {

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

        $variable1=$row["intLodgeNumber"];

        $variable2=$row["Title"];

        $variable3=$row["strFirstName"];

        $variable4=$row["strLastName"];

        $variable5=$row["PersEmail"];

        $variable6=$row["BusinessPhone"];

       

        //table layout for results

       

        print("<tr>");

        echo "<tr align=\"center\" bgcolor=\"#EFEFEF\">\n";

        echo "<td class=\"td_id\">$variable1</td>\n";

        echo "<td class=\"td_id\">$variable2</td>\n";

        echo "<td class=\"td_id\">$variable3</td>\n";

        echo "<td class=\"td_id\">$variable4</td>\n";

        echo "<td class=\"td_id\">$variable5</td>\n";

        echo "<td class=\"td_id\">$variable6</td>\n";

        print("</tr>");

    }

}

?>

 

Link to comment
Share on other sites

See if this works any better (sorry about above)

 

<?php
//query details table begins
$query = mysql_query("SELECT tblLodges.strLodgeName, tblLodges.intLodgeNumber, tblLodges.strDistrictName, tblLodges.strLodgeMailingCity, tblLodges.strLodgeMailingPostCode, tblLodges.strLodgeCounty, tblOfficers.lngLodgeID, tblOfficers.Title, tblOfficers.strFirstName, tblOfficers.strLastName, tblOfficers.BusinessPhone, tblOfficers.PersEmail FROM tblLodges LEFT JOIN tblOfficers ON tblLodges.lngLodgeID = tblOfficers.lngLodgeID WHERE tblLodges.intLodgeNumber=$id LIMIT 0, 50")or die(mysql_error());

echo "<center>\n";
echo "<H2>Roster</H2>\n";
echo "<table border='1'>
   <tr>
<th>Office Number</th>
<th>Title</th>
<th>First</th>
<th>Last</th>
<th>Email</th>
<th>Phone</th>

</tr>";

if (mysql_num_rows($query)) {
   ranksort("President", $query);
   ranksort("Vice President", $query);
   ranksort("Assistant To The President", $query);
   ranksort("Assistant to the Vice President", $query);
}
function ranksort($level, $query) {
    while ($row = mysql_fetch_array($query)) {
        $variable1=$row["intLodgeNumber"];
        $variable2=$row["Title"];
        $variable3=$row["strFirstName"];
        $variable4=$row["strLastName"];
        $variable5=$row["PersEmail"];
        $variable6=$row["BusinessPhone"];
       
        //table layout for results
        if($variable2 == $level) {
     		print("<tr>");
     		echo "<tr align=\"center\" bgcolor=\"#EFEFEF\">\n";
       		echo "<td class=\"td_id\">$variable1</td>\n";
        	echo "<td class=\"td_id\">$variable2</td>\n";
        	echo "<td class=\"td_id\">$variable3</td>\n";
        	echo "<td class=\"td_id\">$variable4</td>\n";
        	echo "<td class=\"td_id\">$variable5</td>\n";
        	echo "<td class=\"td_id\">$variable6</td>\n";
        	print("</tr>");
	}
    }	
}
?>

EDITED CODE

Link to comment
Share on other sites

OK - scratch that last post.

I now have a column called "titleID"

For every "president" there is a value of 1

For every "VP" there is a value of 2... etc.

 

Now I have to try to figure out how to make the 'title' field return sorted data, based on the hidden 'titleID' field.

 

Does that make sense?

Link to comment
Share on other sites

<?php
//query details table begins
$query = mysql_query("SELECT tblLodges.strLodgeName, tblLodges.intLodgeNumber, tblLodges.strDistrictName, tblLodges.strLodgeMailingCity, tblLodges.strLodgeMailingPostCode, tblLodges.strLodgeCounty, tblOfficers.lngLodgeID, tblOfficers.Title, tblOfficers.strFirstName, tblOfficers.strLastName, tblOfficers.BusinessPhone, tblOfficers.PersEmail FROM tblLodges LEFT JOIN tblOfficers ON tblLodges.lngLodgeID = tblOfficers.lngLodgeID WHERE tblLodges.intLodgeNumber=$id LIMIT 0, 50")or die(mysql_error());

echo "<center>\n";
echo "<H2>Roster</H2>\n";
echo "<table border='1'>
   <tr>
<th>Office Number</th>
<th>Title</th>
<th>First</th>
<th>Last</th>
<th>Email</th>
<th>Phone</th>

</tr>";

if (mysql_num_rows($query)) {
while ($row = mysql_fetch_array($query)) {
	switch($row["Title"]) {
		case "President":
			$p .= formattable($row);
			break;
		case "Vice President":
			$vp .= formattable($row);
			break;
   			case"Assistant To The President":
			$attp .= formattable($row);
			break;
   			case"Assistant to the Vice President":
			$attvp .= formattable($row);
			break;
		default:
			$other .= formattable($row);
	}
}
echo($p.$vp.$attp.$attvp.$other);
}
function formattable($row) {
        $variable1=$row["intLodgeNumber"];
        $variable2=$row["Title"];
        $variable3=$row["strFirstName"];
        $variable4=$row["strLastName"];
        $variable5=$row["PersEmail"];
        $variable6=$row["BusinessPhone"];
       
        //table layout for results
     		$html = "<tr>"
     		$html .= "<tr align=\"center\" bgcolor=\"#EFEFEF\">\n";
       		$html .= "<td class=\"td_id\">$variable1</td>\n";
        	$html .= "<td class=\"td_id\">$variable2</td>\n";
        	$html .= "<td class=\"td_id\">$variable3</td>\n";
        	$html .= "<td class=\"td_id\">$variable4</td>\n";
        	$html .= "<td class=\"td_id\">$variable5</td>\n";
        	$html .= "<td class=\"td_id\">$variable6</td>\n";
        	$html .= "</tr>"

return $html;
}
?> 

 

I dunno if that don't work I might give up :P

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.