dongyan1808 2013-10-20 17:14
浏览 46
已采纳

使用SQL Result查询第二次并通过php获取更多结果

I am grabbing a list of school names from a database. When the user clicks on the school name, i want the code to fetch 2-3 other attributes related to the school name that the user has clicked on.

My code for the single school name list:

$query = mysql_query("Select schoolname, product, username, password from credentials c
join products p on p.productid = c.productid
join schools s on s.schoolid = c.schoolid
join icons i on i.productid = p.productid
order by s.schoolname");

echo '<ul>';
while($row = mysql_fetch_array($query))
{                   
echo "<li> <a href='#'>" . $row['schoolname'] . "</a> </li>";
}

echo '';

Current Output:

  • School A
  • School A
  • School B
  • School B
  • School B
  • School C
  • School C

Expected Output:

  1. School name displayed only once (distinct won't work because each school name has 2-3 product credentials

    • School A
    • School B
    • School C
  2. Ability for User to click on a school. then the school names underneath will move down (toggle effect) and user can see product, username and password displayed for the school clicked (for example: user clicked on School A)

    • School A
    • productname1, username, password
    • productname2, username, password

    • School B

    • School C
  • 写回答

1条回答 默认 最新

  • dongxian5735 2013-10-21 21:50
    关注

    1) You should use GROUP BY schoolname, but note that this only would give you only one value for one unique schoolname row, and thus you might lose some other data. Also you might need multiple GROUP BY statements if you want fetch all data at once and not lose your product credentials

    2) There are few ways of doing this. You can fetch all needed data at once, generate html output with school and hidden product names and toggle them via javascript.

    <div class = "school-name">
        School name 1
        <div class = "product-name" style="display: none;">Product1</div>
        <div class = "product-name" style="display: none;">Product2</div>
        <div class = "product-name" style="display: none;">Product3</div>
    </div>
    

    and javascript, i'm using jquery for it

    $(document).ready(function() {
        $('.school-name').on('click', function(e) {
            $(e.currentTarget).children('.product-name').show();
        }
    });
    

    This approach works fine as soon as you have small amounts of data. Otherwise, it's a better practice to use AJAX call to get children nodes of element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况