I have the following results returned via a mysql query:
tag_id article_id (asc) name tag_slug
56 69487 Exploration exploration
10 69488 Events events
32 69488 Military military
28 69489 Arts arts
3 69489 Religion churches
36 69490 Forestry forestry
8 69490 Industry industry
40 69490 Man-Made man-made
42 69490 Politics politics
I need to loop through the results and create a string that would include the tag_slugs associated with each set of article id's. The name and tag_id columns aren't necessary for this part of the code.
For example ...
69487 would have a string that is: '<exploration>'
69488 would have a string that is: '<events><military>'
69489 would have a string that is: '<arts><churches>'
69490 would have a string that is: '<forestry><industry><man-made><politics>'
...and a column named tags would be updated in the db with these strings and the respective article_id.
My attempt below works, kind of, but always leaves off the last db update. I'm sure there has to be a better more logical way but right now I cannot.
$previous_article_id = 0;
while( $row = $result->fetch_assoc() )
{
if ( $row['article_id '] != $previous_article_id && $previous_article_id != 0 )
{
$sql = "
UPDATE
".ARTICLE_TABLE."
SET
tags = '".$tags."'
WHERE
id = ".$previous_article_id."
";
$db->query($sql) OR sql_error($db->error.$sql);
$tags = '';
}
if ( $row['article_id '] == $previous_article_id || $previous_article_id == 0 )
{
$tags .= '<'.$row['tag_slug'].'>';
}
$previous_article_id = $row['article_id '];
}
Yes, I know it should be PDO and the codes a bit crazy, but I'm working on a friends brothers website so not much authority to change it.
tagsis a seperate field & it's value should be<forestry><industry><man-made><politics>say forarticle_id69490?