do while with two conditions

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
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

do while with two conditions

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: do while with two conditions

Post by John Cartwright »

Instead use the LIMIT clause in your query

Code: Select all

SELECT * FROM table LIMIT 20
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

Re: do while with two conditions

Post 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.
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

Re: do while with two conditions

Post 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 '}'
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

Re: do while with two conditions

Post 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']);
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: do while with two conditions

Post by RobertGonzalez »

What are trying to accomplish with this? This seems easy enough. I just want to make sure I understand it properly.
Post Reply