PHP sorunu döngü?

7 Cevap

Ben başka bir renk bir renk ve ikinci satırda ilk satırı görüntülemek için çalışıyorum ama benim kod örneğin her iki renk iki sonucu görüntüler benim kod 10 sonuçlarını göstererek sonuçları katına çıkacak ben 5 sonuç söylüyorlar sağlar. Ben nasıl bu sorunu düzeltebilirim?

İşte php kodudur.

while ($row = mysqli_fetch_assoc($dbc)) {
    //first row
    echo '<h3 class="title"><a href="#" title="">' . $row['title'] .'</a></h3>';
    echo '<div class="summary"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';

    //second row
    echo '<h3 class="title-2"><a href="#" title="">' . $row['title'] .'</a></h3>';
    echo '<div class="summary-2"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';

}

7 Cevap

Her satırda sınıfını değiştirmek gerekir:

$count = 0;

while ($row = mysqli_fetch_assoc($dbc)) {

    if( $count % 2 == 0 ) { 
        $classMod = '';
    } else {
        $classMod = '-2';
    }

    //first row
    echo '<h3 class="title' . $classMod . '"><a href="#" title="">' . $row['title'] .'</a></h3>';
    echo '<div class="summary' . $classMod . '"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';

    $count++;
}

kodunuzu bu gibi olmalıdır

CSS

.odd { background: #CCC }
.event { background: #666 }

PHP

   $c = true;
   while ($row = mysqli_fetch_assoc($dbc)) {
   $style =  (($c = !$c)?' odd':' even');     
   echo '<h3 class="title '.$style.'"><a href="#" title="">' . $row['title'] .'</a></h3>';
   echo '<div class="summary '.$style.'"><a href="#" title="">' .substr($row['content'],0,255) . '</a></div>';

}

İşte az tekrarı ile bir çözüm:

$count = 0;
while (($row = mysqli_fetch_assoc($dbc)) && ++$count) {
    printf(
         '<h3 class="title%1$s"><a href="#" title="">%2$s</a></h3>'
       . '<div class="summary%1$s"><a href="#" title="">%3$s</a></div>'
       , $count % 2 ? "" : "-2"
       , $row['title'] // might want to use htmlentities() here...
       , substr($row['content'], 0, 255) // and here...
    );
}

Sizin kod sadece iki kez her sonuç görüntüler. Satırlar arasında geçiş yapmak için bir koşullu (örneğin bir tamsayı veya boolean) kullanın (gibi: true, sonra yeşil, false, sonra kırmızı ise).

Bir boolean için çok gibi geçerli değerini değiştirebilir:

bool = !bool;

Ekstra puan çift:

  • Sen değil (normalde) bu kadar çok sınıfları gerekir. Eğer varsa <div class="stripe"> sizin konteyner gibi, sen örneğin ile öğeleri hedef olabilir .stripe h3 CSS.
  • Eğer .stripe h3 ile CSS bile garip ve öğeleri hedef varsa, o zaman sadece tek öğeleri üzerine yazabilirsiniz.
  • Mükemmel bir dünyada, sen CSS sunum tutmalı. Bütün tarayıcılar ama IE7 ve desteğinin altında div:odd bir ebeveynin herhangi bir tek çocuğu hedef. Bu hafifçe HTML yapısını değiştirilmesi gerekebilir. Aşağıda IE7 ve için, ben JavaScript yerine PHP ile sınıfları eklemek istiyorum. IE7 artık yok o zaman sadece JS kaldırabilirsiniz.

Bu arada, siz de bu kodu yapabilirdi:

while ($row = mysqli_fetch_assoc($dbc)) {
    echo '<h3 class="title"><a href="#" title="">' . $row['title'] .'</a></h3>';
    echo '<div class="summary"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';

    if($row = mysqli_fetch_assoc($dbc)){
        echo '<h3 class="title-2"><a href="#" title="">' . $row['title'] .'</a></h3>';
        echo '<div class="summary-2"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';
    }
}

IMO, istemci tarafında kod kritik olmayan, sunum katmanı dekorasyonu bu tür delege için hiçbir bahane yoktur.

Sadece jQuery gibi bir kitaplık kullanmak ve böylece gibi tek ve çift satır erişmek:

<script>
$(document).ready(function()
{

  //for your table rows
  $("tr:even").css("background-color", "#F4F4F0");
  $("tr:odd").css("background-color", "#EFF1F2");
});
</script>

Bizi sonraki font etiketlerini üreten olacak.