我想给您看一张桌子。我正在从数据库中获取行,并根据这一点,我希望在表数据中显示数据。
我想要显示一个图像,这将有来自数据库的来源。
当我使用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>
我想让它看起来像模像样。
发布于 2017-04-15 14:32:43
您需要关闭该行,然后继续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>';
}
?>
发布于 2017-04-15 14:28:38
我希望下面的代码能解决你的问题:
<?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>';
}
?>
https://stackoverflow.com/questions/43422861
复制相似问题