如何运行我的两个foreach循环而不仅仅是一个

时间:2015-11-20 15:07:57

标签: php mysql

我正在运行这两个foreach循环来从我的数据库中获取数据,但是当我需要这两个时,我只能让其中一个工作。

if ($stmt->rowcount() > 0) {
        foreach($stmt as $row) {
            $ResultsCounter++;
            $htmlResult .=  "<div style='margin-right:5px; width:280px' class='col-md-4 well text-center'>" .
                                "<h4>". $row['BusinessType'] . "</h4>" 
                            ."</div>";                      
            } 
        } else{
            $htmlResult .= "<h1 class='text-center'>Sorry no search results found please search again</h1>";
    }

    if ($stmt->rowcount() > 0) {
        foreach($stmt as $row) {
            $ResultsCounter++;
            $htmlResult1.=  "<li><a href='#'>" . $row['BusinessType'] . "</a></li>";            
        } 
    }

思想?

1 个答案:

答案 0 :(得分:4)

只需一次性完成:

$htmlResult = '';
$htmlResult1 = '';

if ($stmt->rowcount() > 0) {
    foreach($stmt as $row) {
        $ResultsCounter++;
        $htmlResult .=  "<div style='margin-right:5px; width:280px' class='col-md-4 well text-center'>" .
                            "<h4>". $row['BusinessType'] . "</h4>" 
                        ."</div>";   
        $htmlResult1.=  "<li><a href='#'>" . $row['BusinessType'] . "</a></li>";            

    } 
} else{
        $htmlResult .= "<h1 class='text-center'>Sorry no search results found please search again</h1>";
}