开发者

divide php mysql result left and right

divide php mysql result left and right

 $flag=0;
 if($q->num_rows > 0) :
     echo '<div id="testimonial">';
     while($r = $q->fetch_array开发者_如何学Python(MYSQLI_ASSOC)) :
         if($flag=0) :
              $class=test1; $flag=1;
         else :
              $class=test2; $flag=0;
         endif;

         echo '<div class="'.$class.'">';
           echo '<span class="left">';
           echo '<p>'.$r['compname'].'</p>';
           echo '<p>'.$r['position'].'</p>';
           echo '</span>';
           echo '<span class="right">';
           echo '<p>'.$r['testimonial'].'</p>';
           echo '</span>';
         echo '</div>';
     endwhile;
     echo '</div>';
 else :
     echo '<h1>Coming Soon</h1>';
 endif;

i want the result look like the picture! seems my php code doesnt work out the css class. its only showing 1 class test1 when i echoing the result. so all the result left align.


if($flag=0)
        ^ should be ==

This is easier though with:

$i = 0;
while (…) {
    $class = $i++ % 2 ? 'test1' : 'test2';
}


You have an assignment in your if statement instead of a comparison: if($flag=0).


$class=test1; $flag=1;
       ^----^---

you're missing some quotes there too. As it stands now, you're trying to assign a constant named "test1" and "test2", which are most likely not defined, so they'll evaluate to an empty string.


 $flag=0;
 if($q->num_rows > 0) :
     echo '<div id="testimonial">';
     while($r = $q->fetch_array(MYSQLI_ASSOC)) :
         if($flag==0) :
              $class='test1'; $flag=1;
         else :
              $class='test2'; $flag=0;
         endif;

         echo '<div class="'.$class.'">';
           echo '<span class="left">';
           echo '<p>'.$r['compname'].'</p>';
           echo '<p>'.$r['position'].'</p>';
           echo '</span>';
           echo '<span class="right">';
           echo '<p>'.$r['testimonial'].'</p>';
           echo '</span>';
         echo '</div>';
     endwhile;
     echo '</div>';
 else :
     echo '<h1>Coming Soon</h1>';
 endif;

This should work!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜