【问题标题】:How could I load a page with parameter?如何加载带有参数的页面?
【发布时间】:2021-09-01 17:19:30
【问题描述】:

我正在开发一个博客项目(学习课程),其中显示了相关的火车和 cmets 的账单。它使用 GET 参数。

我做了教授要求的所有改进,除了我必须在 url 上加载带有参数的博客。下面有一个屏幕截图,它显示未定义的索引,这很好,但我不知道如何使用参数加载它:

Cpt1:

我尝试使用 include,但我没有其他文件要加载我在同一页面中进行了所有改进,并且也使用了空的 fonction(),但是在我不知道如何制作之后(我是初学者)。

我不是在问答案,但是如果您有任何提示可以帮助处理,或者如果它太复杂,请给我一个解释! (对不起,如果我的英语不好,我不是本地人)

代码:

<?php 
include('connexion.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/style.css">
    <title>Blog</title>
</head>
<body>
    <h1>Mon super Blog !</h1>
    <p>Derniers billets du blog : </p>



    <!-- NEWS -->
    <div class="news">
        
        
        <?php
        
        $recuperation_titre_du_billets_et_contenu = $bdd->query("SELECT Id,Titre,Contenu,DATE_FORMAT(Date_creation,'%d/%m/%Y à %H:%i') AS date_affiché FROM billets LIMIT 0,4");
        while($données = $recuperation_titre_du_billets_et_contenu->fetch())
        {
            echo "<h3>".htmlspecialchars($données['Titre'])." le ".htmlspecialchars($données['date_affiché'])."</h3>";
            
            ?>
            <p>
                <?php echo htmlspecialchars($données['Contenu']);
            ?>
            
            <a href="commentaires.php?Id_billet=<? echo $données['Id']?>&amp;page_commentaire=1">Commentaires</a>
            
        </p>
        
        <?php 
        }
        $recuperation_titre_du_billets_et_contenu->closeCursor();
        ?>

</div>
<!-- FIN NEWS -->

<!-- NEWS2 -->

<div class="news2">
    
    <?php
                    
                    $billetsdelapage2 = $bdd->prepare("SELECT Id,Titre,Contenu,DATE_FORMAT(Date_creation,'%d/%m/%Y à %H:%i') AS date_affiché FROM billets LIMIT 4,4");
                    $billetsdelapage2->execute(array($_GET['page']));
                    
                    if(isset($_GET['page']) == 2)
                    {
                        while($data = $billetsdelapage2->fetch())
                        {
                            echo "<h3>".htmlspecialchars($data['Titre'])." le ".htmlspecialchars($data['date_affiché'])."</h3>";
                            ?>
                                      <p>
                                          <?php echo htmlspecialchars($data['Contenu']);
                                                ?>
                                                <a href="commentaires.php?Id_billet=<? echo $data['Id'];?>">Commentaires</a>
                                                <?php
                        }
                        ?>

<style>    
                                                    .news
                                                    {
                                                        display:none;
                                                    }
                                                    </style>


</p>
<?
                                    }
                                    $billetsdelapage2->closeCursor();       
                                    ?>
                                    
                                </div>
                                <!-- FIN NEWS2 -->  

                                <!-- NEWS3 -->
                                <div class="news3">
                                    
                                    <?php
                    
                    $billetsdelapage3 = $bdd->prepare("SELECT Id,Titre,Contenu,DATE_FORMAT(Date_creation,'%d/%m/%Y à %H:%i') AS date_affiché FROM billets LIMIT 8,12");
                    $billetsdelapage3->execute(array($_GET['page']));
                    
                    if(isset($_GET['page']) AND ($_GET['page'] != 2 ))
                    {
                        while($data = $billetsdelapage3->fetch())
                        {
                            echo "<h3>".htmlspecialchars($data['Titre'])." le ".htmlspecialchars($data['date_affiché'])."</h3>";
                            ?>
                                      <p>
                                          <?php echo htmlspecialchars($data['Contenu']);
                        ?>
                        <a href="commentaires.php?Id_billet=<? echo $data['Id']?>">Commentaires</a>
                        <?
                    }
                    ?>
                                                    <style>    
                                                    .news
                                                    {
                                                        display:none;
                                                    }
                                                    
                                                    .news2{
                                                        display:none;
                                                    }
                                                    </style>

</p>
<?
                                    }
                                    $billetsdelapage3->closeCursor();       
                                    ?>
                                    
                                </div>
                                
                                
                                <!-- FIN NEWS3 -->
                                
                                
                                
                                
                            </div>
                            
                            <div class="pagination">
                                <!-- Petite erreur qui sera rectifié plus tard vis à vis des numéros de page  ! -->
                                <a href="index.php">1</a>
                                <a href="index.php?page=2">2</a>
                                <a href="index.php?page=3">3</a>
    
</div>

</body>
</html>

【问题讨论】:

  • 请出示代码
  • 我把代码放在上面了。

标签: php mysql url get


【解决方案1】:

如果您想要从 URL 中获取参数,例如:

HTTP://myurl.com/index.php?myParameter=10

然后使用这样的东西应该可以工作。

if (isset($_GET['page'])){ //check to see if 'page' is set.
$pageNo= $_GET['page']; //then set a variable equal to the parameter.
}

【讨论】:

  • 感谢您的回答。在我的网址上写着:localhost/PHP/Blog,但我需要的是当页面加载时:localhost/PHP/Blog/index.php?page=2我不知道我是否足够清楚还是应该改写它?
  • 您能说得更具体一点吗?从您的代码来看,您似乎已经创建了应该将您带到正确 URL 的链接,然后您将使用上面的代码获取页码,然后您可以从那里做您想做的事情。
  • 你说得对,事实是我迷路了......我理解指令就像我有其他页面(page1.php,page2.php,等等......)然后我可以使用包含(index.php)加载默认页面,然后我可以使用下面的链接浏览其他页面,我的问题将得到解决。我只是使用 GET 参数完成了没有它们的项目。
  • 最后我对这个指令如此固执,以至于我把自己弄糊涂了。现在很清楚了,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 2019-10-28
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多