Jump to content

sort


sandy1028

Recommended Posts

This code sorts when clicked on the header of the tree....

Eg: When clicked on Name  names gets sorted.

When I click on Address address gets sorted but the Name comes back to original position

 

When I click on Address address should get sorted and Name should also be in sorted order...

I dont want to use array_multisort

 

 

Please help me with this

 

 

<style type="text/css">
#containerul, #containerul ul{
  text-align:left;
  margin:0; /* Removes browser default margins applied to the lists. */
  padding:0; /* Removes browser default padding applied to the lists. */
}

#containerul li{
  margin:0 0 0 30px; /* A left margin to indent the list items and give the menu a sense of structure. */
  padding:0; /* Removes browser default padding applied to the list items. */
  list-style-type:none; /* Removes the bullet point that usually goes next to each item in a list. */
}

#containerul .symbols{ /* Various styles to position the symbols next to the items in the menu. */
  float:left;
  width:12px;
  height:1em;
  background-position:0 50%;
  background-repeat:no-repeat;
}

#containerul .borders {
border-left-color: blue;
border-right-width: 0px; border-left-width: 3px;
padding-right: 0px; padding-left: 0px;
}



li ul{ border-left: 1px dotted #666;
}
li.connection { background: transparent url(node.png) no-repeat 0px 0px;
background-position:0% 100%
}
ul li ul { display: none; }
li.connection { li.connection {  background: transparent url() no-repeat 0 10px; }
li.connection ul { display: block; }

</style>


<script type="text/javascript">

var temp, temp2, cookieArray, cookieArray2, cookieCount;
function initiate(){
  cookieCount=0;
  if(document.cookie){
    cookieArray=document.cookie.split(";");
    cookieArray2=new Array();
    for(i in cookieArray){
      cookieArray2[cookieArray[i].split("=")[0].replace(/ /g,"")]=cookieArray[i].split("=")[1].replace(/ /g,"");
    }
  }
  cookieArray=(document.cookie.indexOf("state=")>=0)?cookieArray2["state"].split(","):new Array();
  temp=document.getElementById("containerul");
  for(var o=0;o<temp.getElementsByTagName("li").length;o++){
    if(temp.getElementsByTagName("li")[o].getElementsByTagName("ul").length>0){
      temp2 = document.createElement("span");
   temp2.className = "symbols";
//temp2.className = "borders";
      temp2.style.backgroundImage =  (cookieArray.length>0)?((cookieArray[cookieCount]=="true")?"url(minus.png)":"url(plus.png)"):"url(plus.png)";
      temp2.onclick=function(){
        showhide(this.parentNode);
        writeCookie();
      }
      temp.getElementsByTagName("li")[o].insertBefore(temp2,temp.getElementsByTagName("li")[o].firstChild)
      temp.getElementsByTagName("li")[o].getElementsByTagName("ul")[0].style.display = "none";
      if(cookieArray[cookieCount]=="true"){
        showhide(temp.getElementsByTagName("li")[o]);
      }
      cookieCount++;
    }
    else{
      temp2 = document.createElement("span");
temp2.className = "symbols";
//	temp2.className = "borders";
temp2.style.backgroundImage = "url(page.png)";
      temp.getElementsByTagName("li")[o].insertBefore(temp2,temp.getElementsByTagName("li")[o].firstChild);
    }
  }
}

function showhide(el){
  el.getElementsByTagName("ul")[0].style.display=(el.getElementsByTagName("ul")[0].style.display=="block")?"none":"block";
// el.getElementsByTagName("tree")[0].style.display=(el.getElementsByTagName("tree")[0].style.display=="block")?"none":"block";
   el.getElementsByTagName("span")[0].style.backgroundImage=(el.getElementsByTagName("ul")[0].style.display=="block")?"url(minus.png)":"url(plus.png)";
el.getElementsByTagName("span")[0].style.backgroundImage=(el.getElementsByTagName("ul")[0].style.display=="block")?"url(minus.png)":"url(plus.png)";


}

function writeCookie(){ // Runs through the menu and puts the "states" of each nested list into an array, the array is then joined together  and assigned to a cookie.
  cookieArray=new Array()
  for(var q=0;q<temp.getElementsByTagName("li").length;q++){
    if(temp.getElementsByTagName("li")[q].childNodes.length>0){
      if(temp.getElementsByTagName("li")[q].childNodes[0].nodeName=="SPAN" &&  temp.getElementsByTagName("li")[q].getElementsByTagName("ul").length>0){
        cookieArray[cookieArray.length]=(temp.getElementsByTagName("li")[q].getElementsByTagName("ul")[0].style.display=="block");
      }
    }
  }
  document.cookie="state="+cookieArray.join(",")+";expires="+new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString();
}
</script>

<?
$lines = file('./phonedb.txt');

                foreach ($lines as $line) {
                $text_line = explode(":" , $line);

$text_array[]=array('name'=>$text_line[0], 'address'=>$text_line[1],'age'=>$text_line[2], 'email'=>$text_line[3],'mobile'=>$text_line[4],'residence'=>$text_line[5], 'key'=>$key);


foreach ($text_array as $key => $row) {
$name[$key]  = $row['name'];
$address[$key] = $row['address'];
$age[$key] = $row['age'];
$email[$key] = $row['email'];
$mobile[$key] = $row['mobile'];
$residence[$key] = $row['residence'];
}
$id = $_GET['id'];

switch ($id) {
case "name":
array_multisort($name, SORT_ASC,SORT_STRING, $text_array);
        break;
case "address":
         array_multisort($address,SORT_ASC, $text_array);
        break;

case "age":
        array_multisort($age, SORT_ASC, SORT_NUMERIC, $text_array);
         break;

case "email":
         array_multisort($email, SORT_ASC, $text_array);
         break;

case "mobile":
         array_multisort($mobile, SORT_ASC,SORT_NUMERIC, $text_array);
         break;

case "residence":
        array_multisort($residence,SORT_ASC, SORT_NUMERIC, $text_array);
        break;
}
}

//for($i=0; $i<sizeof($text_array); $i++) {
//echo "<ul>";


//echo "<li>".$text_array[$i]['name']."</li>";
//echo "<li>".$text_array[$i]['address']."</li>";
//echo "<li>".$text_array[$i]['age']."</li>";
//echo "<li>".$text_array[$i]['email']."</li>";
//echo "<li>".$text_array[$i]['mobile']."</li>";
//echo "<li>".$text_array[$i]['residence']."</li>";
//echo  "</tr>";
//}


?>


<?
echo "<html>";
echo "<body bgcolor=\"#FFF0FF\" text=\"#000000\">";
$lines = file('phonedb.txt');
echo "<ul id=\"containerul\"><li class=\"connection\">View Tree";

//echo "<a href=\"tree.php?id=name\">Name</a>";

echo "<ul><li class=\"connection\"><a href=\"tree.php?id=name\">Name</a>";

echo "<ul>";

if($id != "name")
{
foreach ($lines as $line) {
                  $text_line = explode(":",$line);

echo "<li>--$text_line[0]</li>";
}
}
if($id == "name")
{
for($i=0;$i<sizeof($text_array); $i++) {

echo "<li>--".$text_array[$i]['name']."</li>";
}
}

echo "</ul></li>";



echo "<li class=\"connection\"><a href=\"tree.php?id=address\">Address</a>";
echo "<ul>";
if($id != "address")
{
foreach ($lines as $line) {
                    $text_line = explode(":",$line);

echo "<li>--$text_line[1]</li>";
}
}
if($id == "address")
{
for($i=0;$i<sizeof($text_array); $i++) {
echo "<li>--".$text_array[$i]['address']."</li>";
}
}

echo "</ul></li>";


//echo "<ul><a href=\"tree.php?id=age\">Age</a>";
echo "<li class=\"connection\"><a href=\"tree.php?id=age\">Age</a>";
echo "<ul>";
if($id != "age")
{
foreach ($lines as $line) {
                    $text_line = explode(":",$line);

echo "<li>--$text_line[2]</li>";
}
}
if($id == "age")
{
for($i=0;$i<sizeof($text_array); $i++) {
echo "<li>--".$text_array[$i]['age']."</li>";
}
}


echo "</ul></li>";


//echo "<ul><a href=\"tree.php?id=email\">Email</a>";
echo "<li class=\"connection\"><a href=\"tree.php?id=email\">Email</a>";
echo "<ul>";
if($id != "email")
{
foreach ($lines as $line) {
                    $text_line = explode(":",$line);

echo "<li>--$text_line[3]</li>";
}
}
if($id == "email")
{
for($i=0;$i<sizeof($text_array); $i++) {
echo "<li>--".$text_array[$i]['email']."</li>";
}
}


echo "</ul></li>";

//echo "<ul><a href=\"tree.php?id=mobile\">Mobile</a>";
echo "<li class=\"connection\"><a href=\"tree.php?id=mobile\">Mobile</a>";
echo "<ul>";
if($id != "mobile")
{
foreach ($lines as $line) {
                    $text_line = explode(":",$line);

echo "<li>--$text_line[4]</li>";
}
}
if($id == "mobile")
{
for($i=0;$i<sizeof($text_array); $i++) {
echo "<li>--".$text_array[$i]['mobile']."</li>";
}
}


echo "</ul></li>";

//echo "<ul><a href=\"tree.php?id=residence\">Residence</a>";
echo "<li class=\"connection\"><a href=\"tree.php?id=residence\">Residence</a>";
echo "<ul>";

if($id != "residence")
{
foreach ($lines as $line) {
                    $text_line = explode(":",$line);

echo "<li>--$text_line[5]</li>";
}
}
if($id == "residence")
{
for($i=0;$i<sizeof($text_array); $i++) {
echo "<li>--".$text_array[$i]['residence']."</li>";
}
}


echo "</ul></li>";

echo "</ul></li></ul>";           	
echo "</div>";
?>


<script type="text/javascript">
initiate();
</script>
<?php
echo "</body>";
echo "</html>";
?>

Link to comment
https://forums.phpfreaks.com/topic/62444-sort/
Share on other sites

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.