Home > Software engineering >  How do I filter with dropdown list? PHP and SQL
How do I filter with dropdown list? PHP and SQL

Time:11-16

We are trying to filter based on the tags or the dropdown menu, we are trying to create a blog website that has tags and those tags can be used to filter the posted content on the homepage Tags dropdown menu:. Here should be the content in the page: Here should be the content in the page:

    <nav> 
      <ul>
        <li><a href="Home_loggedin.php">Home</a></li>

        <li><a href="">Tags<i >arrow_drop_down</i></a>
          <ul class = "dropdown">
          <li><a href="Home_loggedin.php">All</button></a></li>
          <li><a href="Home_loggedin.php">Homemade</button></a></li>
          <li><a href="Home_loggedin.php">Pro</button></a></li>
          <li><a href="Home_loggedin.php">Resto</button></a></li>
          </ul>
        </li> 
<!-- Posting -->
 <div>
                <?php 
                  
                  $query = "select * from posts order by date limit 8";

                  $result = mysqli_query($con,$query,$tagHomemade);
                ?>
                
                <?php if(mysqli_num_rows($result) > 0):?>
                  
                  <div>
                    <?php while ($row = mysqli_fetch_assoc($result)):?>
                      
                      <?php 
                        $user_id = $row['user_id'];
                        $query = "select username, image from users where id ='$user_id' limit 1";
                        $res = mysqli_query($con,$query);

                        $user_row = mysqli_fetch_assoc($res);
                      ?>

                      <div >
                        <div style ="display: flex;">
                          <div style ="flex:1", >
                            <div ><img class ="icon" src="<?=$user_row['image']?>"></div>
                          </div>

                          <div style ="flex:2;" >
                            <h5>Posted by: <?php echo $_SESSION['logged']['username']?>,
                              <?php echo date("jS M, Y",strtotime($row['date']))?>
                            </h5>
                            <h3><?php echo $row['tag']?>: <?php echo $row['title']?></h>
                          </div>  
                        </div>


                        <div>
                          <?php if (file_exists($row['image']))?>

                          <div  style="text-align:center;">
                              <img  src="<?=$row['image']?>">
                          </div>
                        </div>

                        <div>
                          <?php if (!empty ($POST['post']));?>
                          <?php echo $row['post']?>
                        </div>
                        
                    </div>
                    <?php endwhile;?>
                  </div>
              </div>
              <?php endif;?>`
        </div>

CodePudding user response:

Set tags in the url like this:

<li><a href="Home_loggedin.php?tag=homemade">Homemade</button></a></li>

Then you can get it by $_GET['tag'] in your code. Then put it on the SQL query.

Good to know: It's strongly recommended to use prepared statements and don't use $_GET['tag'] directly in your SQL code. It prevents SQL injection and cares about special characters as well.

Update:

    <nav> 
      <ul>
        <li><a href="Home_loggedin.php">Home</a></li>

        <li><a href="">Tags<i >arrow_drop_down</i></a>
          <ul class = "dropdown">
          <li><a href="Home_loggedin.php">All</button></a></li>
          <li><a href="Home_loggedin.php?tag=handmade">Homemade</button></a></li>
          <li><a href="Home_loggedin.php?tag=pro">Pro</button></a></li>
          <li><a href="Home_loggedin.php?tag=resto">Resto</button></a></li>
          </ul>
        </li> 
<!-- Posting -->
 <div>
                <?php 
                  $where = !empty($_GET['tag']) ? "where tag = '" . $_GET['tag'] . "'" : "";

                  $query = "select * from posts $where order by date limit 8";

                  $result = mysqli_query($con,$query);
                ?>
                
                <?php if(mysqli_num_rows($result) > 0):?>
                  
                  <div>
                    <?php while ($row = mysqli_fetch_assoc($result)):?>
                      
                      <?php 
                        $user_id = $row['user_id'];
                        $query = "select username, image from users where id ='$user_id' limit 1";
                        $res = mysqli_query($con,$query);

                        $user_row = mysqli_fetch_assoc($res);
                      ?>

                      <div >
                        <div style ="display: flex;">
                          <div style ="flex:1", >
                            <div ><img class ="icon" src="<?=$user_row['image']?>"></div>
                          </div>

                          <div style ="flex:2;" >
                            <h5>Posted by: <?php echo $_SESSION['logged']['username']?>,
                              <?php echo date("jS M, Y",strtotime($row['date']))?>
                            </h5>
                            <h3><?php echo $row['tag']?>: <?php echo $row['title']?></h>
                          </div>  
                        </div>


                        <div>
                          <?php if (file_exists($row['image']))?>

                          <div  style="text-align:center;">
                              <img  src="<?=$row['image']?>">
                          </div>
                        </div>

                        <div>
                          <?php if (!empty ($POST['post']));?>
                          <?php echo $row['post']?>
                        </div>
                    </div>
                    <?php endwhile;?>
                  </div>
              </div>
              <?php endif;?>
        </div>
  • Related