php ile çoklu renk ile mysql veri getiriliyor

4 Cevap

nasıl ben php ile mysql veri alıp ve benzeri .... mutliple renkli görüntüleyebilirsiniz

[gray]first post
[white]second post
[gray]third post
[white]forth post

i mysql_fetch_array biliyorum ama nasıl ben yukarıdaki gibi çoklu renk ile verileri görüntüleyebilir

4 Cevap

Eğer modül operatörü kullanılarak hesaplanabilir bir "dahi" ya da "garip" satırında (at least, if you want two colors), üzerinde ise bilerek, hatları üzerinden döngü vardır.

Örneğin, hızlı bir fikir olarak, aşağıdaki kod:

$arr = array(
  'first',
  'second',
  'third',
  'fourth',
);

$i = 0;
foreach ($arr as $line) {
  $class = ($i%2 ? 'odd' : 'even');
  echo '<div class="' . $class . '">' . htmlspecialchars($line) . '</div>' . "\n";
  $i++;
}

Bu HTML çıktı verecektir:

<div class="even">first</div>
<div class="odd">second</div>
<div class="even">third</div>
<div class="odd">fourth</div>

Ve şimdi, sizin kadar iki .odd yapılandırmak ve .even CSS sınıfları istediğiniz renkleri elde etmek için.

Bunu yapmanın birçok yolu (bu örnekte daha sofistike çoğu) vardır ama görünüşte basit bir çözüm arıyoruz.

for($c=true; false!==($row=mysql_fetch_array($result, MYSQL_ASSOC)); $c=!$c) {
  $class = $c ? 'even':'odd';
  echo '<div class="', $class, '">', $row['x'], '</div>', "\n";
}

$c ? 'even':'odd'; tests whether $c is true or false and the result is 'even' if it is true and 'odd' if it is false. I.e. $class is either 'even' or 'odd' depending on the value of $c.
The for-loop starts with $c=true (initialization part of the for-loop). At the end of each iteration of the loop $c=!$c is executed which flips $c from true->false or false->true, i.e. $c alternates between true and false.

You can also use javascript to let the client add the coloring as an enhancement.
E.g. using jquery:

<html>
  <head>
    <style type="text/css">
      tr.even td { background-color: red; }
      tr.odd td { background-color: blue; }
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
    <script type="text/javascript">
      $(document).ready( function() {
        $('#t1 tr:odd').addClass('even');
        $('#t1 tr:even').addClass('odd');
      });
    </script>
  </head>
  <body>
    <table id="t1">
      <tr><td>a</td><td>A</td></tr>
      <tr><td>b</td><td>B</td></tr>
      <tr><td>c</td><td>C</td></tr>
      <tr><td>d</td><td>D</td></tr>
      <tr><td>e</td><td>E</td></tr>
    </table>
  </body>
</html>

Itay Moav ait ve Pascal Martin'in örnekleri çalışır, ama sayısal bir dizi varsa biraz kodunu kolaylaştırabilirsiniz:

foreach ($rows as $i => $row){
    $even_odd = ($i%2) ? 'even' : 'odd';
    echo "<tr class='$even_odd'>......";
}

Döngüde

$i=0;
foreach ($rows as $row){
  echo '<tr class="color'.($i%2).'">.........';
  $i++;
}

css

.color1{ background-color: white}
.color0{ background-color: silver}