php - 如果值为 true,则为每个项目添加类

标签 php jquery mysql methods vue.js

帮助我遇到了严重的麻烦,我正在尝试从 mysql 查询获取所有结果并显示它们。对于每个结果 if Automotive = 1 我想将一个类添加到名为 auto 的段落标记中。如果 Gaming = 1 我想添加一个名为 games 的类。一些结果将汽车和游戏设置为一。在这种情况下,我想将这两个类添加到元素中。我如何在 php 中执行此操作?我一直遇到的问题是某些元素正在获取它们不应该拥有的类。似乎它为每个项目添加了相同的类。即使它们没有值 1。再次针对每个结果检查 Gaming 是否 = 1 并将 Gaming 类添加到该段落中。如果 Automotive =1,则将 Automotive 类添加到段落中;如果两个类 =1,则添加它们,依此类推。我需要这些类才能工作,以便我可以在 jquery/vuejs 中有条件地显示和隐藏它们。这是代码。

<?php require_once('init.php');

$query="SELECT * FROM `bannerStock`";
$result = mysqli_query($db_conx, $query);
while($row=mysqli_fetch_assoc($result)) {
         $categorytoadd="";
      if($row['Automotive']==1){$categorytoadd.=' Automotive';}
                                if($row['Backgrounds']==1){$categorytoadd.=' Backgrounds';}
                                if($row['Church']==1){$categorytoadd.=' Church';}
                                if($row['Community']==1){$categorytoadd.=' Community';}
                                if($row['Money']==1){$categorytoadd.=' Money';}
                                if($row['Food']==1){$categorytoadd.=' Food';}
                                if($row['Gaming']==1){$categorytoadd.=' Gaming';}
                                if($row['Healthcare']==1){$categorytoadd.=' Healthcare';}
                                if($row['Holidays']==1){$categorytoadd.=' Holidays';}
                                if($row['Sports']==1){$categorytoadd.=' Sports';}
                                if($row['Patriotic']==1){$categorytoadd.=' Patriotic';}
                                if($row['Retail']==1){$categorytoadd.=' Retail';}
                                if($row['Education']==1){$categorytoadd.=' Education';}
                                if($row['Misc']==1){$categorytoadd.=' Misc';}
    $myArray[] = $row;
}
?>

<!doctype html>
<html>
<head>
  <script src="https://unpkg.com/vue"></script><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
  <div id="app">
      <input type='text' v-model='keyword' placeholder='search item'>
  <button>ALL</button>
      <button v-on:click="showgaming">Gaming</button>
      <button v-on:click="showchurch">Church</button>
      <button v-on:click="showauto">Auto</button>
      <div v-for="item in filteredItemList">
<p class="video <?php echo $categorytoadd; ?>">{{item.name_short}}</p>
    </div>
  </div>
  <script>
  var app = new Vue({
    el: '#app',
    data: {
      itemList: [],
        keyword:'',
        showall:'0',
    },
    created: function() {
        this.loaddata();
    },
    methods: {
            loaddata: function(){
                  var vueapp = this;
                        vueapp.itemList = <?php echo json_encode($myArray, JSON_PRETTY_PRINT); ?>;
                },
        showgaming: function(){
            $('.video:not(.Gaming)').hide();
        },
                showauto: function(){
            $('.video:not(.Automotive)').hide();
        },
                        showchurch: function(){
            $('.video:not(.Church)').hide();
        },
    },
        computed:{
    filteredItemList(){
      return this.itemList.filter((item) => {
        return item.name_short.toLowerCase().includes(this.keyword.toLowerCase());
      });
    },
  }
  });
  </script>
</body>
</html>

最佳答案

1) 在 while 之外定义 $myArray:

$myArray = [];

2) 将类添加到行本身并将其推送到数组(这在 while 内部):

$row['class'] = $categorytoadd;
array_push($myArray, $row);

3)最后,在显示项目时使用行类:

<p :class="['video', item.class]">{{ item.name_short }}</p>

关于php - 如果值为 true,则为每个项目添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49614960/

相关文章:

php - 如何在 PHP session 中存储 javascript 数组?

javascript - 页面高度不根据 div 中的内容量调整大小

java - 尽管在 MySQL 控制台中有效,但PreparedStatement 执行时显示 MySQLSyntaxErrorException

python - django admin 在尝试更新记录时崩溃但在插入新记录时不崩溃

php - 如何: PHP + Javascript Pop-up Window

php - 拉维尔 : enabling debug toolbar

php - 在包含路径 phpstorm 中找不到 PHPUnit

javascript - 按其父 div 缩放 svg

javascript - 如何在 JavaScript 中使用链模式和 self 显示模块模式?

mysql - ElasticSearch river JDBC MySQL 不删除记录