2014-01-21 27 views
-1

我有這個代碼的問題,它創建一個index.html文件鏈接從給定的RSS鏈接。我試圖在index.html結尾文件中生成每個rss源的列,並且列將按每行3個進行分組。所以在腳本處理rss鏈接數組的第三個鏈接之後,我想在新行中重複這個過程,但是我沒有得到正確的排序,我想要計算或插入結束和打開標記。每3 colums的數據生成一個新的div行

<?php 

require_once('magpierss/rss_fetch.inc'); // RSS library to fetch RSS news 

$rss_links = array(
    'NYT World' => 'http://rss.nytimes.com/services/xml/rss/nyt/World.xml', 
    'NYT US' => 'http://rss.nytimes.com/services/xml/rss/nyt/US.xml', 
    'NYT Business' => 'http://rss.nytimes.com/services/xml/rss/nyt/Business.xml', 
    'NYT Technology' => 'http://rss.nytimes.com/services/xml/rss/nyt/Technology.xml', 
    'NYT Sports' => 'http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml' 
    ); 

$limit = 10; // Notice limit per RSS 
$count = 0; 

if ($limit) { 
    $per_column = floor((count($rss_links) * $limit)/3); 
} else { 
    foreach ($rss_links as $url) { 
     $rss = fetch_rss($url); 
     $count += count($rss->items); 
    } 

    $per_column = floor($count/3); 
} 

$html = '<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<title>RSS GENERATE</title> 
<meta name="description" content=""> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="css/bootstrap.css"> 
</head> 
<body> 
<div class="container"> 
<div class="row">'; 

$count = 0; 
$rowCount = 0; 
// print_r($rss_links); 
// break; 
foreach ($rss_links as $url) { 

    if ($rowCount % 3 === 0) { 
     $html .= '</div><div class="row">'; 
    } 

    $rss = fetch_rss($url); 

    if ($count == 0) { 
     $html .= '<div class="col-xs-6 col-sm-4"> 
     <h1>'.$rss->channel['title'].'</h1> 
     <ul class="list-unstyled">'; 
    } 

    // $html .= '<h1>'.$rss->channel['title'].'</h1>'; 
    $c = 0; 

    foreach ($rss->items as $item) { 
     $html .= '<li><a href="' . $item['link'] . '">' . $item['title'] . $count .'</a></li>'; 
     $count++; $c++; 

     if ($limit && $limit == $c) { 
      continue(2); 
     } 

     if ($count == ($per_column + 1)) { 
      $count = 0; 
      $html .= '</ul></div><div class="col-xs-6 col-sm-4"> 
      <ul class="list-unstyled">'; 
     } 
    } 
    $rowCount++; 
} 

$html .= '</ul> 
</div> 
</div> 
</div> 
</body> 
</html>'; 

file_put_contents('index.html', $html); 
?> 
+0

你得到了什麼輸出?顯示輸出。同時舉一個你想要的輸出的例子。 – user2936213

回答

0

這裏是我在PHP '計數':

$count = 0; 
$count_total = 10; // or use dynamic count: count($all_posts) 
$columns = 3; 
foreach ($all_posts as $k=>$v) { 
    $count++; 
    $is_first_column = $is_last_column = false; // always set to false, then set to true only when the condition matches! 
    // Now check is this a post for the first column 
    if($count==1 || ($count-1)%$columns==0) { // check if it's 1st and every 4,7,10 post and place it in 'column 1' 
     // set additional class for every post that need to be placed in the first column 
     $current_column_in_row = 1; 
     $is_first_column = true; 
    } 
    else { 
     $current_column_in_row = ($count-1)%$columns + 1; 
     // for rows 4,7: (4-1)%3+1=(7-1)%3+1=1 
     // for rows 2,5,8: (2-1)%3+1=(5-1)%3+1=2 
     // for rows 3,6,9: (3-1)%3+1=(6-1)%3+1=3 
    } 

    if($count>=$count_total) { 
     $is_last_column = true; 
    } 
} 

線 「$ current_column_in_row =($計數-1)%$列+ 1;」未經測試(我一般只標出第一/最後一列),但它應該正確計算...

CSS

下面是一個提出的解決方案的另一個問題:https://stackoverflow.com/a/21202987/3205524

您可以輕鬆地使用CSS清除浮動像這樣每.column_1:

.column_1 { 
    clear: both; 
} 

這樣每個.column_1進入一個新行!