skip to Main Content

I want to display certain DIVs to certain users. To do that I have a column in phpMyAdmin, called “UserType” which indicates if the user is above or below 18. Based on that, if the user is below 18, I want to hide all DIVs with ID “18+”. Here’s an example of two different DIVs that contain products:

<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>

What I’m trying now is:

    <?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’ means mature user and ‘C’ means child

2

Answers


  1. i just add “.adult” class and check it

    The forEach() method executes a provided function once for each array element.

    The call() method calls a function with a given this value and arguments provided individually.

    [].forEach.call(document.querySelectorAll(".content"), (el) =>
    {
        if (el.classList.contains("adult"))
        {
            el.style.display = "none";
        }
    
    })
    <div id="18+" class="content adult">
      <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>
    Login or Signup to reply.
  2. Considering you are using phpMyAdmin and SQL, I would recommend you use PHP to decide which div is printed to the page.

    Example:

    <?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 */ }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search