2013-04-18 43 views
-1

我正在嘗試使用最近的帖子(博客)創建主頁面。從最後8行中選擇2個特定行用於不同的顯示

我希望最近的2個最近的使用不同的顯示器[html & css]和其他6不同於他們。 就像這幅畫...... http://i.imgur.com/T0imWxM.jpg

編輯1

// Gets 2 recent posts [displayed in 2 big boxes] 
    function get_important_posts() { 
    global $dblink; 
    mysqli_set_charset($dblink, 'utf8'); 
     // if() { 
      $result = mysqli_query($dblink ,"SELECT * 
                    FROM `posts` 
                    ORDER BY `post_id` 
                    DESC LIMIT 0,2") or die(mysqli_connect_error($dblink)); 
      $rowNumber = 0; 
      while ($row = mysqli_fetch_array($result)) { 
       if ($rowNumber < 1) { 
        echo '<div class="bigbox right"> 
        <a href="post.php?id=' . $row['post_id'] .'"> 
        <div class="bigboximg"><img src="'. $row['bigthumb'] .'" width="390" alt="' . $row['title'] .'"></div></a> 
        <a href="post.php?id=' . $row['post_id'] .'"><div class="bigboxtitle">' . $row['title'] . '</div></a> 
        <div class="bigboxexcerpt">'. $row['excerpt'] . '</div> 
        </div>'; 
        $rowNumber++; 
       } else { 
        echo '<div class="bigbox left"> 
        <a href="post.php?id=' . $row['post_id'] .'"> 
        <div class="bigboximg"><img src="'. $row['bigthumb'] .'" width="390" alt="' . $row['title'] .'"></div></a> 
        <a href="post.php?id=' . $row['post_id'] .'"> 
        <div class="bigboxtitle">' . $row['title'] . '</div></a> 
        <div class="bigboxexcerpt">'. $row['excerpt'] . '</div></div>'; 
        $rowNumber++; 
        } 

      } 
    } 
//} 

    // Gets 5 recent posts after the 2 recent posts 
    function get_posts() { 
    global $dblink; 
    mysqli_set_charset($dblink, 'utf8'); 
    $result = mysqli_query($dblink ,"SELECT * 
                  FROM `posts` 
                  ORDER BY `post_id` 
                  DESC LIMIT 2,5") or die(mysqli_connect_error($dblink)); 
    while ($row = mysqli_fetch_array($result)) { 
      echo '<div class="box"> 
      <a href="post.php?id=' . $row['post_id'] .'"> 
      <div class="boximg"><img src="'. $row['smallthumb'] .'" width="130" alt="' . $row['title'] .'"></div></a> 
      <a href="post.php?id=' . $row['post_id'] .'"> 
      <div class="boxtitle">' . $row['title'] . '</div></a> 
      <div class="boxexcerpt">'. $row['excerpt'] . '</div></div>';    
     } 
    } 

回答

0
$result = mysqli_query($dblink ,"SELECT * FROM `posts` ORDER BY `post_id` DESC LIMIT 10"); 
$rowNumber = 0; 
while ($row = mysqli_fetch_array($result)) { 
    if($rowNumber++ < 2){ 
     echo "special row"; 
    } 
    echo '<a href="post.php?id=' . $row['post_id'] .'"><strong>' . $row['title'] . '</strong></a><br>'. $row['excerpt']; 
} 
1

簡單的使用計數器變量,並添加一類特殊的標記爲第2位。

$special_rows = 2; 
    while ($row = mysqli_fetch_array($result)) { 
     $class = ''; 
     if ($special_rows > 0) 
      $class = 'special'; 
     echo '<a class="' . $class . '" href="post.php?id=' . $row['post_id'] .'"><strong>' . $row['title'] . '</strong></a><br>'. $row['excerpt']; 
     $special_rows --; 
    } 

或者,如果你想不同的標記:

$special_rows = 2; 
    while ($row = mysqli_fetch_array($result)) { 
     if ($special_rows > 0) { 
      // First or second row 
      echo '<a href="post.php?id=' . $row['post_id'] .'"><strong>' . $row['title'] . '</strong></a><br>'. $row['excerpt']; 
     } 
     else { 
      echo 'All the other boring rows'; 
     } 
     $special_rows --; 
    } 
+0

Hampus,感謝您的快速回復.. 這可以幫助你理解它.. 的http://我。 imgur.com/T0imWxM.jpg –