Page 1 of 1

do while with two conditions

Posted: Mon Feb 25, 2008 1:23 pm
by micknc
Hey guys,
I have the following code that is working great:

Code: Select all

 
do {
 
            $queryivhead = "SELECT * FROM ivheadtemp order by CUST_ID ASC";
            $result=mysql_query($queryivhead);
            $rowivhead = mysql_fetch_assoc($result);
 
            $custid = $rowivhead ['CUST_ID'];
            $ivpass = $rowivhead ['INVOICE_NO'];
            $idpass = $rowivhead ['SO_NO'];
      include 'iv.php';
    mysql_query("INSERT INTO track (TRACK_SO_NO, TRACK_STATUS, TRACK_NOTES) VALUES ('".$idpass."','25','".$statusname."')");    
    mysql_query("DELETE FROM ivheadtemp WHERE INVOICE_NO=$ivpass");
            $queryivhead1 = "SELECT * FROM ivheadtemp order by CUST_ID ASC";
            $result1=mysql_query($queryivhead1);
            $rowivhead1 = mysql_fetch_assoc($result1);
 
} while ($custid == $rowivhead1['CUST_ID']);
 
This code is really used by another page that effectively bundles my invoices based on the customer id. Some customers have one and others have 50 to 60. The other page uses this one to email the html output of iv.php. Okay so the new twist that I now have to add is I need to regulate how many invoices it bundles at a time.

It is working great if I have a customer with 30 or less but the guys with 50-60 are killing me. Lets say I need to limit it to 20. I know I could do this:

Code: Select all

 
     $num = 1;
     while ( $num <=20 )
     {
            $num = $num + 1;
        }
 
I really need the conditions to be while ($custid == $rowivhead1['CUST_ID']) or ( $num <=20 ) whichever comes first. I tried ($custid == $rowivhead1['CUST_ID'] or $num <=20) with a test of one invoice but it runs the script a second time (with errors that stop the script).

How can I define two conditions. Remember that if either one is true the script needs to terminate.

Re: do while with two conditions

Posted: Mon Feb 25, 2008 1:27 pm
by John Cartwright
Instead use the LIMIT clause in your query

Code: Select all

SELECT * FROM table LIMIT 20

Re: do while with two conditions

Posted: Mon Feb 25, 2008 1:32 pm
by micknc
That query really just grabs the current customer number to compare the next customer number in second query.

If they match the loop continues but if not the loop terminates and the email is sent. That effectively batches the invoices based on customer id.

Re: do while with two conditions

Posted: Mon Feb 25, 2008 1:43 pm
by micknc
I thought that an if statement may do the trick but I can't seem to get the syntax right.

It seems to me that something like this would do the trick:

Code: Select all

 
$num = 1;
do {
            $queryivhead = "SELECT * FROM ivheadtemp order by CUST_ID ASC";
            $result=mysql_query($queryivhead);
            $rowivhead = mysql_fetch_assoc($result);
 
            $custid = $rowivhead ['CUST_ID'];
            $ivpass = $rowivhead ['INVOICE_NO'];
            $idpass = $rowivhead ['SO_NO'];
      include 'iv.php';
    mysql_query("INSERT INTO track (TRACK_SO_NO, TRACK_STATUS, TRACK_NOTES) VALUES ('".$idpass."','25','".$statusname."')");    
    mysql_query("DELETE FROM ivheadtemp WHERE INVOICE_NO=$ivpass");
            $queryivhead1 = "SELECT * FROM ivheadtemp order by CUST_ID ASC";
            $result1=mysql_query($queryivhead1);
            $rowivhead1 = mysql_fetch_assoc($result1);
            $num = $num + 1;
 if ($num <=20) 
 } while ($custid == $rowivhead1['CUST_ID']);
 else ;
 
But that errors on the } in the second to last line:
syntax error, unexpected '}'

Re: do while with two conditions

Posted: Mon Feb 25, 2008 2:21 pm
by micknc
For all of you keeping score at home I just needed to rethink my if statement.

It may not be perfect (and I would love to hear thoughts as I am learning) but it works:

Code: Select all

 
$num = 1;
do { 
            $queryivhead = "SELECT * FROM ivheadtemp order by CUST_ID ASC";
            $result=mysql_query($queryivhead);
            $rowivhead = mysql_fetch_assoc($result);
            
            if ($num <=20){$custid = $rowivhead ['CUST_ID'];}
            else {$custid = "no match";}
 
            $ivpass = $rowivhead ['INVOICE_NO'];
            $idpass = $rowivhead ['SO_NO'];
      include 'iv.php';
    mysql_query("INSERT INTO track (TRACK_SO_NO, TRACK_STATUS, TRACK_NOTES) VALUES ('".$idpass."','25','".$statusname."')");    
    mysql_query("DELETE FROM ivheadtemp WHERE INVOICE_NO=$ivpass");
            $queryivhead1 = "SELECT * FROM ivheadtemp order by CUST_ID ASC";
            $result1=mysql_query($queryivhead1);
            $rowivhead1 = mysql_fetch_assoc($result1);
            $num = $num + 1; 
     } while ($custid == $rowivhead1['CUST_ID']);
 

Re: do while with two conditions

Posted: Mon Feb 25, 2008 2:42 pm
by RobertGonzalez
What are trying to accomplish with this? This seems easy enough. I just want to make sure I understand it properly.