2015-11-26 35 views
0

使用笨框架我這裏有這個代碼foreach循環如何期待第一個結果?

<?php 

foreach ($allposts as $posts) { 
echo '<div class="col-sm-1"> 
    <div class="thumbnail"> 
    <img class="img-responsive user-photo" src="https://ssl.gstatic.com/accounts/ui/avatar_2x.png"> 
    </div><!-- /thumbnail --> 
    </div><!-- /col-sm-1 --> 

    <div class="col-sm-11"> 
    <div class="panel panel-default companel"> 
    <div class="panel-heading companel-heading"> 
    <strong><a href="'.base_url('home/user/'.$posts->author_name).'">'.$posts->author_name.'</a></strong> <span class="text-muted">'.unix_to_human($posts->post_date).'</span> 
    </div> 
    <div class="panel-body"> 
    '.$posts->post.' 
    </div><!-- /panel-body --> 
    </div><!-- /panel panel-default --> 
    </div><!-- /col-sm-11 -->'; 
} 
?> 

現在我想除了第一個結果或者第一篇文章在我的情況?它是可能的

和另一個問題,我可以做一些像顯示帖子,但一個接一個我的意思是對齊一個權利和第二個離開?

回答

1

你可以在foreach之外初始化一個變量並在迭代內計數。

$count=0; 
foreach($allposts as $posts) 
{ 
    $count++; // incrememnt 

    if($count==1) 
    { 
     // this is the first post 
    } 
} 

選項2: 用於循環,而不是foreach

for($i=0; $i<=count($allposts); $i++) 
{ 
    if($i==0) // this is the first post. 
    echo $allposts[$i]; 
} 

使用CSS浮動自己的帖子:

.post:nth-child(odd){ 
float: left; 
} 

.post:nth-child(even){ 
float: right; 
} 
0

你可以在foreach循環外聲明一個var $ count,將它初始化爲0,並在循環結尾增加它。在循環開始時檢查$ count變量是否爲0 - 如果是這種情況 - 跳過回聲。 要顯示一行對齊左側和右側一行,請檢查同一個$ count變量:if $ count%2 == 0然後對另一個方向對齊。

0
foreach($value as $k => $v) 
{ 
    if($k === 0) 
    { 
     continue; 
    } 
    // rest your case 
} 
相關問題