2011-04-12 6 views
0

이 코드를 사용하고 대사관 휴일 : 데이터베이스에서 대사관의 개방 시간을 소요하고 대사관은 현재 열려 있는지 여부를 결정 PHP 개방 시간 및

$rawsql = "SELECT 
* 
FROM 
    _erc_foffices n 
INNER JOIN 
    _erc_openings o ON n.id = o.branch_id AND o.dotw = DAYOFWEEK(CURRENT_DATE()) 
INNER JOIN 
    _erc_openings_times t ON o.id = t.opening_id 
WHERE 
(
    UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close)) 
) 
AND 
(
    n.id = %d 
) 
;"; 

$sql = sprintf($rawsql, mysql_real_escape_string($id)); 

$result = mysql_query($sql); 

/*These if & while statements decide whether or not the information should be displayed. If no results are returned from the query above then an alternative message is shown.*/ 

if(mysql_num_rows($result) > 0) { 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 

     echo "<div class='address'><p>" . $row["title"] . "<br/>"; 
     echo $row["address_1"] . "<br/> " . $row["address_2"] . "<br/> " . $row["address_3"] . "<br/> " . $row["address_4"] . "<br/> " . $row["address_5"] . "</p>"; 
     echo "<div class='buttons'><img src='http://localhost/erc/images/texttouser_button.png'/><br/><a href='" . $row["url"] . "' target='blank'><img src='http://localhost/erc/images/website_button.png'/></a></div></div>"; 
     echo "<div class='email'>" . $row["email"] . "</div>"; 
     $extra_notes = $row["extra"]; 

    } 
} else { 

     $embassy_closed = mysql_query("SELECT * FROM _erc_foffices WHERE id = '$embassy_id'"); 

     while($row = mysql_fetch_array($embassy_closed)) 
         { 

     echo "<div class='address'><p>" . $row["title"] . "<br/>"; 
     echo $row["address_1"] . "<br/> " . $row["address_2"] . "<br/> " . $row["address_3"] . "<br/> " . $row["address_4"] . "<br/> " . $row["address_5"] . " <font color='red'>The embassy is closed.</font></p>"; 
     echo "<div class='buttons'><img src='http://localhost/erc/images/texttouser_button.png'/><br/><a href='" . $row["url"] . "' target='blank'><img src='http://localhost/erc/images/website_button.png'/></a></div></div>"; 
     echo "<div class='email'>" . $row["email"] . "</div>"; 
     $extra_notes = $row["extra"]; 


         } 


} 

. 그렇지 않은 경우 '대사관이 폐쇄되었습니다'라는 메시지가 표시됩니다. 지금은 공휴일에 추가해야합니다, 그래서 다음과 같이 할 수있는 쿼리를 변경 한 :

$rawsql = "SELECT 
    * 
    FROM 
     _erc_foffices n 
    INNER JOIN 
     _erc_openings o ON n.id = o.branch_id AND o.dotw = DAYOFWEEK(CURRENT_DATE()) 
    INNER JOIN 
     _erc_openings_times t ON o.id = t.opening_id 
     LEFT JOIN 
    _erc_holidays h ON h.branch_id = n.id 
    WHERE 
    (
     UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close)) 
    ) 
    AND 
    (
     n.id = %d 
    ) 
     AND 
(
    UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) 
    NOT BETWEEN UNIX_TIMESTAMP(h.begins_at) AND UNIX_TIMESTAMP(h.ends_at) 
) 
     ;"; 

을하지만 이것은 단지 두 번 주소/이메일 등을 출력합니다.

누군가 내가 잘못하고있는 것을 지적 할 수 있습니까? 어떤 도움

편집에 대한

감사합니다 : 난 그냥 2 쿼리를 사용하는 경우/다른 루프, 지금이 문제를 해결했습니다.

답변

0

두 번째 쿼리와 if/else 루프를 사용하여 원하는대로 작동합니다.

0

나는 전문가가 아니며 귀하의 조인은 매우 복잡해 보일 것이라고 공언하지는 않겠지 만, 내 경험에 의하면 귀하의 쿼리에 중복이 생기면 올바르게 그룹화되지 않았으며 GROUP BY를 볼 수 없습니다. 귀하의 모든 질문에.

희망이 있습니다.

그런데 ... 어디에서 공휴일 데이터를 얻었습니까? 나는이 포스트를 가로 질러 온 바로 그 순간에 똑같은 것을 찾고있다.

+0

안녕하세요, 저희 직원이 대사관 웹 사이트를 직접 방문하여 데이터를 수동으로 수집하고 있습니다. –