Jump to content

repeating results from mysql


cindreta

Recommended Posts

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

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 ; )

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);

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.