Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Przemienne kolory rekordów w tabeli
eurosoft
post 9.11.2010, 09:00:31
Post #1





Grupa: Zarejestrowani
Postów: 179
Pomógł: 0
Dołączył: 5.11.2010

Ostrzeżenie: (0%)
-----


w jaki sposób można zrobić przemienne kolorowanie kolejnych rekordów w tabeli?
Poniżej fragment kodu generującego tabelę:

  1. ...
  2. echo "<th>Ilość</th>";
  3. echo "<th>Osoba wpisująca</th>";
  4. echo "<th>Info</th>";
  5.  
  6. while($r = mysql_fetch_array($result) and $z = mysql_fetch_array($result1))
  7.  
  8. {
  9. echo "<tr>";
  10. echo "<td width=40 align=center>".$r[0]."</td>";
  11. echo "<td width=140>".$r[1]."</td>";
  12. echo "<td width=50 align=center>".$r[2]."</td>";
  13. echo "<td width=60 align=center>".$r[3]."</td>";
  14.  
  15. ....


Ten post edytował eurosoft 9.11.2010, 09:01:16
Go to the top of the page
+Quote Post
Ghost_78
post 9.11.2010, 09:04:25
Post #2





Grupa: Zarejestrowani
Postów: 222
Pomógł: 34
Dołączył: 3.11.2010

Ostrzeżenie: (0%)
-----


najprosciej tak:
  1. $i=0;
  2. while($r = mysql_fetch_array($result) and $z = mysql_fetch_array($result1)){
  3. if ($i==0){
  4. $color='background-color: red;';
  5. $i=1;
  6. }else{
  7. $color='background-color: green;';
  8. $i=0;
  9. }
  10. echo "<tr style=\"$color\">";
  11. echo "<td width=40 align=center>".$r[0]."</td>";
  12. echo "<td width=140>".$r[1]."</td>";
  13. echo "<td width=50 align=center>".$r[2]."</td>";
  14. echo "<td width=60 align=center>".$r[3]."</td>";


--------------------
Always look on the bright side of life ;-)
Go to the top of the page
+Quote Post
Quantum
post 9.11.2010, 09:05:44
Post #3





Grupa: Zarejestrowani
Postów: 450
Pomógł: 84
Dołączył: 27.11.2008
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


  1. for ( $i = 0; $i < 10; $i ++ )
  2. {
  3. if ( $i % 2 == 0 )
  4. {
  5. echo 'kolor 1';
  6. }
  7. else
  8. {
  9. echo 'kolor 2';
  10. }
  11. }


zadeklaruj sobie przed while zmienną $i = 0, a pod koniec pętli ją zwiększasz o 1 - ++$i; sprawdzanie polega na dzieleniu przez modulo, jeżeli jest liczbą parzystą wyświetli kolor 1, jeżeli nie kolor 2
Go to the top of the page
+Quote Post
phpion
post 9.11.2010, 09:05:53
Post #4





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Cytat(Ghost_78 @ 9.11.2010, 09:04:25 ) *
najprosciej tak:

Nie, najprościej tak:
http://www.w3.org/Style/Examples/007/evenodd.html
Go to the top of the page
+Quote Post
Quantum
post 9.11.2010, 09:08:45
Post #5





Grupa: Zarejestrowani
Postów: 450
Pomógł: 84
Dołączył: 27.11.2008
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Cytat(phpion)


a jak z obsługą even/odd w przeglądarkach ? z tego co pamiętam były jakieś problemy z ie.
Go to the top of the page
+Quote Post
norbis
post 9.11.2010, 09:16:35
Post #6





Grupa: Zarejestrowani
Postów: 42
Pomógł: 8
Dołączył: 30.04.2008

Ostrzeżenie: (0%)
-----


Koledzy dobrze kombinują, ale można jeszcze uprościć winksmiley.jpg

  1. $i=0;
  2. while($r = mysql_fetch_array($result) and $z = mysql_fetch_array($result1)){
  3. $i++;
  4. echo '<tr style="'.($i%2==0?'background-color: red;':'background-color: green;').'">';
  5. echo "<td width=40 align=center>".$r[0]."</td>";
  6. echo "<td width=140>".$r[1]."</td>";
  7. echo "<td width=50 align=center>".$r[2]."</td>";
  8. echo "<td width=60 align=center>".$r[3]."</td>";
  9. }


Ten post edytował norbis 9.11.2010, 09:18:14
Go to the top of the page
+Quote Post
eurosoft
post 9.11.2010, 09:22:05
Post #7





Grupa: Zarejestrowani
Postów: 179
Pomógł: 0
Dołączył: 5.11.2010

Ostrzeżenie: (0%)
-----


Niestety nie działa, po wprowadzeniu modyfikacji do kodu tabela pokazyje tylko jeden rekord, a dodatkowo pierwsze są puste. sad.gif
Go to the top of the page
+Quote Post
norbis
post 9.11.2010, 09:34:25
Post #8





Grupa: Zarejestrowani
Postów: 42
Pomógł: 8
Dołączył: 30.04.2008

Ostrzeżenie: (0%)
-----


A zamykasz <tr>?
Go to the top of the page
+Quote Post
eurosoft
post 9.11.2010, 12:53:54
Post #9





Grupa: Zarejestrowani
Postów: 179
Pomógł: 0
Dołączył: 5.11.2010

Ostrzeżenie: (0%)
-----


tak oczywiście tr jes zamknięty
Go to the top of the page
+Quote Post
andrzej_
post 9.11.2010, 16:06:02
Post #10





Grupa: Zarejestrowani
Postów: 60
Pomógł: 1
Dołączył: 11.06.2010

Ostrzeżenie: (0%)
-----


Poszedłbym za radą phpion.

  1.  
  2. <style type="text/css">
  3. .tab tr:nth-child(odd) {
  4. border: 0px;
  5. background-color:#e2e1e1;
  6. }
  7.  
  8. .tab tr:nth-child(even) {
  9. border: 0px;
  10. background-color:#f6f6f6; }





  1.  
  2. <table class='tab'>
  3.  
  4. ...
  5. echo "<th>Ilość</th>";
  6. echo "<th>Osoba wpisująca</th>";
  7. echo "<th>Info</th>";
  8.  
  9. while($r = mysql_fetch_array($result) and $z = mysql_fetch_array($result1))
  10.  
  11. {
  12. echo "<tr>";
  13. echo "<td width=40 align=center>".$r[0]."</td>";
  14. echo "<td width=140>".$r[1]."</td>";
  15. echo "<td width=50 align=center>".$r[2]."</td>";
  16. echo "<td width=60 align=center>".$r[3]."</td>";
  17.  
  18. ....


Ten post edytował andrzej_ 9.11.2010, 16:06:52
Go to the top of the page
+Quote Post
minolone
post 9.11.2010, 16:20:14
Post #11





Grupa: Zarejestrowani
Postów: 141
Pomógł: 24
Dołączył: 21.06.2008

Ostrzeżenie: (0%)
-----


Można też zastosować bibliotekę jQuery. Link
Go to the top of the page
+Quote Post
eurosoft
post 9.11.2010, 17:00:40
Post #12





Grupa: Zarejestrowani
Postów: 179
Pomógł: 0
Dołączył: 5.11.2010

Ostrzeżenie: (0%)
-----


dziękuję wszystkim
widać jak się siedzi to i błąd się znajdzie, a błąd był w pisowni....ehhhhh
brakowało średnika w jednej z linii
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 25.06.2025 - 13:27