Skip to main content

How to show products based on each category in Laravel and JavaScript

I have Categories in my Database and The products that should be displayed once I click the buttons of each category so here is my items blade code :

@foreach ($categories as $category)
<span id="span-" class="item"  data-name=""  onclick="btnClick(this)"></span>
 @endforeach

<div  class="activities-grid">
          @foreach ($products as $product)
        <div id="test1" class="activities-grid-item " style=" background-image: url(/">
          <h1 class="activities-h1">
    TEST1
          </h1>
        <div class="col-md-3 col-sm-6 my-3 my-md-0">
         <div class="card-body">
              <h6 class="icons">
                  <i class="fas fa-star"></i>
                  <i class="fas fa-star"></i>
                  <i class="fas fa-star"></i>
                  <i class="fas fa-star"></i>
                  <i class="far fa-star"></i>
              </h6>

          </div>

         </div>

                       <div class="card-stuff">

                    <p class="card-text"></p>
                    <span class="price"></span>
                    ?>
                </div>
        </div>
        @endforeach

(I did the same code for every product)

and this is my JavaScript code :


function btnClick(btn){
  const name = btn.getAttribute('data-name');
  const span1 = document.getElementById("span-test1");
  const span2 = document.getElementById("span-test2");
  const span3 = document.getElementById("span-test3");
  const span4 = document.getElementById("span-test4");
  const span5 = document.getElementById("span-test5");
  const span6 = document.getElementById("span-test6");
  const test1 = document.querySelectorAll('#test1');
  const test2= document.querySelectorAll('#test2');
  const test3= document.querySelectorAll('#test3');
  const test4= document.querySelectorAll('#test4');
  const test5= document.querySelectorAll('#test5');
  const test6= document.querySelectorAll('#test6');
  switch(name){
      case 'test1':
        span1.classList.add('active');
        span2.classList.remove('active');
        span3.classList.remove('active');
        span4.classList.remove('active');
        span5.classList.remove('active');
        span6.classList.remove('active');
        for (i = 0; i < test1.length; i++) {
          test1[i].style.display = "flex";
        }
        for (i = 0; i < test2.length; i++) {
          test2[i].style.display = "none";
        }
        for (i = 0; i < test3.length; i++) {
          test3[i].style.display = "none";
        }
        for (i = 0; i < test4.length; i++) {
          test4[i].style.display = "none";
        }
        for (i = 0; i < test5.length; i++) {
          test5[i].style.display = "none";
        }
        for (i = 0; i < test6.length; i++) {
          test6[i].style.display = "none";
        }
          break;

(I did the same code for every case)

So Please any idea how to fix this problem and display every product based in it category?



source https://stackoverflow.com/questions/68958051/how-to-show-products-based-on-each-category-in-laravel-and-javascript

Comments

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype