Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] Kolorowanie warunkowe w pętli z wynikami
f41t3r
post
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 7.03.2006

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


Mam problem z kolorowaniem wyników wyświetlanych przez skrypt mysql. Chodzi mi o to ażeby wiersze zawierające konkretne wartości tj. error, warning, critical itp. były innego odpowiedniego koloru.

Tak wygląda dotychdczasowy skrypt, który musze się przyznać, że nie działa za dobrze tj. podczas warunku cały czas zwraca wartość false i wykonuje mi tylko elsa czyli podkreśla wszystkie linie na #dddddd


  1. <?php
  2.  
  3.  
  4. @ $query="select * from syslogd.syslogd where
  5. MsgDate='".mysql_escape_string($HTTP_GET_VARS['bydate'])."' AND (Msghostname 
  6.  
  7. LIKE '".mysql_escape_string($HTTP_GET_VARS['Urzadzenie'])."' AND Msgpriority 
  8.  
  9. LIKE '".mysql_escape_string($HTTP_GET_VARS['priority1'])."' OR Msgpriority 
  10.  
  11. LIKE '".mysql_escape_string($HTTP_GET_VARS['priority2'])."' oR Msgpriority 
  12.  
  13. LIKE '".mysql_escape_string($HTTP_GET_VARS['priority3'])."' OR Msgpriority 
  14.  
  15. LIKE '".mysql_escape_string($HTTP_GET_VARS['priority4'])."' OR Msgpriority 
  16.  
  17. LIKE '".mysql_escape_string($HTTP_GET_VARS['priority5'])."') ORDER BY 
  18.  
  19. '".mysql_escape_string($HTTP_GET_VARS['order'])."'";
  20. $result=mysql_query("$query");
  21.  
  22. print "<table border=0>\n";
  23. print "<tr><td><a>Date</a></td><td><a >Time</a></td><td><a 
  24.  
  25. >Priority</a></td><td><a >Hostname</a></td><td><a>Text</a></td></tr>";
  26. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  27. print "\t<tr>\n";
  28. foreach ($line as $col_value) {
  29. print "\t<td bgcolor=";
  30.  
  31. if ($col_value=="%Alert%") {
  32. print "#DD8787";
  33. } 
  34.  
  35. elseif ($col_value=="%Emergency%"){
  36. print "#DD6767";
  37. }
  38.  
  39. elseif ($col_value=="%Critical%"){
  40. print "#DD5757";
  41. }
  42.  
  43. elseif ($col_value=="%Error%"){
  44. print "#87DD91";
  45. }
  46.  
  47. elseif ($col_value=="%Warning%"){
  48. print "#FEFF87";
  49. }
  50.  
  51.  
  52. else {
  53. print "#dddddd";
  54. };
  55.  
  56.  
  57. print ">$col_value</td>\n";
  58. }
  59. print "\t</tr>\n";
  60. }
  61. print "</table>\n";
  62.  
  63. ?>


Jak na razie utknąłem i nie wiem co dalej z tym zrobić.
Proszę o pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
TomASS
post
Post #2





Grupa: Zarejestrowani
Postów: 1 660
Pomógł: 13
Dołączył: 9.06.2004
Skąd: Wrocław i okolice

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


1. Zamiast
  1. <td bgcolor="....">
ładniej dać:
  1. <tr style="background-color: .....">

2. Po co ta "małpa":
  1. <?php
  2. @ $query=....
  3. ?>

3. Czy na prawde potrzebujesz tak komplikować zapytanie:
  1. SELECT *
  2. FROM syslogd.syslogd
  3. WHERE MsgDate='".mysql_escape_string($HTTP_GET_VARS['bydate'])."' AND (Msghostname
  4.  
  5. LIKE '".mysql_escape_string($HTTP_GET_VARS['Urzadzenie'])."' AND Msgpriority
  6.  
  7. LIKE '".mysql_escape_string($HTTP_GET_VARS['priority1'])."' OR Msgpriority
  8.  
  9. LIKE '".mysql_escape_string($HTTP_GET_VARS['priority2'])."' OR Msgpriority
  10.  
  11. LIKE '".mysql_escape_string($HTTP_GET_VARS['priority3'])."' OR Msgpriority
  12.  
  13. LIKE '".mysql_escape_string($HTTP_GET_VARS['priority4'])."' OR Msgpriority
  14.  
  15. LIKE '".mysql_escape_string($HTTP_GET_VARS['priority5'])."') ORDER BY

4. Zrób sobie:
  1. <?php
  2.  
  3. $query....
  4. $result = mysql_query($query);
  5. while ($row = mysql_fetch_array($result)){
  6.  print_r($row);
  7. }
  8.  
  9. ?>

I zobacz co Ci się wyświetla - być może nie ma tam wartości typu %Error% tylko jakieś całkiem inne - a jakie? To właśnie zobaczysz.
5. Zamiast tyle ifów/elsów możesz elegancko to napisać w tabeli albo dzięki switch.
6 Zamiast
  1. <?php
  2. print "#DD6767";
  3. ?>
pisz
  1. <?php
  2. print '#DD6767';
  3. ?>

7. Wyrzuć tą książkę - ona się do niczego nie nadaje (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 30.09.2025 - 05:30