我想要一张桌子。我从数据库获取行,并根据我想在表数据中显示数据。
我想显示一个包含数据库来源的图片。
当我使用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 :(得分: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>';
}
?>
答案 1 :(得分: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>';
}
?>
答案 2 :(得分:0)
在Php文件中回显Html标签。如回声;和所有。你必须将html标签放在echo
中