首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何合并php和html代码?

如何合并php和html代码?
EN

Stack Overflow用户
提问于 2017-04-15 14:12:37
回答 2查看 357关注 0票数 -1

我想给您看一张桌子。我正在从数据库中获取行,并根据这一点,我希望在表数据中显示数据。

我想要显示一个图像,这将有来自数据库的来源。

当我使用ajax时,我从ajax调用getPosts并将其显示在一个表中,所以我从getPosts.php返回数据以显示在一个表中。

我曾尝试过这样做,但在语法上卡住了。我想在表数据中显示数据时添加条件。

所以它给出了一个错误。

getPosts.php

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

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

或者我可以用不同的格式或设计来展示这些帖子?我想要显示的描述,喜欢,后一个缩略图和网址视图。

可能是这样的:

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

我想让它看起来像模像样。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-15 14:32:43

您需要关闭该行,然后继续if/else语句并在其中连接:

代码语言:javascript
复制
<?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>';
          }

?>
票数 1
EN

Stack Overflow用户

发布于 2017-04-15 14:28:38

我希望下面的代码能解决你的问题:

代码语言:javascript
复制
<?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>';

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

https://stackoverflow.com/questions/43422861

复制
相关文章

相似问题

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