2

I am facing problem with logic in multi level marketing.

Refer following image for more explanation.

MLM

THE ABOVE LOGIC WILL WORK TILL LEVEL 10.

Take following example into consideration.

  1. Suppose I am registered as a new user and do not have any sponsor code. Then I will be the root user. The sponsor id for me is 123.

  2. Then new user is registered by entering my sponsor id that is 123 then he will be the 1st child of user 123. maximum number of child for one user is 4. and the new user sponsor id is A123.

  3. Once the child count for user 123 is over 4. and new user is again entering the sponsor id is 123 then he will be registered to the first child of sponsor id A123.

  4. Again new registration with sponsor id 123 will be the registered with reference of 2nd child.

The logic that I've written is going again to the first child of 123 only the iteration is not working as I want.

I am not asking for whole code just give me an idea where I'm wrong.

TABLE STRUCTURE :

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(200) NOT NULL,
  `full_name` varchar(200) NOT NULL,
  `email` varchar(200) NOT NULL,
  `mobile` varchar(200) NOT NULL,
  `sponsor_id` varchar(200) NOT NULL,
  `unique_code` varchar(200) NOT NULL,
  `code_entered` varchar(200) NOT NULL,
  `sponsor_count` int(200) NOT NULL,
  `direct` varchar(200) NOT NULL,
  `direct_count` int(11) NOT NULL,
  `serial_no` varchar(200) NOT NULL,
  `package_amount` varchar(200) NOT NULL,
  `password` varchar(200) NOT NULL,
  `birthday` varchar(200) NOT NULL,
  `pancard_no` varchar(200) NOT NULL,
  `city` varchar(200) NOT NULL,
  `address` text NOT NULL,
  `last_login` varchar(200) NOT NULL,
  `last_login_Ip` varchar(200) NOT NULL,
  `date` varchar(200) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `username`, `full_name`, `email`, `mobile`, `sponsor_id`, `unique_code`, `code_entered`, `sponsor_count`, `direct`, `direct_count`, `serial_no`, `package_amount`, `password`, `birthday`, `pancard_no`, `city`, `address`, `last_login`, `last_login_Ip`, `date`) VALUES
(1, 'SA', 'MK', 'MK@gmail.com', '9702242036', '66666666', '64384703', '', 4, '', 53, '13212', '400.00', '123', '11/11/1994', '15155155155', 'Mumbai', 'Ghatkopar', 'April 2 2016', '58.146.119.92', 'March 17 2016'),
(38, 'jkjdkjskdk', 't', 'jkdjskjkjk', 'salsaj', '64384703', '10404315', '64384703', 4, '64384703', 0, '1312', '400.00', 'jdjsjkdjskdjs', '', '', '', '', '', '', 'March 31 2016'),
(39, 'dsnn', 's', 'ndksnd', 'saksnkan', '64384703', '69669736', '64384703', 4, '64384703', 0, 'samksa', '400.00', 'njnsjnj', '', '', '', '', '', '', 'March 31 2016'),
(40, 'jdsjjaj', 's', 'jkjsjaksj', 'sakskajk', '64384703', '88455053', '64384703', 4, '64384703', 0, '425545454545', '400.00', 'jkdsjkd', '', '', '', '', '', '', 'March 31 2016'),
(41, 'ndsjnd', 'Sumit', '45sansjnaj', '7577545', '64384703', '15397510', '64384703', 4, '64384703', 0, '23232323', '400.00', 'njnsjndjn', '', '', '', '', '', '', 'March 31 2016'),
(75, 'WFwf', 'adWADq', 'fwef', 'wqfWFw', '15397510', '36171300', '64384703', 0, '64384703', 0, 'dWADwdAD', '400.00', 'FwfWF', '', '', '', '', '', '', 'March 31 2016'),
(74, 'CAscSAC', 'ASCC39502728', 'CASC', 'CASCAS', '88455053', '69828189', '64384703', 0, '64384703', 0, 'ACAsc', '400.00', 'ASCASCAS', '', '', '', '', '', '', 'March 31 2016'),
(73, 'caSCASCA', 'ASCASCA', 'CSAc', 'sCSAC', '69669736', '17947094', '64384703', 0, '64384703', 0, 'ASDAd', '400.00', 'Csacasc', '', '', '', '', '', '', 'March 31 2016'),
(60, 'fDwsdw', 'SCSFSF', 'wdwD', 'wedwedw', '10404315', '47797754', '64384703', 1, '64384703', 0, 'WDWDWD', '400.00', 'wfdWFF', '', '', '', '', '', '', 'March 31 2016'),
(61, 'Sacsc', 'SDSdCD', 'csc', 'ScScs', '69669736', '06965959', '64384703', 0, '64384703', 0, 'SFSDASD', '400.00', 'scscsc', '', '', '', '', '', '', 'March 31 2016'),
(62, 'AsCsc', 'asCAsca', 'AscascAs', 'sacasAC', '88455053', '80778770', '64384703', 0, '64384703', 0, 'AsCascS', '400.00', 'AScasXas', '', '', '', '', '', '', 'March 31 2016'),
(63, 'CaScasc', 'ASCascScaS', 'CascacaS', 'ASCAS', '15397510', '61309698', '64384703', 0, '64384703', 0, 'sCFasfC', '400.00', 'acascas', '', '', '', '', '', '', 'March 31 2016'),
(64, 'scascACS', 'asCASc', 'CASCSCa', 'casCS', '10404315', '39502728', '64384703', 1, '64384703', 0, 'asdaSDSD', '400.00', 'cASCAS', '', '', '', '', '', '', 'March 31 2016'),
(65, 'scasc', 'dxadascd', 'ascasca', 'scascasc', '69669736', '15508065', '64384703', 0, '64384703', 0, 'ADADada', '400.00', 'casc', '', '', '', '', '', '', 'March 31 2016'),
(66, 'csdvsdvs', 'asdascsc', 'scscs', 'scsc', '88455053', '96194063', '64384703', 0, '64384703', 0, 'sFf', '400.00', 'dvdsvsd', '', '', '', '', '', '', 'March 31 2016'),
(67, 'ASASSC', 'ascsac', 'aScS', 'ascadsa', '15397510', '79804063', '64384703', 0, '64384703', 0, 'ssSCFa', '400.00', 'ACASDA', '', '', '', '', '', '', 'March 31 2016'),
(68, 'acasC', 'acascasc', 'asca', 'ascasc', '10404315', '15145419', '64384703', 1, '64384703', 0, 'AWDacac', '400.00', 'ascsca', '', '', '', '', '', '', 'March 31 2016'),
(69, 'aA', 'aAFawf', 'afaf', 'asdaSF', '69669736', '48166652', '64384703', 0, '64384703', 0, 'aSDAWDAD', '400.00', 'adsd', '', '', '', '', '', '', 'March 31 2016'),
(70, 'FAFasf', 'sfSFS', 'FSFSFS', 'DSFSDFS', '88455053', '74124082', '64384703', 0, '64384703', 0, 'SFsf', '400.00', 'Faf', '', '', '', '', '', '', 'March 31 2016'),
(71, 'ScScs', 'SCC', 'scSC', 'scscsC', '15397510', '65224240', '64384703', 0, '64384703', 0, 'sScScs', '400.00', 'csc', '', '', '', '', '', '', 'March 31 2016'),
(72, 'Acsac', 'SACAcas', 'Asc', 'cascASC', '10404315', '13703218', '64384703', 1, '64384703', 0, 'ASCASc', '400.00', 'AscAsc', '', '', '', '', '', '', 'March 31 2016'),
(77, 'scdv', 'sdvdsvs', 'sDvsdv', 'sD', '39502728', '87098579', '64384703', 0, '64384703', 0, 'sdadscsdv', '400.00', 'svdsvsd', '', '', '', '', '', '', 'March 31 2016'),
(78, 'asfsadf', 'avsdvads', 'fsdfdsaf', 'ssadv', '15145419', '82251755', '64384703', 0, '64384703', 0, 'asfasf', '400.00', 'safsadf', '', '', '', '', '', '', 'March 31 2016'),
(79, 'sFf', 'ff', 'SFs', 'FsfSF', '13703218', '09815392', '64384703', 0, '64384703', 0, 'fFEF', '400.00', 'sfsf', '', '', '', '', '', '', 'March 31 2016'),
(82, 'SFDSADFD', 'ASDFSADF', 'AFAFASFD', 'FSFASF', '15145419', '62181227', '64384703', 0, '64384703', 0, 'SFSDSF', '400.00', 'SFSF', '', '', '', '', '', '', 'April 1 2016'),
(83, 'KPNKOPN', 'PKPKOPOKP', 'KOPKO', 'OPKOP', '13703218', '37114007', '64384703', 0, '64384703', 0, 'NKONOP;N', '400.00', 'NKPNKOP', '', '', '', '', '', '', 'April 1 2016'),
(86, 'KOPKNOPN', 'PKNPKNPKN', 'PKPKO', 'PKOPKOP', '15145419', '95262805', '64384703', 0, '64384703', 0, 'NKPNKOPN', '400.00', 'KONPNKO', '', '', '', '', '', '', 'April 1 2016'),
(87, 'P;NP', 'ONPPNP', 'NKOPKOP', 'NPOKP', '13703218', '68382470', '64384703', 0, '64384703', 0, 'JINJNP', '400.00', 'PNJPNPN', '', '', '', '', '', '', 'April 1 2016'),
(90, 'HFHFG', 'TFHFH', 'HFGHHG', 'FHFCHFCH', '15145419', '24886528', '64384703', 0, '64384703', 0, 'HTFHTGHC', '400.00', 'HGFHXFH', '', '', '', '', '', '', 'April 1 2016'),
(91, 'XFHXFTH', 'FTHFH', 'HFTHFTXHFX', 'HXFHXFT', '13703218', '58477780', '64384703', 0, '64384703', 0, 'TFHTFHTFH', '400.00', 'HGXHH', '', '', '', '', '', '', 'April 1 2016');

CODE:

<?php
INCLUDE('db.php');


        function min_count($sponsor_id){

                $select11 = mysql_query("SELECT MIN(sponsor_count) as count FROM users WHERE sponsor_id='$sponsor_id'");    
                $rows11 = mysql_fetch_assoc($select11);             
                $count_n1 =  $rows11['count'];

                return $count_n1;


        }

        function sponsor_count_name($sponsor_id){

                $select111 = mysql_query("SELECT sponsor_count FROM users WHERE unique_code='$sponsor_id'");    
                $rows111 = mysql_fetch_assoc($select111);               
                $count_s =  $rows111['sponsor_count'];

                return $count_s;


        }

        function id_check($sponsor_id){

                $select111 = mysql_query("SELECT sponsor_count FROM users WHERE unique_code='$sponsor_id'");    
                $rows111 = mysql_fetch_assoc($select111);               
                $count_s =  $rows111['sponsor_count'];

                return $count_s;


        }



        function sponsor_count($sponsor_id){


                echo "<font color='red'>Sponsor_id : $sponsor_id</font><br/>";
                $min_count =  min_count($sponsor_id);
                //min_count  = 4


                // its full 4

                $sponsor_count_u = sponsor_count_name($sponsor_id);
                // sponsor_count_u = 4  

                echo "<font color='red'>Min Count : $min_count</font><br/><hr/>";



                if($sponsor_count_u < 4){


                echo "<font color='red'>$sponsor_id</font><br/>";

                }else{

            function check_count($min_count,$sponsor_id){   

                if($min_count < 4 ){

                    $select11 = mysql_query("SELECT unique_code FROM users WHERE sponsor_count='$min_count' AND sponsor_id='$sponsor_id'"); 
                    $rows11 = mysql_fetch_assoc($select11);         
                    $sponsor_id = $rows11['unique_code'];

                        return   $sponsor_id; 


                }else{

                    $select1 = mysql_query("SELECT unique_code FROM users WHERE sponsor_id='$sponsor_id'"); 

                        while($rows1 = mysql_fetch_assoc($select1)){
                        $sponsor_id = $rows1['unique_code'];

                        echo "<font color='green'> <b>$sponsor_id</font> </b>&nbsp;";

                        $sponsor_count_u = sponsor_count_name($sponsor_id); 
                        echo "<font color='red'>Min Count: $sponsor_count_u</font><br/>";





                        $select11 = mysql_query("SELECT unique_code FROM users WHERE sponsor_id='$sponsor_id'");    

                        while($rows11 = mysql_fetch_array($select11)){
                        $sponsor_id1 = $rows11['unique_code'];

                        echo "<font color='blue'> <b>$sponsor_id1</font> </b>&nbsp;";

                        $min_count1 = sponsor_count_name($sponsor_id1); 

                        echo "$min_count1<br/>";


                        //$min_array = array($min_count);


                        //  turn check_count($min_count,$sponsor_id1);



                        }


                        }

                    }
            }

            }



            return check_count($min_count,$sponsor_id);



        }


            echo  sponsor_count(64384703);



?>
Deepak S. Gavkar
  • 447
  • 8
  • 21
  • Storing trees in databases is hard, there are 4 possible solutions for this, see: http://stackoverflow.com/questions/4048151/what-are-the-options-for-storing-hierarchical-data-in-a-relational-database – Johan Apr 03 '16 at 12:57

0 Answers0