Figured it out, solution below!
I have an HTML table that is being populated by data from a database with two tables using the following structure:
Table 1:
------------------------------
|id|data|other_data|more_data|
------------------------------
Table 2:
--------------------------------
|id|entryId|fileType|other_data|
--------------------------------
I’m joining these two tables similarly:
SELECT * FROM table1 LEFT JOIN table2 ON table2.entryId = table1.id
Sample return:
------------------------------------------------------------
|id|data|other_data|more_data|id|entryId|fileType|other_data|
-------------------------------------------------------------
|1 |....|..........|.........|1| 1 |file1 |..........|
|1 |....|..........|.........|2| 1 |file2 |..........|
|2 |....|..........|.........|3| 2 |file1 |..........|
|2 |....|..........|.........|4| 2 |file2 |..........|
|3 |....|..........|.........|5| 3 |file1 |..........|
|4 |....|..........|.........|6| 4 |file2 |..........|
For each case, I am expecting at least one result, usually two which I would like to sort based on the fileType field. fileType can either be file1 or file2.
<table>
<tr>
<td>file1 data here</td>
<td>file 2 data here</td>
</tr>
</table>
If there is no file1 or file 2 in the result, insert a dash (-), but always keep that table structured in the same way, and in the same order. How can I accomplish this? I tried a simple if/else construct, but my logic is wrong and returns the data out of order; I’m having trouble wrapping my head around a working solution.
while(docs.next) {
if(docs.getString("fileType").equals("file1")) {
.....<td>file 1 data</td>
}
else if(docs.getString("fileType").equals("file2")){
.....<td>file 2 data</td>
}
}
As always, your help is greatly appreciated!
**Figured it out, thanks! Solution:
I broke the logic into two queries then used what you suggest:
– first query grabs data from table1, sets up HTML table
– set two variables(fileData1, fileData2) null
– second query grabs data from table2, loops through the results to find matches and changes the actual value if the result exists
– after loop, print table cells with results**
If I understand correctly, you need to print both
<td>s no matter what. Yourifcondition restricts printing<td>– it will print<td>only when the given condition is satisfied. So you need to alter it. Instead, you need to do this – read throughdocs, getfileType. If it is file1, assign value for that to a variable, that was initialized to null. If it is file2 , assign value for that to a variable, that was initialized to null. Then test for that variable and print either “-” or the content on each case.Assuming variables
file1Dataandfile2Datadenotes string data from file1 and file2 respectively for each iteration overdocs,