Good day!

<? if (!empty($list6)) { ?> <ul> <? foreach ($list6 as $key => $post6) { if($post5->podkat.$post5->id == $post6->podkat) { ?> <li><a href="#"><?=$post6->name;?></a></li> <? } } ?> </ul> <? } ?> 

Here it is necessary to make so that ul (opening and closing) are output, if the condition

 $post5->podkat.$post5->id == $post6->podkat 

worked, but ul was deduced once, and not in a cycle.

  • one
    @Iranda, yeah. @Cyclomedchik, and yet do not post links to left-wing third-party resources without extreme necessity: besides the fact that someone is dripping a free rating, the main thing is that the resource can be offline, then it becomes unclear what the question was about. - knes
  • Why not make it this way? <? php $ sql = mysql_query ("SELECT * FROM pages "); while ($ result = mysql_fetch_array ($ sql)) {echo "Everything you wish will be displayed here in a loop"; }?> - webkostya 5:14

4 answers 4

The simplest option:

 $lastVar = array_pop($your_array); foreach($your_array as $key=>$value){ //do_something_with_vars } //do_something_with_last_var 

    Another option:

     $len = sizeof($list6); foreach($list6 as $key => $post6){ --$len; if(!$len){ // последний, стало быть } } 

      You can first find out the number of elements in the array through the default function, and then at the end put the condition if they =, then the end.

      The author probably wanted to say how to find the last element? and not how to know if the cycle has ended ....

        What means:

        how to determine the end of the foreach loop inside it

        This cycle is "auto-complete", so to speak. And it is intended mainly for the passage through the data collections, so he himself finishes when he reaches the last element of the collection.

        • four
          What people. =) Well, yes. But precisely because he completes himself and sometimes he has to pervert a bit. Speaking of passage: the number to go through the collections is array_walk - knes
        • @knes, first letter is Russian "A" =) - Salivan