简体   繁体   English

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

[英]Use SQL Result to Query second time and grab more results via 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. 当用户单击学校名称时,我希望代码获取2-3个与用户单击的学校名称相关的其他属性。

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 学校A
  • School A 学校A
  • School B B学校
  • School B B学校
  • School B B学校
  • School C C校
  • School C C校

Expected Output: 预期产量:

  1. School name displayed only once (distinct won't work because each school name has 2-3 product credentials 学校名称仅显示一次(区别不起作用,因为每个学校名称都有2-3个产品凭据

    • School A 学校A
    • School B B学校
    • School C 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) 那么下方的学校名称将向下移动(切换效果),用户可以查看所点击学校的产品,用户名和密码(例如:用户点击了学校A)

    • School A 学校A
    • productname1, username, password 产品名称1,用户名,密码
    • productname2, username, password productname2,用户名,密码

    • School B B学校

    • School C C校

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. 1)您应该使用GROUP BY学校名称,但是请注意,这只会给您一个唯一的学校名称行仅一个值,因此您可能会丢失一些其他数据。 Also you might need multiple GROUP BY statements if you want fetch all data at once and not lose your product credentials 另外,如果您想一次获取所有数据且不丢失产品凭据,则可能需要多个GROUP BY语句

2) There are few ways of doing this. 2)很少有这样做的方法。 You can fetch all needed data at once, generate html output with school and hidden product names and toggle them via javascript. 您可以一次获取所有需要的数据,生成带有学校名称和隐藏产品名称的html输出,并通过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 和JavaScript,我正在使用jQuery

$(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. 否则,最好使用AJAX调用来获取element的子节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM