2011-10-24 5 views
0

SELECT 문장의 MySQL 데이터베이스의 데이터로 채워지는 동적으로 생성 된 테이블이 있다고 가정 해 봅니다.세션 값이있는 페인팅 테이블

이제 테이블 외부의 if 절 내부에 정의 된 변수에 액세스해야합니다. 그것을 만들기 위해 일부 $_SESSION 값을 사용하고 있지만이 색을 전체 행에 적용하려고하면 세션 값이 저장되는 단일 행 대신 모든 표가 색이됩니다. 나는 $RowCt 값을 저장해야 할 필요가 있다고 생각하지만 그 방법을 모르겠습니다.

나는이 문제를 꽤 오랫동안 보아 왔으며,이 코드를 계속보고 나서 약간 눈이 멀었습니다. 어떤 도움을 주셔서 감사합니다.

코드 :

<?php include("../includes/header.php")?> 
<div class="content" align="center"> 

<?php 
    function SQLResultTable($Query) 
    { 
     $link = mysql_connect("localhost", "...", "...") or die('Could not connect: ' . mysql_error()); 
     mysql_select_db("...") or die('Could not select database'); 
     $Table = ""; 

     $Table.= "<table border='0' align='center'>"; 

     $Result = mysql_query($Query); //Execute the query 
     if(mysql_error()) 
     { 
      $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>"; 
     } 
     else 
     { 
      //Header Row with Field Names 
      $NumFields = mysql_num_fields($Result); 

      $Table.= "<tr style=\"background-color: #D00000; float:center; font-size:14px; color: #FFFFFF;\" align='center'>"; 
      for ($i=0; $i < $NumFields; $i++) 
      {  
       if ((mysql_field_name($Result, $i))=="a1") { $Table.= "<th>a1</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a2") { $Table.= "<th>a2</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a3") { $Table.= "<th>a3</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a4") { $Table.= "<th>a4</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a5") { $Table.= "<th>a5</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a6") { $Table.= "<th>a6</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a7") { $Table.= "<th>a7</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a8") { $Table.= "<th>a8</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a9") { $Table.= "<th>a9</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a10") { $Table.= "<th>a10</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a11") { $Table.= "<th>a11</th>"; 
       } else if ((mysql_field_name($Result, $i))=="a12") { $Table.= "<th>a12</th>"; 
       } else { $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; } 
      } 
      $Table.= "</tr>"; 

      $RowCt = 0; 

      while($Row = mysql_fetch_assoc($Result)) 
      { 
       $timezone = new DateTimeZone("Europe/London"); 
       $date = new DateTime(); 
       $date->setTimezone($timezone); 
       $now = $date->format('H:i'); 

       if (!isset($_SESSION['b2'])) { 
        $Table.="<tr style='background-color:red;'>"; 
       } else if (!isset($_SESSION['b1'])) { 
        $Table.="<tr style='background-color:yellow;'>"; 
       } else { 
        $Table.="<tr style='background-color:white;'>"; 
       } 

       foreach($Row as $field => $value) { 
        if ($field == 'b1') { 
         if ($value > $now) { 
          $_SESSION['b1'] = $b1; 
          $Table.= "<td style='font-size:14px;' align='center'>$value</td>"; 
         } else { 
          $Table.= "<td style='font-size:14px;' align='center'>$value</td>"; 
         } 
        } else if ($field == 'b2') { 
         if ($value > $now) { 
          $_SESSION['b2'] = $b2; 
          $Table.= "<td style='font-size:14px;' align='center'>$value</td>"; 
         } else { 
          $Table.= "<td style='font-size:14px;' align='center'>$value</td>"; 
         } 
        } else { 
         $Table.= "<td style='font-size:14px;' align='center'>$value</td>"; 
        } 
       } 
       $Table.= "</tr>"; 
      } 
      $Table.= "<tr style=\"background-color: #FFFFFF; font-size:10px; color: #000000;\"><td colspan='$NumFields'><br />Query returned " . mysql_num_rows($Result) . " result(s).</td></tr>"; 
     } 
     $Table.= "</table>"; 
     return $Table; 
    } 
?> 

<?php 
    echo "<div align='center'><u><strong><a href='index.php'>Query Results</a></strong></u></div>"; 
    echo SQLResultTable("SELECT a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12 FROM test_db ORDER BY a6"); 
?> 
</div> 
이제

에서, $now 값이 실제 시간을 반환, 즉 8시 반; 및 $value 필드의 b1b2은 이전 양식에서 저장 한 것과 동일한 형식의 값을 반환합니다.

거래가 늦었을 때 (필드 b1의 값이 현재보다 큼) 노란색으로 표시됩니다. 매우 늦은 경우 (필드 b2의 값이 현재보다 큼) 행을 빨간색으로 표시합니다. 그렇지 않으면 행이 흰색 배경색에 있습니다.

위의 코드를 사용하면 전체 표가 색상이됩니다. 그리고 이것을 취소하는 방법을 찾을 수 없으므로 색상이 인 경우에만 세션 값이 저장된 행, 일명 늦게 또는 늦게 저장됩니다.

더 많은 도움이 필요하시면 언제든지 문의하십시오.

+0

응답 없음

확인 매 3 개 행이 전환 색상에 대한 해결책은 아래의 주석 PHP 코드 : 편집

귀하의 초기 질문이지만 먼저 코드를 정리하십시오. 그것은 읽을 수 있지만 중복이 많이 있습니다. 체육. 'foreach' 루프는 모두 여러 조건을 가지고 있습니다 ('Table. = " Bjoern

+0

이 답변을 주셔서 감사합니다. 나는 그것을 단순화하려고 노력할 것이다. 원래의 코드가 아니라고 생각한다. 원래의 문제에 더 많은 것을 생각하면, 나는 단지 * css 문제라고 생각하지 않습니다. 그래서 그것이 이런 식으로 게시되었습니다. :) – AleksanderKseniya

답변

1

현학적 인 것은 아니지만 이것을 작성하는 데 더 쉽고 두통을 유발하는 방법이있을 수 있습니다! 그러나 간단히 말해서, 당신의 코드가 거의 있다고 말할 수 있습니다. 당신의 테이블은 하루 중 시간에 따라 하나의 색깔로 나타 납니까? 경우

... 
while($Row = mysql_fetch_assoc($Result)) 
{ 
    $timezone = new DateTimeZone("Europe/London"); 
    $date = new DateTime(); 
    $date->setTimezone($timezone); 
    $now = $date->format('H:i'); 

    foreach($Row as $field => $value) { 
     if ($field == 'b1') { 
      if ($value > $now) { 
       $Table.= "<td style='font-size:14px; background-color:YELLOW;' align='center'>$value</td>"; 
      } else { 
       $Table.= "<td style='font-size:14px; background-color:WHITE;' align='center'>$value</td>"; 
      } 
     } else if ($field == 'b2') { 
      if ($value > $now) { 
       $Table.= "<td style='font-size:14px; background-color:RED;' align='center'>$value</td>"; 
      } else { 
       $Table.= "<td style='font-size:14px; background-color:WHITE;' align='center'>$value</td>"; 
      } 
     } else { 
      $Table.= "<td style='font-size:14px; background-color:WHITE;' align='center'>$value</td>"; 
     } 
    } 
    $Table.= "</tr>"; 
} 
... 

죄송합니다 : 나는 당신의 세션 변수 만 세트, 결코 해제 ...

이 충분할 같은

겠습니까 뭔가되고 함께 할 수있는 뭔가가 말할 것 색이 섞여있어! ;) 희망이 올바른 경로에 당신을 가리 키도록 도와주세요! (- 정말 빨리 쓴 내가 바로 그것을 가지고 희망!)

... 

// so this is my understanding of what I think this code must do 
// based on what your original code looks like and what your original 
// question was highlighting... 

// based on how you're calling the rows and checking the fields for 
// values 'b1' and 'b2', I'm making an assumption here that the fields 
// 'b1' and 'b2' will always both exist in every row you parse. Maybe you 
// shouldn't be checking the '$field' variable for equivalence to 'b1' 
// or 'b2', but rather the '$value' variable? Regardless, I'm throwing down 
// this code for the '$RowCt' question you had, it will alternate between the 
// 3 colours...I hope it helps... 

$RowCt = 0; 
while($Row = mysql_fetch_assoc($Result)) 
{ 
    // get the time, that's all working ok... 
    $timezone = new DateTimeZone("Europe/London"); 
    $date = new DateTime(); 
    $date->setTimezone($timezone); 
    $now = $date->format('H:i'); 

    // increase the counter, work out the colour... 
    $RowCt++; 

    $background_color = 'white'; 
    if ($RowCt % 3 == 1) 
     $background_color = 'red'; 
    if ($RowCt % 3 == 2) 
     $background_color = 'yellow'; 

    $Table.="<tr style='background-color:". $background_color .";'>"; 

    foreach($Row as $field => $value) 
     $Table.= "<td style='font-size:14px;' align='center'>$value</td>"; 

    $Table.= "</tr>"; 
} 

... 
+0

그 동안 내가 그것을 점검하고 있었던 것을 얻었다. 그래서 고마워. 그러나, 그것은 단지 전체 행 대신에 그 필드를 칠합니다 (올바른 색으로, 나는 그것을 뒤섞어서 변경합니다). 어쨌든 고마워요 :) – AleksanderKseniya

+0

그리고 당신이 게시 한 질문에 관한 btw. 원래의 질문에 대해서는 분명히 알았지 만, 해당 행의 필드가 실제 시간보다 큰 값일 때 정의 된 색상 (노란색/빨간색/흰색)으로 전체 행을 표시하고자합니다. – AleksanderKseniya

+0

하하 문제 없어요! 코드가 아니라 코드를 다시 작성하면 코드를 다시 작성하면 코드를 단순화 할뿐만 아니라 문제를 해결하는 데 도움이 될 수 있습니다. 원래 코드를 사용하는 것에 익숙하지 않은 경우 특정 세션 변수를 설정하는 동안 다른 세션 변수를 설정 해제 해보십시오. 그렇지 않으면 위의 코드로 행 배경색 전환을 시도하십시오. –