Thursday, July 7, 2016

Function to generate Random Key


HERE is a function to generate a random key in PHP

$Length = number of digit of which you need to generate random key


function GenerateRandomKey($Length){
  $random_chars="";
  $characters = array(
"A","B","C","D","E","F","G","H","J","K","L","M",
"N","P","Q","R","S","T","U","V","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9");

//make an "empty container" or array for our keys
$keys = array();

//first count of $keys is empty so "1", remaining count is 1-6 = total 7 times
while(count($keys) < $Length) {
    //"0" because we use this to FIND ARRAY KEYS which has a 0 value
    //"-1" because were only concerned of number of keys which is 32 not 33
    //count($characters) = 33
    $x = mt_rand(0, count($characters)-1);
    if(!in_array($x, $keys)) {
       $keys[] = $x;
    }
}

foreach($keys as $key){
   $random_chars .= $characters[$key];
}
return $random_chars;



 }


Just pass the length parameter and will get random key generated

Example

echo $keygen = GenerateRandomKey(13);

OUTPUT

Every time you call the function and it would return a random key

KFZBHR18TN3X4
Y38JGTDVHCP9L
9DMNF6ACE37XQ
MUGSFC4RT5BKH

You can make the key more complex by adding character and digits  to $character array

Thanks

Cheers


Wednesday, May 7, 2014

function to show number of left node and right node in a binary tree

Dear friends

i m a developer and making a MLM site i had  a problem to show number of left and right nodes of a binary tree.

if more then 1000 data are there in the database  then we can not bring all the count for each node.
so we had a solution for that we called an ajax script which  would return the count for each when event of mouse-hover occurs.



script to find the count is here
 find_counts.php
<?php
         //echo "start    TOT=".$total;
        $memid=$app->getRequestVar("memid");
        $total_other=0;
        $total_right_ajax= get_tree($app->getRequestVar("memid"),'right');
        //$app->assign("total_right_ajax", $total_right_ajax);           
       
        global $total_other;
        $total_other=0;
        //echo "after    TOT=".$total;
        $total_left_ajax= get_tree($app->getRequestVar("memid"),'left');   
        //$app->assign("total_left_ajax", $total_left_ajax);   
       
       
       
        function get_tree($memid,$leg){
       
            $sql="select user_id from user
            left join user_binary  on user_binary .user_id=user.id
            where   user_binary.parent_id='".$memid."' and binary_position='".$leg."'";
            // echo "<br>";
            $res=mysql_query($sql);
           
            global $total_other;
            $total_other=$total_other+mysql_num_rows($res);
            $row=mysql_fetch_array($res);
           
            if($row['user_id']!=''){
                get_tree ($row['user_id'],'left');
                get_tree ($row['user_id'],'right');
            }
           
            return $total_other;
            }

        echo "LEFT: ".$total_left_ajax."&nbsp;       RIGHT: ".$total_right_ajax;
?>    

Now just write a ajax function to call find_counts.php  and display it

if any query feel free to ask ..