0

I have SQL query inside a for loop and I want to echo something of success of these query only once. Here is part of my code

<?php
   for($i=0;$i<sizeof($assigned_project_id);$i++){

 $sql2="SELECT * FROM assign_task INNER JOIN branch ON assign_task.branch_ID=branch.branch_ID WHERE project_ID=".$assigned_project_id[$i]." AND USER_ID=1";
              
                $query2=mysqli_query($con,$sql2);
  if(mysqli_num_rows($query2)>0){
                echo" You have Assigned following Reports";
            }
                while($row2=mysqli_fetch_assoc($query2)){

                    echo $row2['branch_ID']."&nbsp;".$row2['branch_name']."<br/>";
                }

            }
            ?>

I want to display the "You have Assigned following Reports" only once. Please help me.

3
  • 2
    so meve the message outside the loop. and running the querry in a loop is just a bad idea, get all the data you want and loop that Commented Jun 20, 2015 at 6:35
  • can you please give a example Commented Jun 20, 2015 at 6:41
  • if I place that outside the loop won't it echo if there is not single thing to show Commented Jun 20, 2015 at 6:45

1 Answer 1

1

Use it as below

<?php
       $test =0 ;
       for($i=0;$i<sizeof($assigned_project_id);$i++){

            $sql2="SELECT * FROM assign_task INNER JOIN branch ON assign_task.branch_ID=branch.branch_ID WHERE project_ID=".$assigned_project_id[$i]." AND USER_ID=1";

            $query2=mysqli_query($con,$sql2);
            if(mysqli_num_rows($query2)>0){
               if($test==0){
                   echo" You have Assigned following Reports";
                  $test++;
               }
            }
            while($row2=mysqli_fetch_assoc($query2)){

                echo $row2['branch_ID']."&nbsp;".$row2['branch_name']."<br/>";
            }

        }
        ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.