2010-12-30 1 views
1

XHTML 마크 업 검증을 깨지 않고 PHP에서 HTML 제목을 변경하는 방법은 무엇입니까?

다음은 웹 사이트의 구조입니다.

PHP 색인 파일

//my class for analyzing the PHP query 
$parameter = new LoadParameters(); 

//what this does is it accesses the database 
//and according to the query, figures out what should be 
//loaded on the page 
//some of the things it sets are: 
// $parameter->design - PHP file which contains the design 
//       HTML code of the page 
// $parameter->content - Different PHP file which should be loaded 
//       inside the design file 
$parameter->mysqlGetInfo($_SERVER['QUERY_STRING']); 

//load the design file 
include($parameter->design); 

PHP 디자인 파일

그냥 일반적인 구조입니다. 분명히 더 많은 디자인 요소를 가지고 있습니다.

<html> 
    <head> 
    ... 
    </head> 
    <body> 
    <?php 
     //this loads the content into the design page 
     include($parameter->content); 
    ?> 
    </body> 
</html> 

질문

그래서 여기에 내가 경험하는 문제가 있습니다. $parameter->content 파일은 동적 인 PHP 파일로서 내용에 따라 쿼리가 변경됩니다.

예를 들어 ?img=1?img=2와 같은 쿼리가 포함 된 이미지 페이지가있는 경우 내 LoadParameter 클래스는 쿼리의 img 부분 만보고 페이지 내용이 image.php가되어야 함을 알게됩니다. image.php 그러나 쿼리를 다시 살펴보고로드 할 이미지를 정확히 파악할 것입니다.

다른 이미지에 대해 다른 <title></title>을 갖고 싶어하므로이 문제가 발생합니다. 그래서 내 솔루션 그냥 콘텐츠 페이지에서 <title></title> 요소를 설정하는 것이었다. 이것은 작동하지만 W3C에서 사이트의 구조가 다음과 같아지기 때문에 XHTML 마크 업 유효성 검사가 중단됩니다.

<html> 
    <head> 
    ... 
    </head> 
    <body> 
    ... 
    <title>sometitle</title> 
    ... 
    </body> 
</html> 

<body></body> 내에서 <title></title>를 갖는 것은 허용되지 않습니다.

그러면 XHTML 마크 업 확인을 위반하지 않고 제목을 어떻게 바꿀 수 있습니까?

참고 : 검색 엔진이 페이지의 제목을 볼 수 없기 때문에 자바 스크립트를 사용할 수 없습니다. PHP에서 직접해야합니다.

고맙습니다.

답변

0

이것은 문제를 해결 한 방법입니다.

내가 좋아하는 뭔가에 PHP 디자인을 변경 :

//get the content PHP file 
//inside the file I set the following variables 
//which are used below: 
//$parameter->title - the string which contains the title 
//$parameter->html - the string which contains the HTML content 
include($parameter->content); 

//string which will contain the html code of the whole page 
$html = <<<EndHere 
<html> 
    <head> 
     <title> 
EndHere; 
//add title 
$html .= $parameter->title; 
$html .= <<<EndHere 
     </title> 
    </head> 
    <body> 
EndHere; 
//add the content of the page 
$html .= $parameter->html; 
$html .= <<<EndHere 
    </body> 
</html> 
EndHere; 

//output the html 
echo $html; 

그리고 여기에 내용 PHP 파일의 기본 구조입니다. 파일을 포함 할 수있는 유일한 페이지가 내 디자인 페이지이기 때문에 $parameter을 참조 할 수 있습니다.

//set the title 
$parameter->title = "sometitle"; 

//set the content HTML 
$parameter->html = "some HTML here"; 

아주 깨끗한 해결책은 아니지만 잘 작동합니다.

0

왜 적절한 장소에서 제목을 수행하려면 두 번째 포함하지 않아도됩니까? 내가 html_head 같은이라는 '공통 기능'에 <head> 모든 코드를 이동할 것

<html> 
    <head> 
    <title><? print($parameter->title); ?></title> 
    </head> 
    <body> 
    <?php 
     //this loads the content into the design page 
     include($parameter->content); 
    ?> 
    </body> 
</html> 
0

은 그냥 PHP 코드를 변경할 수 없습니다 ($ title) 다음에 그것이 속한 제목을 넣으십시오.

다음 페이지에서 해당 기능을 호출하면 문제가 해결됩니다.

해당 기능에 <body> 태그를 포함해야합니다. 그렇지 않으면 작동하지 않습니다.

정교화) $ 파라미터 유효> 내용 $ 파라미터 유효> 내용의 HTML을 표시하지 않고 포함시킬 수 있다면,

+0

제목은 $ parameter-> content에서 결정되므로 $ parameter-> content를 포함하기 전에 제목을 인쇄 할 수 없습니다. – miki725

+0

$ paremeter-> content의 내용은 어떻게 생깁니 까? 그래서 다른 해결책이 있는지 알 수 있습니까? 사이드 노트에서 나는 코드의 디자인이 당신의 필요에 맞지 않는다고 생각한다. 제가 당신이라면 먼저 PHP 코드를 디자인하기 전에 무엇을하고 싶은지 생각해 볼 것입니다. 이것은 개발/유지/스케일링/등의 후속 단계에서 많은 두통을 예방합니다. – PeeHaa

+0

제안 사항이 고가입니다. 실제로이 단어를 만들 수 있도록 구조가 상당히 바뀌 었습니다. $ parameter-> content는 내용의 HTML 코드가 들어있는 PHP 파일입니다. – miki725

0

: 당신이 뭔가를 할 수 있도록

<html> 
    <head> 
    <?php 
     inlcude($parameter->title); 
    ?> 
     ... 
    </head> 
    <body> 
    <?php 
    //this loads the content into the design page 
     include($parameter->content); 
    ?> 
    </body> 
</html> 
+0

자세히 설명해주세요. – miki725

0

그것은 쉬울 것) html_head ("제목"전화에서 다음

function html_head($title) {?> 
    <html> 
     <head> 
      <title><?=$title?></title> 
      <!-- Put whatever you want... here! --> 
     </head> 
     <body> 
<?} 

코드 대신 HTML 코드를 표시하는 $ 매개 변수 -> 표시 (또는 유사한) 함수가 있어야합니다. 그렇게하면 파일의 처음에 PHP 코드를 포함 할 수 있으며 제목에 액세스 할 수 없다는 걱정은 없습니다.

<?php 
    require_once($parameter->content); 
?> 
<!DOCTYPE html> 
<html lang="en" dir="ltr"> 
    <head> 
    <meta charset="UTF-8" /> 
    <title><?php echo $parameter->title; ?></title> 
    </head> 
    <body> 
    <?php 
     echo $parameter->display; 
    ?> 
    </body> 
</html> 
관련 문제