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

时间:2016-11-23 22:41:46

标签: php codeigniter

所以我正在创建一个简单的论坛,用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 个答案:

答案 0 :(得分:0)

对于初学者来说,你需要重构你生成链接的尝试,因为每个人都指出了这个问题。 所以我冒昧地想出一些测试代码来展示一些东西。

所以这是第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事件那就完全是基本的..这会触发一个AJAX调用,它将页面id发布到我们返回HTML的方法我们想要显示的结果......