Parallel MySQL queries for HTML table - WHILE(x or y)?
Posted
by
Beti Chode
on Stack Overflow
See other posts from Stack Overflow
or by Beti Chode
Published on 2012-11-19T04:08:49Z
Indexed on
2012/11/19
5:00 UTC
Read the original article
Hit count: 111
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!
© Stack Overflow or respective owner