【问题标题】:If and else statement in PHP inside of an echo [duplicate]回声中的PHP中的if和else语句[重复]
【发布时间】:2016-09-11 17:12:18
【问题描述】:

我想让管理员能够激活用户帐户,因此我必须将状态设置为活动和非活动 当我运行我的代码时,我得到 4 个状态 积极的 不活跃 积极的 不活跃 你会在这里找到一个截图来理解我面临的问题

http://i.imgur.com/2c0VcN7.png

    if(isset($_GET['id_user'])){

        include("conexion.php");

        $rep=$db->query("select * from user WHERE id_user=" .$_GET['id_user']  );

        while($l=$rep->fetch()){
echo "

<form class= mainSettingsForm add method='POST'>


    <label>Numero utlisateur :</label>
    <input disabled='disabled' type='text' name='ref' value=' ".$l[0]." ' ></input> 
    <input name='id_user2' type='hidden' value='".$l[0]."' ></input> 

    <label>Nom utlisateur :  </label>    
    <input type='text' name='username2' value='".$l[1]."' > 

    <label> nom :    </label>   
    <input type='text' name='nom2' value='".$l[3]."' > 

    <label> prenom :      </label>
    <input type='text' name='prenom2' value='".$l[4]."' > 

    <label> Statut :  </label>    
    <select name=statut >


      if ($l[6] ==active) {
      echo '
      <option value=active >active</option>
      <option value=inactive >inactive</option>  }'

      else {
      echo '
      <option value=inactive >inactive</option>      
      <option value=active >active</option> }'

        </select>



   <input class=btn-primary type='submit' name='enregistrer' value='enregistrer' > 

    </form>"; // fin echo
    }   
    }
?> 

【问题讨论】:

标签: php html


【解决方案1】:

您的代码可能格式不正确。试试这个:

    <?php

        if( isset($_GET['id_user']) ){              
            include("conexion.php");                
            $rep = $db->query( "select * from user WHERE id_user=" . $_GET['id_user']  );

            // YOU ARE FETCHING A SINGLE USER... NO NEED FOR A LOOP...
            // JUST GET THE ARRAY OBJECT AND USE IT DIRECTLY...
            $l  = $rep->fetch();
    ?>

    <form class= mainSettingsForm add method='POST'>            
        <label>Numero utlisateur :</label>
        <input disabled='disabled' type='text' name='ref' value='<?php echo $l[0]; ?>'/>
        <input  name='id_user2' type='hidden' value='<?php echo $l[0]; ?>'/>

        <label>Nom utlisateur :</label>
        <input type='text' name='username2' value='<?php echo $l[1]; ?>' />

        <label>Nom :</label>
        <input type='text' name='nom2' value='<?php echo $l[3]; ?>' />

        <label>Prenom :</label>
        <input type='text' name='prenom2' value='<?php echo $l[4]; ?>' />

        <label>Statut :</label>         
        <select name=statut >               
            <option value='active' <?php if($l[6] == "active"){ echo "selected";} ?> >active</option>
            <option value='inactive' <?php if($l[6] == "inactive"){ echo "selected";} ?>inactive</option>
        </select>           

        <input class=btn-primary type='submit' name='enregistrer' value='enregistrer' >     
    </form>

    <?php } ?>

【讨论】:

  • 感谢您的回答,但即使用户处于非活动状态,我仍然会优先选择活动!问候。
  • @AbdelhakOhammou $l[6] 的内容是什么?你能做一个 var_dump($l[6]) 吗?这将告诉您 $l[6] 是否包含字符串“active”或“inactive”。让我知道你发现了什么。干杯...
  • 我已经解决了这个问题,感谢您的宝贵时间,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2017-02-01
  • 2020-01-28
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多