cindreta Posted April 1, 2010 Share Posted April 1, 2010 Hi i have this problem where i have to export my database and it has a lot of tables and all. But ill give you an example of two i have the news table and tags table. And there can be like xx number of tags for every news item in the db, and i have to implode them with ";". But thats the problem, when i run this code all the tags do appear but they keep repeating like this: news id:1 => tags: tag1;tag2 news id=2 => tags:tag1;tag2;tag3 and for every next news id he repeats all the tags from every last item. can you help me fix this? thank you $sql = "SELECT * FROM news WHERE pid='$getp' ORDER BY added"; $result = mysql_query($sql) or die("Couldn't execute query: Reason; ".mysql_error()); while ($row = mysql_fetch_assoc($result)) { $id = $row['id']; //get the id for use in late query $query_t = "SELECT tag FROM tags WHERE news_id='$id'"; $result_t = mysql_query($query_t) or die('Error : '.mysql_error()); $rows_t = mysql_num_rows($result_t); if ($rows_t != "0") { while ($row_t = mysql_fetch_row($result_t)) { for ($i = 0; $i < mysql_num_fields($result_t); $i++) { $glue_t[] = "$row_t[$i]"; } //end for loop $joined_t = implode("; ", $glue_t); echo $joined_t; } //end second while loop } //end if has results } //main while Link to comment https://forums.phpfreaks.com/topic/197216-repeating-results-from-mysql/ Share on other sites More sharing options...
ignace Posted April 1, 2010 Share Posted April 1, 2010 $joined_t = implode(';' . PHP_EOL, $glue_t); Link to comment https://forums.phpfreaks.com/topic/197216-repeating-results-from-mysql/#findComment-1035157 Share on other sites More sharing options...
cindreta Posted April 1, 2010 Author Share Posted April 1, 2010 hi ignace, i tried your fix but it still repeats the results? what can i be doing wrong? Link to comment https://forums.phpfreaks.com/topic/197216-repeating-results-from-mysql/#findComment-1035162 Share on other sites More sharing options...
cindreta Posted April 7, 2010 Author Share Posted April 7, 2010 can anybody help me then, i still didn't manage a way to fix it : ( thank you all Link to comment https://forums.phpfreaks.com/topic/197216-repeating-results-from-mysql/#findComment-1038229 Share on other sites More sharing options...
five Posted April 7, 2010 Share Posted April 7, 2010 considering the $glue_t array, on the second time it will run through the loop is not clear, so it contains data from the previous loop. if that is the case use if (isset($glue_t) unset($glue_t); or simply $glue_t = array(); in the beginning of the while loop.. Link to comment https://forums.phpfreaks.com/topic/197216-repeating-results-from-mysql/#findComment-1038236 Share on other sites More sharing options...
cindreta Posted April 7, 2010 Author Share Posted April 7, 2010 hi thank you very much five! it did stop the repeating of results : ) but now i does not implode those values with ";" or whatever i define. the code now looks like this: $query_t = "SELECT tag FROM tags WHERE news_id='$id'"; $result_t = mysql_query($query_t) or die('Error : '.mysql_error()); $rows_t = mysql_num_rows($result_t); if ($rows_t != "0") { while ($row_t = mysql_fetch_row($result_t)) { $glue_t = array(); for ($i = 0; $i < mysql_num_fields($result_t); $i++) { $glue_t[] = "$row_t[$i]"; } //end for loop $joined_t = implode("; ", $glue_t); echo $joined_t; } //end second while loop } //end if has results it echo's out the unique results but like this: "tag1tag2tag3" and like i said thats why i put implode so it would be: "tag1;tag2;tag3" any ideas? maybe i am putting somewhere in the wrong spot or something, but i am close to what i need so thank you very much, if you can figure this out it would be wonderfull ; ) Link to comment https://forums.phpfreaks.com/topic/197216-repeating-results-from-mysql/#findComment-1038246 Share on other sites More sharing options...
five Posted April 7, 2010 Share Posted April 7, 2010 well in your database you got a table named tags which has a column named tag where there are several tags for each news id.. so i assume you got the same news_id in many records for a bunch of tags. so $glue_t = array(); $sql = "SELECT * FROM tags"; $res = mysql_query($sql); while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { $news_id = $row['news_id']; if ($news_id == $id) { $glue_t[] = $row['tag']; } } echo implode("; ", $glue_t); Link to comment https://forums.phpfreaks.com/topic/197216-repeating-results-from-mysql/#findComment-1038266 Share on other sites More sharing options...
cindreta Posted April 7, 2010 Author Share Posted April 7, 2010 thank you very much for now it is working : ) Link to comment https://forums.phpfreaks.com/topic/197216-repeating-results-from-mysql/#findComment-1038273 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.