Results 1 to 4 of 4
  1. #1

    PHP Foreach Loop Issue...

    Hello WHT,

    I am having an issue where some users are not having an invoice created when this script runs.

    The gist of the system is that the user can charge items to their account all month long and then on the 1st of the next month, have all the previous months pending charges (balancepending) become balance due (balancedue).

    We add $2 to the balance due for our fee and then create an invoice.

    Now, I am selecting all users and going through them in a foreach loop for an array. Randomly however some are not being included in the invoice creation process, and still have their balancepending and no balancedue ( aka it's like the whole script skipped a few random accounts ).

    Can anyone see an error or lapse in the code? Thank you for the assist.

    Code:
    $userid_array = array( );
    
    $query= "SELECT * FROM users";
    $result = mysql_query($query) or die("Error in user query.");
    
    while($row = mysql_fetch_assoc($result)) {
    extract($row);
    
    echo $firstname . " " . $lastname;
    echo "<br/>";
    echo $email;
    echo "<br/>";
    echo "$" . $balancepending;
    echo "<br/> <hr>";
    
    array_push($userid_array, $ID);
    
    }
    
    print_r($userid_array);
    
    
    foreach ($userid_array as $value){
    //SELECTING user BALANCE INFO
    $balanceq = "SELECT * FROM users WHERE ID='$value'";
    $balancer = mysql_query($balanceq) or die("Error in balance query");
    
    while($balancerow = mysql_fetch_assoc($balancer)) {
    extract($balancerow);
    
    $balancedue = $balancepending;
    $balancepending = "0.00";
    
    //UPDATING BALANCE DUE
    $updateq = "UPDATE users SET balancedue=$balancedue, balancepending=$balancepending WHERE ID='$value'";
    $updater = mysql_query($updateq) or die("Error in balance update.");
    
    if($balancedue == '0.00'){
    //zero balance
    }else{
    $balancedue = $balancedue + 2.00;
    }
    
    //CREATE INVOICE
    $periodending = date("m/t/Y", strtotime("last month"));
    $status = "INVOICED";
    $invoiceq = "INSERT INTO invoices (pid, date, total, status) VALUES('$value', '$periodending', '$balancedue', 'INVOICED')";
    $invoicer = mysql_query($invoiceq) or die("Error in invoice creation." . mysql_error());
    
    //END FOREACH
    }

  2. #2
    Join Date
    May 2009
    Location
    Tennessee
    Posts
    303
    I've cleaned up the code for you:

    PHP Code:
    <?php
    $query 
    "SELECT * FROM users";
    $result mysql_query($query) or die("Error in user query.");
    while(
    $row mysql_fetch_array($result)) {
        
    $value $row["ID"];
        
    $balancedue $row["balancepending"];
        
    $balancepending "0.00";

        
    //if balance due != 0, create an invoice
        
    if($balancedue) {    
            
    //UPDATING BALANCE DUE
            
    $updateq "UPDATE users SET balancedue=$balancedue, balancepending=$balancepending WHERE ID='$value'";
            
    $updater mysql_query($updateq) or die("Error in balance update.");
        
            
    $balancedue $balancedue 2.00;

            
    //CREATE INVOICE
            
    $periodending date("m/t/Y"strtotime("last month"));
            
    $status "INVOICED";
            
            
    $invoiceq "INSERT INTO invoices (pid, date, total, status) VALUES('$value', '$periodending', '$balancedue', 'INVOICED')";
            
    $invoicer mysql_query($invoiceq) or die("Error in invoice creation." mysql_error());
        }
    }
    ?>
    AudioProbe.net - Playing the best music from the 70s, 80s, 90s, 00s, and more...
    Commercial free, high quality 192kb/s, and firewall friendly port 80!
    Look for us in the iTunes Radio directory under Adult Contemporary

  3. #3
    Thank you kindly, do you think this will minimize the chance of accounts being skipped? Definitely looks a lot cleaner

  4. #4
    Join Date
    May 2009
    Location
    Tennessee
    Posts
    303
    Accounts won't be skipped (unless the script times out...adding set_time_limit(0) to the top of the script will fix that). Test it first before adding to the live site, of course.
    AudioProbe.net - Playing the best music from the 70s, 80s, 90s, 00s, and more...
    Commercial free, high quality 192kb/s, and firewall friendly port 80!
    Look for us in the iTunes Radio directory under Adult Contemporary

Similar Threads

  1. need help with php/mysql foreach loop
    By mjfroggy in forum Programming Discussion
    Replies: 11
    Last Post: 07-22-2009, 09:39 AM
  2. Using Foreach/Loop on a query
    By acctman in forum Programming Discussion
    Replies: 5
    Last Post: 09-02-2008, 11:05 AM
  3. Problem with foreach loop
    By P-nut in forum Programming Discussion
    Replies: 4
    Last Post: 08-09-2007, 09:43 PM
  4. using a foreach() loop
    By mjfroggy in forum Programming Discussion
    Replies: 8
    Last Post: 06-23-2007, 01:24 PM
  5. while vs foreach in php
    By stuffradio in forum Programming Discussion
    Replies: 4
    Last Post: 06-06-2006, 01:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •