简体   繁体   English

PHP while循环分为两部分

[英]PHP while loop split into two

I'm running a while loop which is taking rows from mySQL and echo'ing them out. 我正在运行一个while循环,该循环从mySQL中获取行并将它们回显。 Pretty standard. 很标准。 However, there needs to be different HTML markup for the first item, next two items, then it continues as normal into the while loop. 但是,第一个项目,接下来的两个项目需要有不同的HTML标记,然后照常继续进入while循环。

Currently, my while loop looking something like this: 目前,我的while循环看起来像这样:

while( ( $row = mysql_fetch_object( $result ) ) !== false )
    {   

    // Places ad based of predefined variable frequency
    if ($ad == $adFrequency){

     echo '<div class="one-advertisement-article clearfix">';
     echo '<div class="grid_9 suffix_2"><img src="http://placehold.it/263x75/000000/ffffff"></div>';
     echo '<div class="grid_1"><a class="navigate-right-icon ss-icon" href="#">navigateright</a></div>';
     echo '</div>';

     $ad = 0;
    }

    echo '<div class="one-news-article clearfix">';
    if( $row->imagelibid )
            {
                $imageid = intval( $row->imagelibid );
                $image_result = mysql_query( "SELECT * FROM imagelib WHERE id = {$imageid}", $db );

                $image_row = mysql_fetch_object( $image_result );

                $image_url = "http://www.#.com/slir/w280/imagelib/" . $image_row->id . "_" . $image_row->filename;

                echo '<div class="grid_4"><a href="#"><img src="'.$image_url.'"></a></div>';        
            }
    else {
                echo '<div class="grid_4"><div class="news-placeholder"><span class="ss-icon">ellipsischat</span></div></div>';
        }

    echo '<div class="grid_7">';
    echo '<h2><a href="item-news.php?id='.$row->id.'">'.$row->title.'</a></h2>';

    $published_date = date('D, d M Y', strtotime($row->datein));
    echo '<p class="published-date">'.$published_date.'</p>';

    echo '</div>';
    echo '<div class="grid_1">';
    echo '<div class="news-item-vertical-sep">&nbsp;</div>';
    echo '<p></p>';
    echo '</div>';
    echo '</div>';

    $ad++;

    }

This works fine, but I need to take the first three news items and use this: 这工作正常,但我需要使用前三个新闻项并使用它:

echo ' <div class="one-news-featured-article clearfix">';
    echo '  <div class="grid_12">';

    if( $row->imagelibid )
            {
                $imageid = intval( $row->imagelibid );
                $image_result = mysql_query( "SELECT * FROM imagelib WHERE id = {$imageid}", $db );

                $image_row = mysql_fetch_object( $image_result );

                $image_url = "http://www.#.com/slir/w280/imagelib/" . $image_row->id . "_" . $image_row->filename;

                echo '<div class="large-featured-image-container"><img src="'.$image_url.'">/div>'; 
            }   

    echo '      <div>';
    echo '        <h2><a href="item-news.php?id='.$row->id.'">'.$row->title.'</a></h2>';
    echo '      </div>';
    echo '    </div>';
    echo '  </div>';
    echo '  <div class="grid_6">';

Any help? 有什么帮助吗? Essentially, it needs to apply the second code to the first three items in the query, then follow through with the code included in the current while loop - like an offset I guess. 从本质上讲,它需要将第二个代码应用于查询中的前三个项目,然后继续执行当前while循环中包含的代码-就像我想的偏移量一样。

Thanks, R 谢谢,R

This is quite simple. 这很简单。 I hope I understood your question correctly: 希望我能正确理解您的问题:

$i = 0;
while ( ( $row = mysql_fetch_object( $result ) ) !== false ) {
    if ($i < 3) {
        // First code
    } else {
        // Second code
    }
    $i += 1;
}

Firstly, you should avoid using any mysql_ functions as they are all deprecated and will be removed from future versions of PHP. 首先,您应避免使用任何mysql_函数,因为它们已全部弃用,并将从以后的PHP版本中删除。 I'd recommend using PDO instead. 我建议改用PDO。 See this tutorial to get started. 请参阅本教程以开始使用。

Then, it'll simply be a case of doing something like this: 然后,只需执行以下操作:

foreach($db->query($query) as $index => $row) {
    if ($index < 3) {
        // first 3 items
    } else {
        // other items
    }
}

You can do it easaly with an simple counter and an if statement: 您可以使用简单的计数器和if语句轻松完成此操作:

$count = 0;

while(fetching) {
    if($count < 3) {
        // items 1, 2 and 3
    } else {
        // do normal
    }

    $count++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM