PHP Infinite Class classification method and code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags array class code function name php php tutorial programming

PHP Tutorials Unlimited class classification


class trees{


Private $_keyid = ' Id ';


Private $_keyname = ' Name ';


Private $_keyfid = ' Fid ';


 


function __construct ($keyId = ', $keyName = ', $keyFid = ')


 {


if ($keyId = = "") {$this->_keyid = $this->_keyid;} else{$this->_keyid = $keyId;}


if ($keyName = = "") {$this->_keyname = $this->_keyname;} else{$this->_keyname = $keyName;}


if ($keyFid = = "") {$this->_keyfid = $this->_keyfid;} else{$this->_keyfid = $keyFid;}


 }


 


Public Function Treelistall ($fid, $step =0,& $fromArray,& $resultArray)


 {


$step + +;


foreach ($fromArray as $k => $v) {


if ($v [$this->_keyfid] = = $fid) {


$newArray [] = $v;


   }


  }


if (isset ($newArray)) {


foreach ($newArray as $k => $v) {


$this->treelistall ($v [$this->_keyid], $step, $fromArray, $resultArray);


$v [' step '] = $step;


$resultArray [] = $v;


   }


  }


 }


 


Public Function Gettreelistall ($fid =0, $step =0,& $fromArray,& $resultArray)


 {


$step + +;


foreach ($fromArray as $k => $v) {


if ($v [$this->_keyfid] = = $fid) {


$newArray [] = $v;


   }


  }


if (isset ($newArray)) {


foreach ($newArray as $k => $v) {


$this->gettreelistall ($v [$this->_keyid], $step, $fromArray, $resultArray);


$v [' step '] = $step;


$resultArray [] = $v;


   }


  } 


 }


 


Public Function gettreelist ($id = 0,& $fromArray)


 {


$resultArray = Array ();


$this->gettreelistall ($id, 0, $fromArray, $resultArray);


$resultArray = Array_reverse ($resultArray);


return $resultArray;


 }


 


Public Function Gettreearray ($id = 0,& $fromArray)


 {


$result _one_array = $this->gettreelist ($id, $fromArray);


foreach ($result _one_array as $k => $v) {


$result _two_array[] = Array ($v [$this->_keyid]);


  }


if (isset ($result _two_array)) {


for ($i =0 $i <count ($result _two_array), $i + +) {


for ($j =0 $j <count ($result _two_array[$i]); $j + +) {


$result [] = $result _two_array[$i] [$j];


   }


  }


}else{


$result = Array ();


  }


return $result;


}

Public Function Treelistalltop ($fid, $step =0,& $fromArray,& $resultArray)


 {


$step + +;


foreach ($fromArray as $k => $v) {


if ($v [$this->_keyid] = = $fid) {


$newArray [] = $v;


}


  }


if (isset ($newArray)) {


foreach ($newArray as $k => $v) {


$this->treelistalltop ($v [$this->_keyfid], $step, $fromArray, $resultArray);


$v [' step '] = $step;


$resultArray [] = $v;


   }


  }


 }


 


Public Function Gettreelistalltop ($fid =0, $step =0,& $fromArray,& $resultArray)


 {


$step + +;


foreach ($fromArray as $k => $v) {


if ($v [$this->_keyid] = = $fid) {


$newArray [] = $v;


   }


  }


if (isset ($newArray)) {


foreach ($newArray as $k => $v) {


$this->gettreelistalltop ($v [$this->_keyfid], $step, $fromArray, $resultArray);


$v [' step '] = $step;


$resultArray [] = $v;


   }


  } 


 }


 


Public Function gettreelisttop ($id = 0,& $fromArray)


 {


$resultArray = Array ();


$this->gettreelistalltop ($id, 0, $fromArray, $resultArray);


$resultArray = Array_reverse ($resultArray);


return $resultArray;


 }


 


Public Function gettreearraytop ($id = 0,& $fromArray)


 {


$result _one_array = $this->gettreelisttop ($id, $fromArray);


foreach ($result _one_array as $k => $v) {


$result _two_array[] = Array ($v [$this->_keyfid]);


  }


if (isset ($result _two_array)) {


for ($i =0 $i <count ($result _two_array), $i + +) {


for ($j =0 $j <count ($result _two_array[$i]); $j + +) {


$result [] = $result _two_array[$i] [$j];


   }


  }


}else{


$result = Array ();


  }


return $result;


 }


 


Public Function makeoptionstring ($sourcArray, $firstHint = "top-level category", $selectId =array ('-1 '), $type =0)


 {


if ($type ==0) {


if ($firstHint!= "") {


$str = ' <option value= ' 0 ' > '. $firstHint. ' </option> ';


}else{


$str = ';


   }


foreach ($sourcArray as $value) {


$level = "";


for ($i =1, $i < $value [' step ']; $i + +) {


$level = $level. " ----|";


    }


$selectStr = "";


if (In_array ($value [$this->_keyid], $selectId)) {


$selectStr = "selected";


}else{


    


    }


$str. = ' <option value= '. $value [$this->_keyid]. ' " '. $selectStr. ' >| '. $level. $value [$this->_keyname]. " </option> ";


$level = "";


   }


}else{


$flagStep =-1;


$str = ' <option value= 0 ' > ' $firstHint. ' </option> ';


foreach ($sourcArray as $value) {


$level = "";


for ($i =1, $i < $value [' step ']; $i + +) {


$level = $level. " ----|";


    }


$selectStr = "";


if ($type = = $value [$this->_keyid]) {


$flagStep = $value [' Step '];


}else{


if ($flagStep!=-1 && $value [' Step ']> $flagStep) {


    


}else{


if ($flagStep!=-1 && $value [' Step ']<= $flagStep) {


$flagStep =-1;


}


if ($value [$this->_keyid] = = $selectId) {


$selectStr = "selected";


      }


$str. = ' <option value= '. $value [$this->_keyid]. ' " '. $selectStr. ' >| '. $level. $value [$this->_keyname]. " </option> ";


     }


    }


   


$level = "";


   }


  }


return $str;


 }


}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.