2

I got a resultset from php, from which I want to display an one time result. It is mix of html and php as follows.

foreach ($folder_info as $show) {
    if (isset($show['folder_name']) && ($check == 0)) {
        echo '<div class="alert alert-info">';
        echo 'Note! You could click on a Folder to view the images inside it.';
        echo '</div>';
        echo '<br/>';
    }
}

By executing the above loop, I am getting the following: Two times coming

and my result set array is like follows:-

Array (
    [folder_info] => 
        Array ( 
            [0] => Array ( [folder_id] => 1 )
            [1] => Array ( [folder_id] => 2 )
        )
)

Any suggestions on how to improve my coding will also be helpful! I want to show the line only one time.

7
  • So there is no problem you just want improvement ideas? That would be more suited to codereview.stackexchange.com Commented May 26, 2017 at 9:11
  • break out of foreach once you find the first result. So that would only display the message once instead for all the elements in $folder_info Commented May 26, 2017 at 9:12
  • @RamRaider, actually i am getting 2 lines. i want only one time to show it. I modified the question.. Commented May 26, 2017 at 9:14
  • @oh i using the <br/> inside the loop, should i use out of loop? Commented May 26, 2017 at 9:18
  • what exactly do you want? Do you want to print the condition only once? Commented May 26, 2017 at 9:19

2 Answers 2

2

You can try out this checking the zeroth element $key == 0

        foreach($folder_info as $key=>$show){ 
          if(isset($show['folder_name'])&&  ($key == 0))
          {                        
            echo '<div class="alert alert-info">';
            echo 'Note! You could click on a Folder to view the images inside it.';
            echo '</div>';
            echo '<br/>';
           }
            }?>
Sign up to request clarification or add additional context in comments.

3 Comments

Severity: Notice Message: Undefined variable: check
remove that condition from your if loop
Good Hope it helped you
2
$check = 0; // initialize $check here
foreach($folder_info as $show){

          if(isset($show['folder_name'])&& ($check==0))
          {                        
            echo '<div class="alert alert-info">';
            echo 'Note! You could click on a Folder to view the images inside it.';
            echo '</div>';
            echo '<br/>';
          $check++; //increment the $check here
           }
            }?>
  1. initialize the $check outside the foreach
  2. increment $check++ inside the foreach

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.