0

Using codeigniter framework i have this code here

<?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 -->'; 
} 
?>

now i want to except first result or first post in my case ? it is possible

and another question may i do something like display posts but one by one i mean align one right and second one left?

Zumry Mohamed
  • 9,318
  • 5
  • 46
  • 51
AhmedEls
  • 708
  • 2
  • 9
  • 25

3 Answers3

1

You can initialize a variable outside the foreach and count inside the iteration.

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

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

Option 2: Use for loop instead of foreach

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

Using CSS to float your posts:

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

.post:nth-child(even){
float: right;
}
Sarvap Praharanayuthan
  • 4,212
  • 7
  • 47
  • 72
0

You can declare a var $count outside the foreach loop, initilize it to 0 and increment it at the end of the loop. At the beginning of the loop check if the $count variable is 0 - if that is the case - skip the echo. To display one row aligned to the left and one to the right check the same $count variable: if $count%2 == 0 then align one direction else the other.

0
foreach($value as $k => $v)
{
    if($k === 0)
    {
        continue;
    }
    // rest your case
}
Tpojka
  • 6,996
  • 2
  • 29
  • 39