0

I'd like to add a class to the first result of a query inside a foreach and then echo the rest of the information.

I am using the following code to retrieve and show only the first result

foreach ($highlightedArticles as $data)
{   
    $article_title      = $data['article_title'];
    $article_image      = $data['article_image'];

    echo '
    <section class="headlines">
        <h2 class="heading orange">
            <a href="#">news</a>
        </h2>

        <ul>
            <li class="first">
                <a href="">
                    <figure>
                        <img src="url/'.$article_image.'"/>
                        <figcaption>'.$article_title.'</figcaption>
                    </figure>
                </a>
            </li>
        </ul>
    </section>
    ';
}

The issue that I cannot resolve now is how to add the rest of the results(4 resulsts total with the class"first") since everything is inside an echo and I need to add the results that are in a < li > .

This is what I would like to show..

<ul>
    <li class="first">
        <a href="">
            <figure>
                <img src="url/'.$article_image.'"/>
                <figcaption>'.$article_title.'</figcaption>
            </figure>
        </a>
    </li>
    <li>another result</li>
    <li>another result</li>
    <li>another result</li>
</ul>
iBrazilian2
  • 2,204
  • 6
  • 23
  • 45
  • possible duplicate of [How to determine the first and last iteration in a foreach loop?](http://stackoverflow.com/questions/1070244/how-to-determine-the-first-and-last-iteration-in-a-foreach-loop) – nkmol Nov 22 '13 at 08:27
  • Thank you but I posted a code in the question that does the exact same thing as the answer in that question. – iBrazilian2 Nov 22 '13 at 08:33

4 Answers4

0

is that what you are looking for?

$out= '
    <section class="headlines">
        <h2 class="heading orange">
            <a href="#">news</a>
        </h2>

        <ul>';
foreach ($highlightedArticles as $data)

{   

    $article_title      = $data['article_title'];
    $article_image      = $data['article_image'];

$out.= '    
            <li class="first">
                <a href="">
                    <figure>
                        <img src="url/'.$article_image.'"/>
                        <figcaption>'.$article_title.'</figcaption>
                    </figure>
                </a>
            </li>';
}
$out.='  </ul>
    </section>';
echo $out;
cytofu
  • 873
  • 9
  • 17
  • I've thought about doing it this way but it's a hacky way of doing it from what I've thought. – iBrazilian2 Nov 22 '13 at 08:34
  • whats "hacky" about it? If suits your eyes better you could first collect the li's content in some $myLisContent, and put that var afterwards between the
    in the echo. But essentially its the same.. or if for whatever reason the $out bothers you, just echo directly (replace each $out with echo, omitting the `echo $out;` obviously)
    – cytofu Nov 22 '13 at 08:49
0

please modify your the code that you tried in following ways:

$firstLoop = true;
foreach( $network_value as $key => $value ){

   if( $firstLoop ){
       //do things on only the first loop
       $firstLoop = false;
       continue;
   }

   //do other things

}

edit

$out= '
<section class="headlines">
    <h2 class="heading orange">
        <a href="#">news</a>
    </h2>

    <ul>';
$firstloop=true;
foreach ($highlightedArticles as $data)

{   

   $article_title      = $data['article_title'];
   $article_image      = $data['article_image'];
   if($firstloop){
     $out.= '    
     <li class="first">
         <a href="">
              <figure>
                    <img src="url/'.$article_image.'"/>
                    <figcaption>'.$article_title.'</figcaption>
                </figure>
            </a>
        </li>';
    $firstloop = false;
    continue; 
 }

 $out.= '    
      <li>
            <a href="">
                <figure>
                    <img src="url/'.$article_image.'"/>
                    <figcaption>'.$article_title.'</figcaption>
                </figure>
            </a>
      </li>';
}
$out.='</ul>
</section>';
echo $out;
user3011768
  • 191
  • 2
  • 3
  • 11
0

I would do it like this:

  1. Get the first item of the $highlightedArticles like this $highlightedArticles[0] and echo the image and the title.
  2. Then call array_slice($highlightedArticles, 1); which will return the $highlightedArticles except first element and then iterate and echo the returned items.
0
<section class="headlines">
    <h2 class="heading orange">
        <a href="#">news</a>
    </h2>

    <ul>
    <?php foreach ($highlightedArticles as $key => $data)
    {   
        $article_title      = $data['article_title'];
        $article_image      = $data['article_image'];

        reset($highlightedArticles);

        printf('<li %s>
                    <a href="">
                        <figure>
                            <img src="url/%s"/>
                            <figcaption>%s</figcaption>
                        </figure>
                    </a>
                </li>
            ',
            $key === key($highlightedArticles) ? 'class="first"' : '',
            $article_image,
            $article_title
        );
    } ?>
    </ul>
</section>
Steely Wing
  • 16,239
  • 8
  • 58
  • 54