Home>

Tell me how to implement the sample correctly. There is an array of employees and each is assigned a group in the database "F" under the number of the department

<
?
$ sql_result= $ db->
Query ("SELECT * from` Localuser` Where f!= 0 Order by f + 0 ");
While ($ row= $ db->
get_row ($ sql_result)) {
$ name= $ row ['name'];
$ f= $ row ['f'];
Echo '
≪
DIV CLASS= "CBP-Item '. $ f.'" >
≪
span >
≪
B >
'. $ Name.' <
/b >
≪
/span >
≪
/div >
 ';
}
? ≫

following the result of HTML code:

<
DIV Class= "CBP-Item 1" >
≪
span >
≪
B >
Employee name in group 1 <
/b >
≪
/span >
≪
/div >
≪
div class= "CBP-ITEM 2" >
≪
span >
≪
B >
Employee name in group 2 <
/b >
≪
/span >
≪
/div >
≪
div class= "CBP-ITEM 2" >
≪
span >
≪
B >
Employee name in group 2 <
/b >
≪
/span >
≪
/div >
≪
DIV Class= "CBP-Item 3" >
≪
span >
≪
B >
Employee name in group 3 <
/b >
≪
/span >
≪
/div >
≪
DIV Class= "CBP-Item 4" >
≪
span >
≪
B >
Employee name in group 4 <
/b >
≪
/span >
≪
/div >
≪
DIV Class= "CBP-Item 4" >
≪
span >
≪
B >
Employee name in group 4 <
/b >
≪
/span >
≪
/div >

Tell me how to make it was before each group was a headline for example:

<
H1 >
Group 1 <
/h1 >
    ≪
DIV Class= "CBP-Item 1" >
    ≪
span >
≪
B >
Employee name in group 1 <
/b >
≪
/span >
    ≪
/div >
≪
H1 >
Group 2 <
/h1 >
    ≪
div class= "CBP-ITEM 2" >
    ≪
span >
≪
B >
Employee name in group 2 <
/b >
≪
/span >
    ≪
/div >
    ≪
div class= "CBP-ITEM 2" >
    ≪
span >
≪
B >
Employee name in group 2 <
/b >
≪
/span >
    ≪
/div >
≪
H1 >
Group 3 <
/h1 >
    ≪
DIV Class= "CBP-Item 3" >
    ≪
span >
≪
B >
Employee name in group 3 <
/b >
≪
/span >
    ≪
/div >
≪
H1 >
Group 4 <
/h1 >
    ≪
DIV Class= "CBP-Item 4" >
    ≪
span >
≪
B >
Employee name in group 4 <
/b >
≪
/span >
    ≪
/div >
    ≪
DIV Class= "CBP-Item 4" >
    ≪
span >
≪
B >
Employee name in group 4 <
/b >
≪
/span >
    ≪
/div >

At the moment I see only the ability to set WHERE F= 1, then copy the code and set in where F= 2 and so on. But it seems to me that it is completely wrong. After all, at the moment there are 10 departments, and it turn out to be the same in their essence pieces of code. Thank you.

It is usually easier to separate the data processing and their output, first pull out, grouped (in the case of PDO it is simple), and then remove the double cycle. And in relation to this code fragment, put the initial name of the group in NULL, if we see. If different from the current line, output the new name and replace the value of the current one.

teran2021-06-09 09:09:19

At the PHP level, get a complete list of employees as it is. Then rebuild the resulting array to the new, grouped by the group. The third step will withdraw everything using 2 cycles (one by groups, another employees who will be embedded)

ArchDemon2021-06-09 09:09:25