簡體   English   中英

從站點頂部的數據庫中獲取數據

[英]Taking data from database in head section of site

可能這是一個愚蠢的問題,但是網站的結構是index.phphead.phpfooter.php所以我在索引頁面中包含了head.phpfooter.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是可以接受的解決方案,因為它現在正在工作? 有什么我可以改善的,還是應該讓它像這樣?

創建一個init.php在所有其他PHP頁面之前加載,即甚至在head.php之前head.php

在此頁面中,包括該頁面的所有數據庫查詢 ,然后在以后的部分中根據需要使用它們,例如head.phpindex.phpfooter.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>

在使用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'];
}

不是最好的解決方案,但可以與Jquery一起使用。 我還沒有測試過,但我認為應該沒問題。

在頁面末尾的index.php使用它,或使用適當的語義,即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

        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM