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
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.