PHP使用数据库制作论坛,无法使链接正常工作



所以我正在做一个简单的论坛,以使用Codeigniter学习一些PHP,简单地说,我的意思是带有帖子的1页,您可以单击它们以评论并查看更多信息(想想Reddit)。所有数据都存储在MySQL数据库中。无论如何,我在页面上显示了所有要显示的链接,但是我无法确定的是如何打开一个新页面以显示帖子的描述和评论。我记得很久以前做过类似的事情,不记得我是怎么做到的。

<?php
   foreach($records as $rec){
$test = $rec->PostName."<br/>";  
Echo "<a href=#$test>$test</a>";
   }        
   ?>
    <?php
      echo '<div data-role="page" id="$test"></div>';
   echo "THIS ISSSSS $test";   
   ?>

所以这是我需要帮助的部分。任何建议都非常感谢

对于初学者来说,您需要重构尝试生成链接的尝试,因为每个人都指出的是问题。因此,我借此自由提出了一些测试代码来演示一些事情。

所以这是第1部分。

<?php
// Simulated Database results, an array of objects
//
// These may already be Slugs with First letter upper case, hyphenated between words
// But I will do that when generating the links.
$records = [
    (object)['PostName'=>'Why I recommend reading more PHP Tutorials'],
    (object)['PostName'=>'Why I should take heed of what others suggest and actually try them out before dismissing them'],
    (object)['PostName'=>'What is the difference between using single and double quotes in strings'],
    (object)['PostName'=>'Why everyone should know how to use their browsers View Source tool to inspect the generated HTML'],
];
foreach ( $records as $rec ) {
    $test = $rec->PostName;
    // This would make a good helper but Codeigniter might have something already.
    // So you should go and read the manual.
    echo "<a href="#".ucfirst(str_replace(' ','-',$test))."">$test</a>";
    // The above uses " to wrap the string with escaped " within the string
    // cause you cant have both.
    echo '<br>';
}
// This is only here for testing... I think.
echo '<div data-role="page" id="'.$test.'"></div>';
// The above uses single quotes to wrap the string.
echo "THIS ISSSSS $test";

部分第2部分...

<?php
// Simulated Database results, an array of objects
//
//
// These may already be Slugs with First letter upper case, hyphenated between words
// But I will do that when generating the links.
$records = [
    (object)[
        'id'=>1,
        'PostName'=>'Why I recommend reading more PHP Tutorials'],
    (object)[
        'id'=>2,
        'PostName'=>'What is the difference between using single and double quotes in strings'],
];
foreach ( $records as $rec ) {
    $name = $rec->PostName;
    $id = $rec->id;
    // The slug of the page isn't really being used here as
    // we are providing the pages id for lookup via an AJAX Call.
    echo "<a id=".$id." href="#".ucfirst(str_replace(' ','-',$name))."">$name</a>";
    echo '<br>';
}
// This is where the AJAX call fired by a click on an anchor, that sends the id
// which results in the fetching of the required details to plonk in our page-area div.
echo '<div class="page-area"></div>';

有多种方法可以做到这一点,但是如果我们创建JS事件以单击锚点。我们要显示的结果...

最新更新