I made a data base with head, sub_head, item 1,item2 and want the display to be
Head
Subhead Item1 Item2
subhead Item1 Item2
Head
Subhead Item1 Item2
subhead Item1 Item2… wher the subhead would be two or more.
Currently the code is correctly
Head
Subhead Item1 Item2
subhead Item1 Item2
Don’t run select queries inside of loops. This is very inefficient, due to the time it takes for each round-trip communication between php and the database server, vs the time it actually takes to execute the query on the database server.
Also, the first query is getting all the data, not just the unique head(ing) values, so, you won’t get the expected result from the current method, and the reason for the output stopping after the first pass through the outer loop is because you are reusing the variable holding the result from the first query, in the code for the second query. That variable no longer contains a value that the outer loop expects.
Just change the first/single query so that it gets the data you want in the order that you want it (almost every select query should have an ORDER BY term.) To produce the output, with the heading only displayed once, you can either use a variable to hold the ‘last’ heading value, then detect and save the new value each time it changes, or I like to index/pivot the data using the heading value as the main index in an array of arrays, when you retrieve the data, then simply loop over this array of arrays to produce the output.
Some other points -
List out the columns you are selecting in the query. This will insure you are only selecting the data you need and it will cause your query/code to be self documenting. Anyone reading the query/code will be able to tell what you are trying to do, without needing your database table definition.
Don’t copy variables to other variable without any purpose, just use the original variables the data is in. Unless you are doing this to get typing practice, to improve your speed and accuracy, the 7 lines of code copying variables to other variables don’t add any value to what you are doing.
The head(ings) should be defined in a separate table. This will produce heading id values. You would store the ids in any table holding data related to the headings. This will use the last amount of data storage, produce the fastest queries, and allow the heading names to be edited without requiring all the data using the headings to be updated.
That’s a two-dimensional array assignment statement. What it is doing is this -
Using $data[] = $row; would create numerically indexed, 0, 1, 2, … elements in $data. Using $data[ $row['head'] ] = $row; would create elements in $data having an index value of whatever is in $row[‘head’], with each new element for any specific ‘head’ index value replacing the previous element. By adding a 2nd array level, the on the end, making an array of arrays, or a sub-array, each new $row is stored as numerically indexed, 0, 1, 2, … elements in the main array of whatever ‘head’ index value the $row of data contains.
If you examine the data, with the following code, it should be clear(er) what is occurring -