www.webdeveloper.com
Results 1 to 9 of 9

Thread: Loop Problem

  1. #1
    Join Date
    Sep 2010
    Posts
    6

    Loop Problem

    Hi,

    I'm struggling with this piece of code although I know there is a simple solution, the loop only executes the code once where it should multiple times.

    PHP Code:
     $query "SELECT * FROM practitioners WHERE approved = 1";

            
    $result pg_query($query);
            if (!
    $result) {
                echo 
    "Problem with query " $query "<br/>";
                echo 
    pg_last_error();
                
            }

            while(
    $myrow pg_fetch_assoc($result)) {
        
                
    $idx $myrow['practitioner_id'];
                
    $form_results $_POST["$idx"];
                
                
                if(
    $form_results='yes'
                    {

                       
                                    
                                            
    $query "UPDATE practitioners SET approved='0' where practitioner_id='$idx'";
                                    
                                            
    $result pg_exec($db$query);
                                    
                                            if (!
    $result) {
                                                
    printf ("ERROR");
                                                
    $errormessage pg_last_error();
                                                echo 
    $errormessage;
                                                exit;
                                            }
                                    
                                    
                                             
                                            
                        }
                                                        
                    
                
            } 
    $idx are the values sent through the form

  2. #2
    Join Date
    Jun 2007
    Posts
    3
    You redefine $result. Did the same thing myself not an hour ago

  3. #3
    Join Date
    Feb 2010
    Posts
    39
    Also, the condition

    if($form_results='yes')

    Will ALWAYS evaluate to true since you used the assignment operator (=) and not the equality operator (==). Make sure to change it to

    if($form_results=='yes')

  4. #4
    Join Date
    Sep 2010
    Posts
    6
    Thanks! 100&#37;

  5. #5
    Join Date
    Sep 2010
    Posts
    6

    Red face

    OMW, my brain wont work anymore.... Now I don't know why this part is only executing once while the other query executes multiple times like it shoud:
    PHP Code:
            $query "SELECT * FROM practitioners WHERE approved = 1";

            
    $result pg_query($query);
            if (!
    $result) {
                echo 
    "Problem with query " $query "<br/>";
                echo 
    pg_last_error();
                exit();
            }
    ///////
            
    while($myrow pg_fetch_assoc($result)) {
    ///////              
                
    $id $myrow['practitioner_id'];
         

       
            
    $approvals pg_escape_string($_POST['approve' $id]);
            
    $removals pg_escape_string($_POST['remove' $id]);
            
            if (
    $approvals == approve) {
                                                                
                                            
                                                                    
    $query2 "UPDATE practitioners SET approved='0' WHERE practitioner_id='$id'";
                                                            
                                                                    
    $result2 pg_exec($db$query2);
                                                            
                                                               if (!
    $result2) {
                                                                        
    printf ("ERROR");
                                                                        
    $errormessage pg_last_error();
                                                                        echo 
    $errormessage;
                                                                        
                                                                    } 
                                                            
                                                                    
    printf ("These values were updated in the database - %s ",$id);
                                                                    
                                                                    
                                                                    
    ///////////////Only loops once then returns SQL error///////////////    
                                                                    
    $query3 "SELECT * FROM practitioners WHERE practitioner_id='$id'";
                                                            
                                                                    
    $result3 pg_query($query3);
                                                                    if (!
    $result3) {
                                                                        echo 
    "Problem with query " $query3 "<br/>";
                                                                        echo 
    pg_last_error();
                                                                        
                                                                    }
                                                            
                                                                    while(
    $myrow3 pg_fetch_assoc($result3)) {
                                                                       
    $email $myrow3['email'];
                                                                    } 
                                                                    
    printf ("Emails - %s ",$email);
                                            
    ////////////////////////////////
                                        
                                        
    }
                                        
            if (
    $removals == remove) {
                
            
    $query3 "DELETE FROM practitioners WHERE practitioner_id='$id'";
                                                            
                                                                    
    $result3 pg_exec($db$query3);
                                                            
                                                               if (!
    $result3) {
                                                                        
    printf ("ERROR");
                                                                        
    $errormessage pg_last_error();
                                                                        echo 
    $errormessage;
                                                                        exit;
                                                                    } 
                                                            
                                                                    
    printf ("These values were removed in the database - %s ",$removals);                                                        
                                        }
            
    pg_close();
    /////////        
             


  6. #6
    Join Date
    Feb 2010
    Posts
    39
    On the statements

    $approvals == approve

    and

    $removals == remove,

    what exactly are those things on the right side of the equality operators? Do you mean for them to be strings?

  7. #7
    Join Date
    Sep 2010
    Posts
    6
    If I understood you correctly, it's more like functions, when the form passes through a "remove", the statement checks it and then executes the code to remove the users from the database.

    The same thing for the approval part, the thing is the approval database update sql works fine & loops fine but not where the email is retrieved???

  8. #8
    Join Date
    Sep 2010
    Posts
    6
    My brain started working again.....

    the "pg_query(" should be "pg_exec("

    I hate PostgreSQL.....

  9. #9
    Join Date
    Feb 2010
    Posts
    39
    Glad you got it!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles