Hmm looking at your latest post maybe the join is not kosher.
Code:
ARTICLE TABLE:
| id | title | article TEXT ?|
RELATIONS TABLE:
| show_article FOREGIN KEY on ARTICLE( ID )| related_article FOREGIN KEY on ARTICLE( ID ) |
What is the primary key?? Both?
So, if you have 5 articles,
1, 'matt is cool', 'sdad'
2, 'matt 0wnz j00!', 'yes'
3, 'matt wins the lottery, eleventy-billion!', '$$$$'
4, 'unrelated', 'unrelated'
5, 'lottery', 'lottery news'
How would the relations table be? Obviously the first 3 are related since they talk about me
.
So you could potentially have 6 rows in relations based upon the matt connection:
1, 2
1, 3
2, 1
2, 3
3, 1
3, 2
And two? for the lottery:
4, 5
5, 4
Seems a little odd but I think that is the only way to handle this sort of thing (unless you only go one direction then it would be cut in half).
So your application flow will be something like this:
Give me an article ID.
Show me all articles in the database but a checkmark on ones which are related to this one.
So, you would run something like this:
Code:
SELECT title,
id,
related_article
FROM kb_articles
LEFT OUTER JOIN kb_related_article
ON related_article = id
AND show_article = $id
If you run that with show_article = 1:
Code:
+--------------+----+-----------------+
| title | id | related_article |
+--------------+----+-----------------+
| matt is cool | 1 | NULL |
| matt 0wnz | 2 | 2 |
| matt lottery | 3 | 3 |
| unrelated | 4 | NULL |
| lottery | 5 | NULL |
+--------------+----+-----------------+
Then in your loop run this:
PHP Code:
if( $query[ related_article ] != '' ) {
$checked = "checked";
} else {
$checked = "";
} // end if
echo "<input type=.. $checked>";
What I was hinting to earlier is that if you know a little bit about MySQL you can set it up to return a 'checked' or '' value depending on the value of related_article.
So you wouldn't have the if .. block but simply:
PHP Code:
$query = "select title, if( related_article is not null ) { 'checked' } else { null } )
from table... ";
while( loop ) {
echo "<input type.... $query[ checked ]>";
} // end while
That way you could dump the PHP logic and echo it right out. That is an exercise left up to you
.
Bookmarks