也许这是一个愚蠢的问题,但是网站的结构是index.php,head.php,footer.php,所以我在索引页面中包含了head.php和footer.php。所有的头像都在head.php里
现在我想在网站上添加Facebook共享按钮,所以我在head.php中添加了
<meta property="og:image:width" content="404">
<meta property="og:image:height" content="404">
<meta property="og:locale" content="bg_BG" />
<meta property="og:type" content="website" />
<meta property="og:title" content="Title of site" />
<meta property="fb:app_id" content="" />
<meta property="og:url" content="URL of the content" />
<meta property="og:image" content="IMAGE HERE" />
<meta property="og:description" content="Description" />
<meta property="og:site_name" content="name" />我的问题是如何使用<meta property="og:image" content="IMAGE HERE" />,因为每个页面上都有不同的图片,用户可以共享。
我有从数据库中检索图像的代码,但是在索引页中,即<head></head>部件的下面。如果我把
<meta property="og:image" content="http://website.com/image/'.$row['image'].'"/>它不起作用,因为此时还没有$row['image']。
更新
好的,这就是我基于@RickJames答案所做的。我创建了init.php文件,并将其放入其中
<?php
$pdo = Database::connect();
if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])){
$image_id = $_GET['image_id'];
$result = $pdo->prepare("SELECT image_name from images WHERE image_id= :image_id ");
$result->bindParam(':image_id', $image_id, PDO::PARAM_INT);
if ($result->execute())
{
$row_name = $result->fetch();
}
}
Database::disconnect();
?>在我需要的每一页中,我都把它包括在页面的顶部
<?php
include 'misc/database.inc.php';
include 'misc/init.php';
include 'misc/head.php';
?>
// rest of the page头部:<meta property="og:image" content="<?php echo $row_name['image_name']; ?>"/>
这样,我就不会改变页面结构的其余部分。我将遵循@Rick的建议,在这个文件中移出所有的查询,但现在将像这样工作。
@RickJames这个解决方案可以接受吗?因为它现在起作用了?我有什么可以改进的吗?还是应该这样做呢?
发布于 2015-10-06 06:23:26
创建一个init.php,在所有其他PHP页面之前加载,即在head.php之前加载
在这个页面中,包括您对页面的所有数据库查询,然后根据您的需要在后面的部分中使用它们,例如head.php,index.php,footer.php,等。
。
init.php如下:
<!--- **init.php:** --->
<?php //**init.php:**
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli(
$servername,
$username,
$password,
$dbname
);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT image FROM table_name WHERE image_id = " .$_GET["image_id"]
$result1 = $conn->query($sql);
if ($result1->num_rows > 0) {
// output data of first row/image only
$row1 = $result->fetch_assoc());
}
else { echo "result1 has 0 results"; }
$sql2 = "SELECT first_name FROM employees";
$result2 = $conn->query($sql); //use result2 later in code
//with a loop and $row2
if ($result2->num_rows <= 0) {
echo "result2 has 0 results";
}
$conn->close();
?>。
head.php如下:
<!--- START **head.php:** --->
<head>
<meta property="og:image:width" content="404">
<meta property="og:image:height" content="404">
<meta property="og:locale" content="bg_BG" />
<meta property="og:type" content="website" />
<meta property="og:title" content="Title of site" />
<meta property="fb:app_id" content="" />
<meta property="og:url" content="URL of the content" />
<meta property="og:image" content="http://website.com/image/<?php echo $row['image']; ?>"/>
<meta property="og:description" content="Description" />
<meta property="og:site_name" content="name" />
</head>
<!--- END **head.php:** ---> 。
index.php如下:
<!--- START **index.php:** --->
<HTML>
include 'init.php';
include 'head.php';
<BODY>
..... blah, blah, blah
</BODY>
include 'footer.php';
</HTML>发布于 2015-10-06 06:31:19
在使用header.php的所有文件中创建一个函数。
header.php
<meta property="og:image" content="<?php echo meta_og_image(); ?>"/>index.php
function meta_og_image() {
//Write your query here, and get only one field like SELECT image FROM table_name
return $row['image'];
}发布于 2015-10-06 06:34:09
这不是最好的解决方案,但将使用Jquery。我还没有测试过,但我认为应该没问题。
在页面末尾的index.php pref中使用它,或者使用适当的语义,如isset()等。
jQuery(document).ready(function () {
jQuery("meta[property='og\\:image']").attr("content", <?php echo $row['image'];?>);// Note you can place any variable at the place of $row['image'] and it will be applied to the image
});https://stackoverflow.com/questions/32962978
复制相似问题