代码之家  ›  专栏  ›  技术社区  ›  David

如何操作数组中的前3个条目

  •  0
  • David  · 技术社区  · 15 年前

    我目前正在一个项目中使用 而($r=mysql_fetch_array($the_data))。

    我想让第一个,说3-4个结果的背景色不同,然后把其余的保留为已经设计好的,我肯定这里有一些简单的选择,但我不知道到哪里去看真正的…

    希望你能帮忙,谢谢!

    3 回复  |  直到 15 年前
        1
  •  0
  •   KyleFarris    15 年前
    <?php    
    $i = 0;
    $r = mysql_fetch_array($the_data);
    foreach($r as $row) {
        if($i <= 4) {
            // Do special styling...
        } else {
            // Do normal styling.
        }
        $i++;
    }
    ?>
    

        2
  •  0
  •   user152759    15 年前

    #specialResult {background-color: #fff; }
    #normalResult {background-color: #000; }
    

    $i = 0;
    while (...)
    {
        if ($i < 4) echo "<div class='specialResult'>";
        else echo "<div class='normalResult'>"; 
    
        .... rest of your code
    
        $i++;
    }
    

    $i = 0;
    while (...)
    {
        ?><div class='<?php echo ($i++ < 4 ?"specialResult":"normalResult); ?>'><?php
    
        .... rest of your code
    }
    
        3
  •  0
  •   swordofpain    15 年前

    $i = 0;
    while ( ( $row = mysql_fetch_assoc ( $result ) ) && $i < 4 ) {
        /* Your first 4 rows */
        echo 'Special : ' . $row['title'];
        ++$i;   // Don't forget to increment
    } while ( $row = mysql_fetch_assoc () ) {
        /* Normal way */
        echo $row['title'] . ' is already outdated';
    }