【问题标题】:several parameters in the URLURL中的几个参数
【发布时间】:2021-08-05 12:35:44
【问题描述】:

我正在尝试在类似 MVC 的架构中(每个页面都有一个 url“index.php?action=nameofthepage”),通过 while 循环显示与前一页中已经显示的每个项目相对应的个人页面( 1):
查看第 1 页:

 while ($data=$eleves->fetch()) {?>  
            <div class="enseignant">
                <a href="index.php?action=portraitDanciensIndividuel&amp;id=<?=$data['id']?>"> //  this doesn't work
                    <img src="<?php echo $data['photo']?>" alt="" class="photoEnseignant">
                    <h2 style="margin-left:auto;margin-right:auto;"><?= $data['prenom'] .' '. $data['nom']?></h2>
                </a>
            </div>
            <?php } ?>

查看第 2 页:

    <?php
    $data=$stmt->fetch() ?>
        <img src="<?= $data['photo']?>" alt=""class="AfficherEleveIndividuelPhoto">
        <h3 ><?=$data['prenom'] .' '. $data['nom']?></h3>
        <p class="seCultiverTxt">
            <?=nl2br($data['description'])?>
        </p>

模型页面 2:

<?php
//PDO etc.
$stmt = $bdd->prepare('SELECT * FROM eleves WHERE id=?');
$stmt->execute(array($_GET['id']));

控制器页面 2

function portraitIndividuel() {
    require ('dbConnect.php');
    require ('models/PortraitsDanciensIndividual.php');
    require ('portraitDancienIndividuelV.php');
}

路由器页面 2

if ($_GET['action'] == 'portraitDanciensIndividuel') {
            portraitIndividuel();
        }

这是我的问题:我想知道如何使用 action 参数并包括 id 参数进入另一个页面?

我是 php 新手!
我已经尝试了很多不同的东西 欢迎任何线索

【问题讨论】:

    标签: php sql model-view-controller


    【解决方案1】:

    您可以使用“&”组合参数。因此,在您的示例中,您将在链接上更新到以下内容

      <a href="index.php?action=portraitDanciensIndividuel&id=<?php echo $data['id']; ?>">
    

    然后,当您在页面上处理该信息时,如下所示:

    if( isset( $_GET['id'] ) && $_GET['id'] !== '' ){
     $id = $_GET['id'];
    }
    
    //or same return in shorthand
    $id = isset( $_GET['id'] ) && $_GET['id'] !== '' ? $_GET['id'] : 0;
    

    永远不要相信来自 url 参数的任何信息,始终仔细检查正确的数据;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-23
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多