0

i am fetching data from xml, where it has 100 elements, my code:

foreach($sections->children() as $question){
    $xml_data .= 'Section Name: '.$question->section_name.'<br/>';
    $xml_data .= 'Question No.: '.$question->qno.'<br/>';
    if($question->answer == null || $question->answer == ''){
        $question->answer = 'Not attempted';
        $xml_data .= 'Answer: '.$question->answer.'<br/>';
    } else {
        $xml_data .= 'Answer: '.$question->answer.'<br/>';
    }
    $xml_data .= 'Time: '.$question->time.'<br/>';
    if($question->seen == '1'){
        $xml_data .= 'Seen<br/>';
    } else {
        $xml_data .= 'Not Seen<br/>';
    }
    if($question->flag == '1'){
        $xml_data .= 'Flaged<br/>';
    } else {
        $xml_data .= 'Not Flaged<br/>';
    }
}

From above i am getting 100 question but i wabt to display only 25 from above, how should i do this?

AD7six
  • 63,116
  • 12
  • 91
  • 123

2 Answers2

1
$count = 1;
foreach($sections->children() as $question){

    ...
    ...

    if($count++ == 25){
        break;
    }
}
Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59
0

Do it like this:

$t = 1; $rate=array(1 , 2 ,3 ,4 ,5 ); print_r($rate); foreach($rate as $rt){ echo $rt; if($rt==3){ exit;} $t++; }
Vikas Gautam
  • 997
  • 7
  • 23