Jump to content

Recommended Posts

My while loop to do the process to every member_id won't work.Its only printing one member_id thats done.Also my INSERT statement wont insert even that 1 member_id. Can someone please point my mistake.

<?php
include 'config.php';
    
function tree_gather($node)   
  {
     
$sql = "SELECT lchild,rchild FROM tree WHERE parent = '$node'";
    $execsql = mysql_query($sql);
    $array = mysql_fetch_array($execsql);
    
    if(!empty($array['lchild']) || !empty($array['rchild']))
    {
      if(!empty($array['lchild']))
      $child[] = $array['lchild'];
      if(!empty($array['rchild']))
      $child[] = $array['rchild'];
      if(!empty($array['lchild']))
      $lchild = tree_gather($array['lchild']);
      if(!empty($array['rchild']))
      $rchild = tree_gather($array['rchild']);
      if(!empty($lchild) && !empty($rchild)) {
      $merge_both = array_merge($lchild, $rchild);
      $child = array_merge($child, $merge_both);    
      }
      elseif(!empty($lchild) && empty($rchild))
      $child = array_merge($child, $lchild);
      elseif(!empty($rchild) && empty($lchild))
      $child = array_merge($child, $rchild); 
     
    } 
     return $child;
  } 
  
$sql = mysql_query("SELECT member_id FROM member");
while($array = mysql_fetch_array($sql)) {
    
$memid = $array['member_id'];

$pair = tree_gather($memid)  ;

$remove = mysql_query("SELECT lchild,rchild FROM tree WHERE parent ='".$memid."'");
$remove_array = mysql_fetch_array($remove);
if(!empty($remove_array['lchild']) || !empty($remove_array['rchild']))
{
   $count = count($pair);
    for($i = 0 ; $i < $count ; $i++) {
        if($pair[$i] != $remove_array['lchild'] && $pair[$i] !=$remove_array['rchild']) {
            $trimmed[] = $pair[$i];
        }
        else {
            $two_one[] = $pair[$i];
        }
    }
}

$add = mysql_query("SELECT lchild,rchild FROM tree WHERE parent='".$remove_array['lchild']."'");
$add_array = mysql_fetch_array($add);
if(!empty($add_array['lchild']) || !empty($add_array['rchild'])) {
    $count = count($trimmed);
    for($i=0;$i < $count ; $i++) {
        if(!empty($add_array['lchild'])) {
            if($add_array['lchild'] == $trimmed[$i]) {
                $two_one[] = $add_array['lchild'];
                unset($trimmed[$i]);
                $done = 1;
                break;
                
            }
        }    
        if(!empty($add_array['rchild']) && $done !=1) {
            if($add_array['rchild'] == $trimmed[$i]) {
                $two_one[] = $trimmed[i];
                unset($trimmed[$i]);
                $done = 1;
                break;
                
            }
        }    
               
    }
}

if($done != 1) {
    
    $add = mysql_query("SELECT lchild,rchild FROM tree WHERE parent='".$remove_array['rchild']."'");
    $add_array = mysql_fetch_array($add);
    if(!empty($add_array['lchild']) || !empty($add_array['rchild'])) {
    $count = count($trimmed);
    for($i=0;$i < $count ; $i++) {
        if(!empty($add_array['lchild'])) {
            if($add_array['lchild'] == $trimmed[$i]) {
                $two_one[] = $add_array['lchild'];
                unset($trimmed[$i]);
                $done = 1;
                break;
                
            }
        }    
        if(!empty($add_array['rchild']) && $done !=1) {
            if($add_array['rchild'] == $trimmed[$i]) {
                $two_one[] = $trimmed[i];
                unset($trimmed[$i]);
                $done = 1;
                break;
                
            }
        }    
               
    }
}
    
}  
  
  $select_child = mysql_query("SELECT rchild,lchild FROM tree WHERE parent='".$memid."'");
  $select_array = mysql_fetch_array($select_child);
  
  if(!empty($select_array['rchild']))
  $rchild = tree_gather($select_array['rchild']);
  $rchild[] = $select_array['rchild'];
  
  if(!empty($select_array['lchild']))
  $lchild = tree_gather($select_array['lchild']);
  $lchild[] = $select_array['lchild']; 
  
  if(!empty($rchild)) {
      $right_count = count($rchild);
      
      }

  if(!empty($lchild)) {
      $left_count = count($lchild);
      
      
  }

    
if($right_count < $left_count) {

    $pair_num = $right_count;
    
    }    

elseif($right_count == $left_count)
{
     $pair_num = $right_count - 1;
}
    else {

    $pair_num = $left_count;

    }
    if(count($two_one) == 3) {
        $dues_adjust = 100;
    
    if($pair_num >= 1) {
        $dues_adjust = $dues + 100; 
        $calcom = 200 * $pair_num;
        $tds = 20.60 * $pair_num;
        $service = 9.40 * $pair_num;
        $paid = $calcom - $dues_adjust;
        $insert = mysql_query("INSERT INTO `payout` (member_id, binary, tds, service, total, dues, amount_paid, dues_adjust, pairs) VALUES ('".$memid."', '".$calcom."', '".$tds."', '".$service."', '".$calcom."', '0', '".$paid."', '".$dues_adjust."', '".$pair_num."')");
        unset($pair);
        
        echo "<br />".$memid." Done";
    }
    }
    
    
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/237263-my-while-loop-and-sql-insert-wont-work/
Share on other sites

There is an awful lot going on in that code. I started reading it line-by-line to understand what was going on but, to be honest, it is very confusing. Not only are there no comments, but many of the variable names have no meaning (e.g. $two_one).

 

Have have a very strong feeling that you are doing much more work and making this more complicated than it should be. For example, I see where you do a query to get an id, then do a subsequent query using that ID to pull records from another table. You should instead be using a JOIN query.

 

I think it might be more advantageous to explain what it is you are trying to accomplish along with a description of your tables structures.

I got my while loop right :) I just had to unset the value of the array $two_one in the end before starting new loop. However my insert won't work.

 

Table -

CREATE TABLE IF NOT EXISTS `payout` (
  `member_id` int( NOT NULL,
  `binary` int(10) NOT NULL,
  `tds` varchar(6) NOT NULL,
  `service` varchar(6) NOT NULL,
  `total` int(10) NOT NULL,
  `dues` int(10) NOT NULL,
  `amount_paid` int(10) NOT NULL,
  `dues_adjust` int(10) NOT NULL,
  `pairs` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

Are you still interested to know what i am trying to achieve ?

Like mjdamato said, your code is very confusing and hard to follow.  It's going to be very difficult for someone to debug this code, especially since we can't replicate your testing environment.  You're going to need to try and isolate your problem.  Echo out the member id to see which one it stops on & also in the while/for/if blocks to see where it's breaking down.

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.