dousao8152
2017-06-08 08:41
浏览 34
已采纳

添加CSS样式,这取决于mysql_fetch_array中的php IF语句

I want to add CCS style which depends on php IF statement. I have found something: changing the style inside "if" statement, but somehow it doesn`t work, maybe because of WHILE statement in my code.

<?php
$possible_lang_list = mysqli_query($con, 
'SELECT * FROM page WHERE title = "lang" ORDER BY name1 ASC');

while ($row = mysqli_fetch_array($possible_lang_list)) {
  echo '
      <div class="language" id="' . $row['name2'] . '">
      <p>' . $row['name1'] . '</p>
      </div>
  ';
}
?>

I want to display only div class="language" where $row['name2'] == $x, with possibility to display whole class "language" with JavaScript. Could anyone help?

I have tried:

 while ($row = mysqli_fetch_array($possible_lang_list)) {
  if ($row['name2'] == $x) {
  ?>
  <div style="display:block;">
  <?php } else {
  ?>
  <div style="display:none;">
  <?php
  echo '
      <div class="" id="' . $row['name2'] . '">
        <p>' . $row['name1'] . '</p>

      </div>
  </div>';
  }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doumei1772 2017-06-08 08:53
    已采纳

    now it is working:

    while ($row = mysqli_fetch_array($possible_lang_list)) {
      if ($row['name2'] == $x) {
      ?>
      <div style="display:block;">
      <?php } else {
      ?>
      <div style="display:none;">
      <?php   }
      echo '
          <div class="" id="' . $row['name2'] . '">
            <p>' . $row['name1'] . '</p>
    
          </div>
      </div>';
    
    }
    
    点赞 打赏 评论
  • douchunxian9740 2017-06-08 08:53

    Assign a style element with a true/false ternary, like below. Then just output it!

    <?php
    
    while ($row = mysqli_fetch_array($possible_lang_list)) {
      $class = ($row['name2'] == $x) ? 'language' : null;
      echo '
          <div class="'.$class.'" id="' . $row['name2'] . '">
          <p>' . $row['name1'] . '</p>
          </div>
      ';
    }
    

    And then:

    点赞 打赏 评论
  • douzhengzuo7283 2017-06-08 08:54

    Try this code :

    while ($row = mysqli_fetch_array($possible_lang_list)) 
    {
      if($row['name2'] == $x)
      { 
         echo '<div class="language" id="' . $row['name2'] . '"><p>' . $row['name1'] . '</p>
      </div>';
      }
      else
      {
        echo '<div class="" id="' . $row['name2'] . '"><p>' . $row['name1'] . '</p>
      </div>';
      }
    

    }

    Hope it will work

    点赞 打赏 评论

相关推荐 更多相似问题