dounaoji2054 2018-12-31 22:41
浏览 64
已采纳

用新图像重新加载DIV

I have an employee database which includes images as well as their work location (specialty). I have created a page where I fill out a form and upload the image to a directory and the path to the database. I then load the main page where I pull in all the images from the database (into the "photos" DIV. Everything works fine.

What I would like to do is reload the images in the DIV based on a MySQL query from a button. For example, instead of showing all employees, I only want to see those who have a specific job function i.e. Management. I currently have this accomplished by redirecting to a new page, where I run a specific query and that works fine as well. However, I'd like to learn how this is done without creating a new page for each query. I've spent many days looking at AJAX and PHP tutorials, which I how I was able to accomplish what I have, but I can't find a method to do what I want. This is the relevant part of my code:

Main.php

<div class="container-fluid">
<div class="row">
<div class ="col-lg-12"  style="width: 18%; height: 100%; border:3px solid red;">

<a href = "management.php" class="btn btn-primary">MANAGEMENT</a>
<a href = "high.php" class="btn btn-primary">HIGH</a>

</div>
<?php
 include ('db_connect.php');
 //include_once ('functions.php');

 $result = $db->query("SELECT * from monctonfir order by initials ASC");

 if($result->num_rows > 0){
    while ($row = $result->fetch_assoc()){
        $imageURL = 'image/'.$row["file"];
        $initial = $row["initials"];
        $name = $row["name"];
    ?>




    <div id="photos" class = "col-lg-1 no-gutters" style="margin-top:1rem;">
    <div class="card text-center" style="width: 5rem;">
    <a href = "#">
        <img class="img card-img-top" src = "<?php echo $imageURL ?>"> 
    </a>
    <div class = "card-body">
        <h5 class = "card-title round-button" style="text-align: center;"><?php echo $initial  ?></h5>
    </div>

    </div>
    </div>

    <?php }
 } ?>

</div>

Can someone point me in the right direction? Thanks!

  • 写回答

2条回答 默认 最新

  • dousi4257 2019-01-01 00:17
    关注

    You don't need jQuery for what you are doing. You can use query/GET parameters to build your sql so you don't have to create a different page. Like:

    <div class="container-fluid">
        <div class="row">
            <div class ="col-lg-12"  style="width: 18%; height: 100%; border:3px solid red;">
    
                <a href = "?job=management" class="btn btn-primary">MANAGEMENT</a>
                <a href = "?job=high" class="btn btn-primary">HIGH</a>
                <a href = "?" class="btn btn-primary">ALL</a>
    
            </div>
            <?php
            include ('db_connect.php');
            //include_once ('functions.php');
    
            $sql = "SELECT * from monctonfir WHERE 1 ";
    
            if(isset($_GET['job'])) $sql .= " AND job = '".$_GET['job']."' ";
    
    
            $sql .= " order by initials ASC";
    
            $result = $db->query($sql);
    
            if($result->num_rows > 0){
                while ($row = $result->fetch_assoc()){
                    $imageURL = 'image/'.$row["file"];
                    $initial = $row["initials"];
                    $name = $row["name"];
                    ?>
    
    
    
    
                    <div id="photos" class = "col-lg-1 no-gutters" style="margin-top:1rem;">
                        <div class="card text-center" style="width: 5rem;">
                            <a href = "#">
                                <img class="img card-img-top" src = "<?php echo $imageURL ?>">
                            </a>
                            <div class = "card-body">
                                <h5 class = "card-title round-button" style="text-align: center;"><?php echo $initial  ?></h5>
                            </div>
    
                        </div>
                    </div>
    
                <?php }
            } ?>
    
        </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度