0

I have this code:

foreach($html->find('ul.results') as $article) { $item['date'] = $article->find('span.result_date', 0)->plaintext; $item['title'] = $article->find('a.result_title', 0); $item['text'] = $article->find('span.result_text', 0)->plaintext; $item['read'] = $article->find('a.read_more', 0); $articles[] = $item; } foreach ($articles as &$item) { while ($i < 5) { echo $item['date']."&nbsp;&nbsp;&nbsp;"; echo $item['title'].'</br>'; echo $item['text']."&nbsp;"; echo $item['read'].'</br></br>'; $i++; } } 

And I am trying to echo the results. Right now the second foreach isn't doing anything. It is just displaying five of the same articles. The articles are in the format of: date, title, text, read more. I am trying to eco the first five $articles, but I can't find a proper way to do so that isn't print_r.

3
  • no the whole point of the $i is to just give me the first five $articles, but with the current code right now it is just repeating the same article. Not giving me the second value in it. The problem is I need each of the items to be formatted in that way while they are echoed. Commented Jul 22, 2012 at 4:15
  • I am parsing some code that is in several chunks. I am storing each of the titles, text, date, etc in one array of an $article. I need to display the first five articles. Commented Jul 22, 2012 at 4:18
  • Can you edit your question to include the output from print_r($articles)? Commented Jul 22, 2012 at 4:19

1 Answer 1

1

The whole loop inside the for each will just loop on the first element 5 times, move to the next element and loops 5 times, etc.

Try something like this:

$i = 0; foreach ($articles as $item){ echo items stuff... $i++; if ($i == 5) break; } 
Sign up to request clarification or add additional context in comments.

Comments