2017-04-15 203 views
-1

我想顯示一張表。我從數據庫中獲取行,並根據這些行想要在表數據中顯示數據。如何合併php和html代碼?

我想顯示一個圖像,將有數據庫來源。

由於我使用ajax,我從ajax調用getPosts並在表中顯示它,所以我從getPosts.php返回數據以顯示在表中。

我試圖做到這一點,但在語法上陷入困境。我想在表格數據中顯示數據時添加條件。

所以這是一個錯誤。

getPosts.php

<?php 

ini_set('display_errors', 1); 
error_reporting(1); 
ini_set('error_reporting', E_ALL); 

include 'Database.php'; 

$database = new Database(Constants::DBHOST,Constants::DBUSER,Constants::DBPASS,Constants::DBNAME); 

$dbConnection = $database->getDB(); 

$stmt = $dbConnection->prepare("SELECT * FROM posts"); 
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$stmt->execute(); 
$results = $stmt->fetchall(PDO::FETCH_ASSOC); 
$posts=''; 

if (count($results > 0)) { 

    $posts.='<table><tr><th>Title</th><th>Description</th><th>Url</th></tr>'; 

    foreach($results as $row) { 

     $posts .= '<tr><td>' . $row['title'] . '</td> <td>' . $row['description'] . '</td>' // getting error here 

       if (strcmp($row['url_type'],"2")) 
        { 
         '<td><a href="'.$row['ur'].'" target="_blank"> 
    <image src="'.$row['thumb_url'].'" height="200" width="200"></image> 
    </a></td>'; 
        } 

        else{ 

     '<td>' . $row['url'] . '</td></tr>'; 

    } 
    } 

} else { 

    $posts.='<tr><td>No data found</td></tr>'; 

} 

$posts.='</table>'; 

echo $posts; 

?> 

HTML:

<!doctype html> 
<html> 

<head> 
    <style> 
     td { 
      text-align: center; 
      border: 1px solid black; 
      border-collapse: collapse; 
     } 
    </style> 
    <meta charset="utf-8"> 
    <title>Posts</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
    <script> 


     $(document).ready(function() { 


      getValue(); 

     }); 
     function getValue() { 

      $.ajax({ 
       type: "POST", 
       url: 'getPosts.php', 
       dataType: 'text', 
       async: false, 
       cache: false, 
       success: function (result) { 

        // alert(result); 

        $('#table').html(result); 
       } 
      }); 
     } 
    </script> 
</head> 

<body> 
<form method="post" enctype="multipart/form-data"> 

    <table id="table" style="width:60%"> 

    </table> 

</form> 
</body> 
</html> 

或者我可以顯示這些崗位在不同的格式和設計?我想顯示說明,喜歡,張貼縮略圖和網址的意見。

也許是這樣的:

<div id="post"> 

<p>Description</p> 
<a href="https://vimeo.com/channels/staffpicks/209597030" target="_blank"> 
<image src="http://i3.ytimg.com/vi/lQMdhS_oOvA/default.jpg" height="200" width="200"></image> 
</a> 
<p>likes</p><p>views</p> 
<p>url</p> 

我想讓它像樣。我是網絡開發的初學者,請和我一起裸照。

任何人都可以幫助,請謝謝你。

+0

'所以這是一個錯誤'什麼錯誤(s)? – OldPadawan

+0

解析錯誤:語法錯誤,意外的'if'(T_IF)在C:\ Program Files(x86)\ Ampps \ www \ AgTv \ getPosts.php 33行 語法錯誤。 @OldPadawan – Sid

+0

您可以打開一個PHP文件作爲您的HTML,並使用_Session來訪問它們之間的數據。我發現要比使用Ajax的POST/GET更容易 –

回答

1

您需要關閉線,然後進行了if/else語句並連接在它:

<?php 

$posts .= '<tr><td>'.$row['title'].'</td> <td>'.$row['description'].'</td>'; /* close here*/ 

    if (strcmp($row['url_type'],"2")) 
     { 
     $posts .= '<td><a href="'.$row['ur'].'" target="_blank"> 
<image src="'.$row['thumb_url'].'" height="200" width="200"></image></a></td>'; 
      } 
      else 
      { 
     $posts .= '<td>' . $row['url'] . '</td></tr>'; 
      } 

?> 
+0

你好你在嗎? – Sid

+0

@Sid:有問題嗎? – OldPadawan

+0

是的,我想連接一個鏈接,但它不工作。 Sid

0

echo phtml文件中的Html標籤。和所有。你必須把HTML標籤回聲

2

我希望下面的代碼解決您的問題:

<?php 

ini_set('display_errors', 1); 
error_reporting(1); 
ini_set('error_reporting', E_ALL); 
include 'Database.php'; 

$database = new Database(Constants::DBHOST,Constants::DBUSER,Constants::DBPASS,Constants::DBNAME); 

$dbConnection = $database->getDB(); 

$stmt = $dbConnection->prepare("SELECT * FROM posts"); 
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$stmt->execute(); 
$results = $stmt->fetchall(PDO::FETCH_ASSOC); 
$posts=''; 

if (count($results > 0)) { 

    $posts.='<table><tr><th>Title</th><th>Description</th><th>Url</th></tr>'; 
    foreach($results as $row) { 
     $posts .= '<tr><td>' . $row['title'] . '</td> <td>' . $row['description'] . '</td>'; // <- You need to close your $posts variable here. 

       if (strcmp($row['url_type'],"2")) 
       { 
       $posts .= '<td><a href="'.$row['ur'].'" target="_blank"><image src="'.$row['thumb_url'].'" height="200" width="200"></image></a></td></tr>'; // You need to store this result in your post variable like this. 
       } 

       else{ 

       $posts .= '<td>' . $row['url'] . '</td></tr>'; // You need to store this result in your $posts variable as well. Because both if and else are give an different output. So if you want to display output which return by if and else then you must have to store output in $posts variable. 
      } 
    } 

} else { 
    $posts.='<tr><td>No data found</td></tr>'; 

} 
?>