【问题标题】:Open seperate page for fetched data through PHP after click点击后通过PHP打开单独的页面以获取数据
【发布时间】:2016-07-20 22:20:23
【问题描述】:

我正在尝试制作一个问答网页。我通过 PHP 从数据库中获取数据,如下所示:

现在,我怎样才能让每个数据都可以点击,以便在点击特定问题后,它会打开一个模态或一个新页面,点击问题的数据所在的位置。

我的代码是:

   <?php     
 //Start to show '...' after 200 words. 
         function limited_echo($x, $length){
            if(strlen($x)<=$length){
            echo $x;}   
            else{
            $y=substr($x,0,$length) . '...';
            echo $y;}}
 //End Here
 //Main Code
    include_once 'inc/connection.php';
    $record= "SELECT * FROM questions";  
    $getdata = mysqli_query($link,$record);
    if(! $getdata ) {
      die('Could not get data');
    }
    while($row = mysqli_fetch_array($getdata)){
    ?>
   <div class="section">  
         <h1><?php echo "{$row['title']}"; ?></h1>
         <p><?php limited_echo($row['description'], 200); ?></p>
   </div>
<?php
    }
 ?>

希望,这些信息就足够了。如果需要任何额外的东西,请告诉我。

【问题讨论】:

    标签: php web mysqli


    【解决方案1】:

    Since HTML5 您可以使用&lt;a&gt; 使&lt;div&gt; 标记可链接。

    <a href="[url here]">
        <div class="section">  
            <h1><?php echo "{$row['title']}"; ?></h1>
            <p><?php limited_echo($row['description'], 200); ?></p>
        </div>
    </a>
    

    对于它链接到的 URL,拥有每个帖子链接到的 ID 会很有用,并且在该页面上使用该 ID 您需要在数据库中查找完整的帖子信息。

    www.example.com/post.php?id=5 //post with the id 5

    然后您将使用$_GET['post'] 变量在数据库中查找帖子。 (警告:确保在查找时转义此变量,否则您 may end up with an SQL injection attack。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 2015-07-11
      • 2023-02-05
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多