首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >个性化内容

个性化内容
EN

Stack Overflow用户
提问于 2018-10-28 13:01:37
回答 2查看 42关注 0票数 0

我想,点击一个链接,显示一个个性化的内容。

代码语言:javascript
复制
<div class="tab_event">             
<?php
do{
    echo'                   


    <table class="preview_event">
        <td class="date_event">
            '.$row["Date"].'
        </td>
        <td class="title_event">
            <p>'.$row["Title"].'<br></p>
            <a href="">Read more !</a>
        </td>

    </table>


';
} while ($row = $query->fetch(PDO::FETCH_ASSOC));

?>

我的程序包括对数据库中的一些文章进行“预览”。当你点击“阅读更多”,我想,自动,你会得到一个页面,其中所有的文章是写的。但我对如何做到这一点没有任何想法,bcs我不想为每一篇文章手工创建一个页面。有人知道怎么做吗?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-28 13:30:24

要实现您想要的结果,您需要为另一个/相同的PHP支持的页面生成一个href,以及一个具有唯一标识数据库中文章的值的查询参数。

假设您的数据库表结构类似于

代码语言:javascript
复制
| Id | Date       | Title                  |
|----|------------|------------------------|
| 1  | 0000-00-00 | Some catchy post title |
| 2  | 0000-00-00 | Yet another post title |
| 3  | 0000-00-00 | Some other title?      |

我将使用上面的表结构使用$row["Id"]

代码语言:javascript
复制
   <div class="tab_event">             
    <?php
      do {
        echo '                   
        <table class="preview_event">
            <td class="date_event">
                '.$row["Date"].'
            </td>
            <td class="title_event">
                <p>'.$row["Title"].'<br></p>
                <a href="/showArticle.php?aid='.$row["Id"].'">Read more !</a>
            </td>

        </table>';
      } while ($row = $query->fetch(PDO::FETCH_ASSOC));
    ?>

现在访问链接脚本( aid )上的showArticle.php参数,从数据库中获取文章

代码语言:javascript
复制
<?php
   if (!empty($_GET['aid']) {
      // escape and verify value of `$_GET['aid']`
      // fetch and return article from database using the `$_GET['aid']` parameter
   } else {
      // tell the user no `Id` was provided
   }
?>
票数 1
EN

Stack Overflow用户

发布于 2018-10-28 13:52:36

这是一个非常简单的任务,首先,您需要修改您的表页,并编辑read链接如下:

<a href="read.php?article=.$row['article_id'].">Read more !</a>

当您单击链接时,article_id是用来存储该信息的任何记录标识,例如,在数据库中创建文章的id字段,现在创建一个read.php页面,它将根据链接中的文章的id更改其内容,因此您只需要为每一篇文章创建一个页面就可以了。

然后在read.php中,从URL中检索该信息,以便可以使用正确的文章ID查询数据库,并显示其内容:

代码语言:javascript
复制
...
...
$article_id = $_GET['article'];

// then you query the database using that variable (using whatever method you prefer, this in just an example)

$query = $db->prepare("SELECT title, date, content FROM articles WHERE articles.id = " . $article_id);

$query->fetch(PDO::FETCH_ASSOC);

通过这种方式,您可以检索单个文章的信息,并在read.php页面上显示它们,只创建一个页面,该页面根据单击的链接更改其内容。

另外,值得一提的是,您应该删除do...whileforforeach循环,这些循环要优雅得多。

一个例子是(假设包含结果的变量称为$rows):

代码语言:javascript
复制
<table> <!-- table tag outside of the loop, otherwise for each article a table is created, which is wrong -->
 <tbody>
  <?php

  foreach($rows as $article) {
   echo "<tr>"; // since we need one row for each article, we include the tr tag in the loop
   echo "<td>".$article['title']."</td>";
   echo "<td>".$article['date']."</td>";
   echo "<td>".$article['author']."</td>";
   ...
   ...
   echo "</tr>"; // and we close the row
  }

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

https://stackoverflow.com/questions/53031805

复制
相关文章

相似问题

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