an hidden infinite loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

an hidden infinite loop

Post by matthiasone »

There appears to be an infinite loop problem with this code, but I can not find it. Here is the code

Code: Select all

function order_complete()
{
  session_start();
  chck_nomaster_list(); 
  if( isset($_SESSION['client']['cid']))
  {
    $query = "INSERT INTO orders VALUES ('".$_SESSION['order']['rid']."', '".
	         $_SESSION['order']['odate']."', '".$_SESSION['client']['cid']."', '".
			 $_SESSION['order']['otype']."', '".$_SESSION['order']['rep']."', '".
		     ($_SESSION['acount'] + $_SESSION['vcount'])."', '', '".$_SESSION['order']['comments']."', '".
			 $_SESSION['shipping']."', '".$_SESSION['handling']."', 'N', 'N', 'N', 'N','N')";
      echo 'oq='.$query.'<br>';
    $oresult = query_db($query, '2');
    if (mysql_affected_rows() == 1)
      $oflag = 1;
    else
      $oflag = 0;	
    echo 'of='.$oflag.'oif='.$oiflag.'pf='.$pflag.'tf='.$tflag.'<br>';
    echo 'dc='.$_SESSION['cnt'].'<br>';
	for($i = 0; $i <= $_SESSION['cnt']; $i++)
    {
      if (isset($_SESSION['oitem'][$i]['media']))
	  { 
        $cnt ++;
	    $oiid = makeoiid($cnt);
        $query = "INSERT INTO orderdetails VALUES ('".$oiid."', '".$_SESSION['order']['rid']."', '".
                 $_SESSION['oitem'][$i]['sid']."', '".$_SESSION['oitem'][$i]['qty']."', '".
				 $_SESSION['oitem'][$i]['media']."', '".$_SESSION['oitem'][$i]['uprice'].
				 "', 'N', 'N', '0000-00-00', 'N', 'N', 'N')";
      echo 'oiq='.$query.'<br>';
	    $oiresult = query_db($query, '2');
        if (mysql_affected_rows() >= 1)
          $oiflag = 1;
        else
          $oiflag = 0;	
	  }
    }
    $cnt = 0;	  
    echo 'of='.$oflag.'oif='.$oiflag.'pf='.$pflag.'tf='.$tflag.'<br>';
    for($p = 0; $p <= count($_SESSION['package'])-1; $p++)
    {
      $cnt ++;
	  $pid = make_pid($cnt);
      $query = "INSERT INTO orderpackages VALUES ('".$pid."', '".$_SESSION['order']['rid']."', '".
               ($_SESSION['acount'] + $_SESSION['vcount'])."', '".$_SESSION['package'][$p]['pkg']."')";
      echo 'pq='.$query.'<br>';
	  $tresult = query_db($query, '2');
	  if (mysql_affected_rows() >= 1)
        $pflag = 1;
       else
        $pflag = 0;
    }
    $cnt = 0;	  
    echo 'of='.$oflag.'oif='.$oiflag.'pf='.$pflag.'tf='.$tflag.'<br>';
    echo 'tc='.$_SESSION['tcnt'].'<br>';
    for($t = 0; $t <= $_SESSION['tcnt']; $t++)
    {
	  if (isset($_SESSION['tran'][$t]['ttype']))
	  { 
        $cnt ++;
	    $tid = maketid($cnt);
        $query = "INSERT INTO ordertrans VALUES ('".$tid."', '".$_SESSION['order']['rid']."', '".
                 $_SESSION['tran'][$t]['ptype']."', '".$_SESSION['tran'][$t]['ttype']."', '".
			     $_SESSION['tran'][$t]['cknum']."', '".$_SESSION['tran'][$t]['amt']."', 'N')";
        echo 'tq='.$query.'<br>';
	    $tresult = query_db($query, '2');
	    if (mysql_affected_rows() >= 1)
          $tflag = 1;
        else
          $tflag = 0;
       }
    }
    echo 'of='.$oflag.'oif='.$oiflag.'pf='.$pflag.'tf='.$tflag.'<br>';
	if($oflag == 1 and $oiflag == 1 and $tflag == 1 and $pflag == 1)
    {  
  	  $rid = $_SESSION['order']['rid'];
      $user = $_SESSION['user'];
      $repnum = $_SESSION['repnum'];
      $tech = $_SESSION['tech'];
	  session_unset();
	  $_SESSION['user'] = $user;
      $_SESSION['repnum'] = $repnum;
      $_SESSION['tech'] = $tech;
    }
  }		
    echo 'final of='.$oflag.'oif='.$oiflag.'pf='.$pflag.'tf='.$tflag.'<br>';
	order_complete_form($rid);
}
Any help would be appreciated[/quote]
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

for($i = 0; $i <= $_SESSION['cnt']; $i++)

what is $_SESSION['cnt'] and $_SESSION['tcnt']
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

They actually are showing the correct information.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

maybe its happening in one of the many other fuctions you are running inside that function
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Post by thomas777neo »

My best advice is to comment out all the loops except one, then execute the code. Do this until you have found the loop hole.

Also try and have a breaking mechanism in a loop. So if for example, you know the loop will return the number of records in a table. If the counter + 1 > number of rows, break the loop and echo something.

Just take it one step at a time, I feel that some of the values that you store in the session could be the problem.

Enjoy debugging, your loops look structurally correct.
Last edited by thomas777neo on Wed May 25, 2005 5:55 pm, edited 1 time in total.
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Post by thomas777neo »

I agree with shiznatix, check your other functions out first
Post Reply