i have some reference to record for a given article. a reference might be of type an article, book, thesis etc..
each might have different attributes, e.g.
//article_refs table
ID Article_ID Article_Title Author_Name ...
1 1 title1 author1
2 1 title2 author2
//thesis_refs table
ID Article_ID Thesis_Title Thesis_Publisher ...
1 1 thesis1 publisher1
2 1 thesis2 publisher2
//ref_types table
ID Article_ID ReferenceType
1 1 book
2 1 article
3 1 article
4 1 book
when i insert into one of the tables, i first into ref_type table with its type (book, article). then insert whichever table it belongs. (e.g. if it is an article, insert into article table)..
now, i have to be able list references in order.
$sql=mysql_query("SELECT * FROM ref_types
WHERE $article_ID=Article_ID ORDER BY ID ASC");
while($row = mysql_fetch_array($sql)){
$counter=1;
if($row[2]=="thesis"){
$sqlthesis=mysql_query("SELECT * FROM thesis_refs
WHERE $article_ID=Article_ID
ORDER BY ID ASC");
while($thesis_row = mysql_fetch_array($sqlthesis)){
echo "record $counter: ";
echo $row[2];
echo ", ";
echo $thesis_row[2];
echo "<BR>";
$counter++
}
}elseif.....
this way it lists all thesis records then lists article table etc..
record 1: book1
record 2: book2
record 3: article1
record 4: article2
i know it is simply because of while loop, but how to get this (same order with ref_types table..)??
record 1: book1
record 2: article1
record 3: article2
record 4: book2
any help is appreciated.
thanks in advance..
In addition to having ReferenceType column in ref_types table, you also need a Reference_ID column that refers to the actual ID in the corresponding table as a foreign key.
Then, you can avoid a WHILE loop and let MySQL do the work for you with JOINs:
Yields the result: