PHP分组和LOOPS(PHP Groupings and LOOPS)

编程入门 行业动态 更新时间:2024-10-27 18:32:11
PHP分组和LOOPS(PHP Groupings and LOOPS)

我如何通过技巧对其进行分组并回显每个技能的所有照片。

SELECT skills.teacher, skills.student, skills.skill, profile.photo FROM skills INNER JOIN profile ON skills.student = profile.username WHERE teacher = 'teach1' teacher | student | skill | photo ------- | -------- | ------ | ------------ teach1 | student1 | skill1 | student1.jpg teach1 | student2 | skill1 | student2.jpg teach1 | student3 | skill2 | student3.jpg teach1 | student3 | skill1 | student3.jpg teach1 | student4 | skill3 | student4.jpg teach1 | student1 | skill2 | student1.jpg

我强调这是一个例子。 我实际上并不知道“技能”的名称,因为它们是由学生创建的。 我不知道会列出多少技能。 我不知道用户会为教师添加什么技能。

我尝试过while循环和foreach循环。 我只是想不出如何分组这个和使用什么类型的循环...

我需要我的最终结果让我回应。

<div>skill1 <img src="student1.jpg"> <img src="student2.jpg"> <img src="student3.jpg"> ... ... ... </div> <div>skill2 <img src="student1.jpg"> <img src="student3.jpg"> ... ... ... </div> <div>skill3 <img src="student4.jpg"> ... ... ... </div>

How would I group this by skill and echo all the photo's for each skill.

SELECT skills.teacher, skills.student, skills.skill, profile.photo FROM skills INNER JOIN profile ON skills.student = profile.username WHERE teacher = 'teach1' teacher | student | skill | photo ------- | -------- | ------ | ------------ teach1 | student1 | skill1 | student1.jpg teach1 | student2 | skill1 | student2.jpg teach1 | student3 | skill2 | student3.jpg teach1 | student3 | skill1 | student3.jpg teach1 | student4 | skill3 | student4.jpg teach1 | student1 | skill2 | student1.jpg

I stress this is an example. I do not actually know the names of the "skill" as they are created by the students. I don't know how many skills will be listed in all. I don't know what users will add what skills to what teachers.

I have tried while loops and foreach loops. I just can't figure out how to group this and what type of loop to use...

I need my end result to allow me to echo.

<div>skill1 <img src="student1.jpg"> <img src="student2.jpg"> <img src="student3.jpg"> ... ... ... </div> <div>skill2 <img src="student1.jpg"> <img src="student3.jpg"> ... ... ... </div> <div>skill3 <img src="student4.jpg"> ... ... ... </div>

最满意答案

首先,按技能排序,以便按顺序获取它们:

SELECT skills.teacher, skills.student, skills.skill, profile.photo FROM skills INNER JOIN profile ON skills.student = profile.username WHERE teacher = 'teach1' ORDER BY skills.skill

在while循环之外分配一个变量(在本例中$lastSkill )。 检查它是否已更改,并相应地输出内容。 看看下面的例子。 您的PHP代码看起来与此类似:

$lastSkill = null; while ( $row = fetchrow() ) { if ( $row['skill'] !== $lastSkill ) { //Don't close a div, if it's the first row if ( $lastSkill !== null ) echo '</div>'; //We've moved onto a new row, echo it out echo '<div>' . $row['skill']; //Store the lastSkill $lastSkill = $row['skill']; } echo '<img src="' . $row['photo'] . '">'; }

First off, order by skill, so that you get them in order:

SELECT skills.teacher, skills.student, skills.skill, profile.photo FROM skills INNER JOIN profile ON skills.student = profile.username WHERE teacher = 'teach1' ORDER BY skills.skill

Assign a variable (In this case $lastSkill) outside of your while loop. Check if it's changed, and output things accordingly. Take a look at the example below. Your PHP code would look something similar to this:

$lastSkill = null; while ( $row = fetchrow() ) { if ( $row['skill'] !== $lastSkill ) { //Don't close a div, if it's the first row if ( $lastSkill !== null ) echo '</div>'; //We've moved onto a new row, echo it out echo '<div>' . $row['skill']; //Store the lastSkill $lastSkill = $row['skill']; } echo '<img src="' . $row['photo'] . '">'; }

更多推荐

本文发布于:2023-08-07 20:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465674.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   LOOPS   Groupings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!