首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从现场首部数据库中提取数据

从现场首部数据库中提取数据
EN

Stack Overflow用户
提问于 2015-10-06 06:17:16
回答 3查看 85关注 0票数 1

也许这是一个愚蠢的问题,但是网站的结构是index.phphead.phpfooter.php,所以我在索引页面中包含了head.phpfooter.php。所有的头像都在head.php

现在我想在网站上添加Facebook共享按钮,所以我在head.php中添加了

代码语言:javascript
运行
复制
<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>部件的下面。如果我把

代码语言:javascript
运行
复制
<meta property="og:image" content="http://website.com/image/'.$row['image'].'"/>

它不起作用,因为此时还没有$row['image']

更新

好的,这就是我基于@RickJames答案所做的。我创建了init.php文件,并将其放入其中

代码语言:javascript
运行
复制
<?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();
 ?>

在我需要的每一页中,我都把它包括在页面的顶部

代码语言:javascript
运行
复制
<?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这个解决方案可以接受吗?因为它现在起作用了?我有什么可以改进的吗?还是应该这样做呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-06 06:23:26

创建一个init.php,在所有其他PHP页面之前加载,即在head.php之前加载

在这个页面中,包括您对页面的所有数据库查询,然后根据您的需要在后面的部分中使用它们,例如head.phpindex.phpfooter.php,等。

init.php如下:

代码语言:javascript
运行
复制
<!--- **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如下:

代码语言:javascript
运行
复制
<!--- 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如下:

代码语言:javascript
运行
复制
<!--- START **index.php:**  --->
<HTML>
  include 'init.php';
  include 'head.php';
  <BODY>
  ..... blah, blah, blah
  </BODY>
  include 'footer.php';    
</HTML>
票数 3
EN

Stack Overflow用户

发布于 2015-10-06 06:31:19

在使用header.php的所有文件中创建一个函数。

header.php

代码语言:javascript
运行
复制
<meta property="og:image" content="<?php echo meta_og_image(); ?>"/>

index.php

代码语言:javascript
运行
复制
function meta_og_image() {
    //Write your query here, and get only one field like SELECT image FROM table_name
    return $row['image'];
}
票数 1
EN

Stack Overflow用户

发布于 2015-10-06 06:34:09

这不是最好的解决方案,但将使用Jquery。我还没有测试过,但我认为应该没问题。

在页面末尾的index.php pref中使用它,或者使用适当的语义,如isset()等。

代码语言:javascript
运行
复制
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

        });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32962978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档