gpt4 book ai didi

javascript - 根据用户年龄显示div

转载 作者:搜寻专家 更新时间:2023-10-30 20:03:44 24 4
gpt4 key购买 nike

我想向某些用户显示某些 DIV。为此,我在 phpMyAdmin 中有一个名为“UserType”的列,它指示用户是高于还是低于 18 岁。基于此,如果用户低于 18 岁,我想隐藏所有 ID 为“18+”的 DIV。下面是包含产品的两个不同 DIV 的示例:

<div id="18+" class="content">
<a href= "pulpfiction.html"><img src="img/2.jpg" style="width:100%"></a>
<p>Pulp Fiction</p>
<p>£8.90</p>
<hr>
</div>

<div id="13+" class="content">
<a href= "blueexorcist.html"><img src="img/blueexorcist.jpg" style="width:100%;"></a>
<p>Blue Exorcist</p>
<p>£8.90</p>
<hr>
</div>

我现在正在尝试的是:

    <?php
$userQuery = mysqli_query($con, $sql="SELECT UserType FROM users;");
$user = mysqli_fetch_assoc($userQuery); //gets user data as an array
?>

<script>
function toggleDivAdult(){
var x = document.getElementById("13+");
var y = document.getElementById("18+");
if ($user['UserType'] == 'M') { x.style.display = "none"; }
else if ($user['UserType'] == 'C') { y.style.display = "none"; }
</script>

'M'表示成熟用户,'C'表示 child

最佳答案

考虑到您正在使用 phpMyAdmin 和 SQL,我建议您使用 PHP 来决定将哪个 div 打印到页面。

例子:

<?php
$c = ; //your sql connection
$userQuery = mysqli_query($c, /* your query for the user */);
$user = mysqli_fetch_assoc($userQuery); //gets user data as an array
if ($user['UserType'] == '18+') { ?>
<div ...></div>
<? }
else if ($user['UserType'] == '13+') { ?>
<div ...></div>
<? }
else { /* otherwise */ }
?>

关于javascript - 根据用户年龄显示div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54311117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com