如何在php中使用其他元标记的页面值?

时间:2012-08-01 15:40:53

标签: php facebook-opengraph

我有两个页面正在动态提供内容,第一个index.php包含另一个页面person.php,当我点击链接时:

index.php:

<?php include("db1.php");
define('WP_USE_THEMES', false);
require('blog/wp-blog-header.php');
$pages=array(
                 "person"=>"person.php"


                 ); 

?>
<!DOCTYPE html>
<head>
<title>Untitled Document</title>
<meta property="og:title" content=""/>

    <meta property="og:url" content=""/>
    <meta property="og:site_name" content=""/>
    <meta property="og:description" content=""/>
</head>

<body>
<?
    $sql="select id, name,title,image from persons  where cat =10 ";
    $rs=mysql_query($sql)or die(mysql_error());
    if(list($id,$name,$title,$image)=mysql_fetch_array($rs)){
        ?>
<a href="?page=person&p=<? echo $id;?>" id="<? echo $id;?>" class="details">
<?
    }
    ?>
<div class="twelve columns" id="persons">

        <?
if($_GET["page"]=="")
                    $p="persons1";
                else
                    $p=$_GET["page"];

                if($pages[$p]!="")
                    include($pages[$p]);
                else
                    echo "page not found ";
                    ?>
                  </div>
</body>
</html>

第2页:person.php:

<? include("db1.php");?>
<?php
define('WP_USE_THEMES', false);
require('blog/wp-blog-header.php');
?>
<div class="row">
    <div class="twelve columns">
        <div class="eight columns">

    <?
    if(isset($_GET['p'])){
$id=str_replace('-',' ',(string)$_GET['p']);
$name1="";
    $sql="select id, name,title,details,image,cat from persons  where name ='".$id."' order by name asc";
    $rs=mysql_query($sql)or die(mysql_error());
    if(list($id,$name,$title,$details,$image,$cat)=mysql_fetch_array($rs)){
        $a=$name;
  ?>
    <h3><? echo $name;?></h3>
    <h5><? echo $title;?></h5>
    <p align="justify">
        <img alt="<? echo $name;?>" src="images/persons/<? echo $image?>" title="<? echo $name;?>" style="float:right; margin-left:15px;" />
        <? echo $details;?>
    </p>
    <?
    }
    }
?>

当包含页面时,如何在index.php的元标记中使用person.php页面的值?

1 个答案:

答案 0 :(得分:0)

您可以使用php global关键字声明要使用的变量,以在两个文件中定义变量。有关详细信息,请参阅此处:http://us3.php.net/manual/en/language.variables.scope.php

但是,使用像你这样的通用名称($ name,$ title,$ details,$ image)必然会引起冲突,尤其是在Wordpress环境中。您最好的选择是选择一个唯一的变量名称,将其声明为数组,并将每个项目设置为元素:

 global $amer_enaya_fb_metadata;
 $amer_enaya_fb_metadata = array( 'name' => $name, ... );