I’m trying to create a table using PHP. What I need is a table with two columns.
So I have an SQL table with 4 fields – primary key id, language, word and definition. The language for each is either Arabic or Russian.
I want a table that does the following:
| defintion |
|____________________|
| |
| rus1 | arab1 |
| rus2 | arab2 |
| rus3 | arab3 |
| rus4 | |
So it divides the list by English word, creates a for each English word, then lists Russian equivalents in the left column and Arabic in the right. However there are often not the same number for both. What I am doing right now is running a WHILE loop in a WHILE loop. The outer loop is running fine but I think I am doing the inner loop wrong. Here is the bulk of the code:
$definitions=mysql_query("SELECT DISTINCT definition FROM words")
WHILE($row=mysql_fetch_array($definitions)
{
ECHO '<tr><th colspan="2">' . $row['definition'] . '</th></tr>';
$russian="SELECT * FROM words WHERE language='Russian' AND definition='".$row['definition']."'";
$arabic="SELECT * FROM words WHERE language='Arabic' AND definition='".$row['definition']."'";
WHILE($rus=mysql_fetch_array($russian) or $arb=mysql_fetch_array($arabic))
{
ECHO '<tr><td>'.$rus['word'].'</td><td>'.$arb['word'].'</td></tr>';
}
}
Sadly I am getting soemthing like this:
| defintion |
|____________________|
| |
| rus1 | |
| rus2 | |
| rus3 | |
| rus4 | |
| | arab1 |
| | arab2 |
| | arab3 |
Not sure what other way I can do this? I tried changing the or to || thinking the different precedence would cause another outcome, but then I get ONLY the Russian column.
I’m out of ideas, you guys are my only hope!
Well you’re basically saying: “While (output) do a row and a cell” then inside that “Do another and another” without adjust for the other cells already there.
Tbh, I’d just be putting the results from each table or mysql query in an array, use array_merge to put them into a single array and then doing the output.
E.g. Russian has 4 results, arab has 3 so the array would look like this:
Then do a:
Good luck!