Hi there. I am developing my personal module based on the "annotation module" proposed by "Pro Drupal Development" book.
I am making my own modifications and i am trying to do the following step:

I have this code:

 $query = db_query('SELECT note FROM {annotations} WHERE nid = %d 
     AND uid = %d', $node->nid, $user->uid);
    while($row = db_fetch_array($query)){
     echo "Annotation =>> " . $row[note] . " <br>");
    }

This just show me all the annotation previously added to a specific node and by a specific user (the one that added the node ofc). As a matter of fact the list of annotations are just showed on the top of the web page...above the header because no content items are speficied to show them in a specific area of the page.

To fix this, i created 2 forms as "boxes" for my query result.

 $form['annotateshow']=array(
 '#type'=>'fieldset',
 '#title' =>t('Show Annotations'),
);

$form['annotateshow']['content']=array(
 '#type'=>'item',
 '#value' => isset($node->annotations) ? $node->annotations : ' ',
);

return $form;
}

All i have to do now is writing the result of the query inside $node->annotations

I ranged from silly tries to more "tought" ones.

1)

 $query = db_query('SELECT note FROM {annotations} WHERE nid = %d 
     AND uid = %d', $node->nid, $user->uid);
    while($row = db_fetch_array($query)){
     $node->annotation = "Annotation =>> " . $row[note] . " <br>");
    }

This obviously doesn't work because, if i get it right, the $node->annotation content item will be replaced everytime the while goes for a iteration; infact the result will be the last annotation and nothing more.

2) I tought about placing everything in a new array so that i could get out of the "while" and print the new array inside $node->annotation, like this below:

$query = db_query('SELECT note FROM {annotations} WHERE nid = %d 
     AND uid = %d', $node->nid, $user->uid);
    $arr = array();
    while($row = db_fetch_array($query)){
     array_push($arr, "Annotation =>> " . $row[note] . " <br>");
    }
    $node->annotations = $arr; 

I don't get why this shouldn't work; i did some debug printing the array with print_r and everything is right...the new array contain all the annotations as well...also printing $arr[i], with i as 0 or 1 or 2 ecc, shows annotations perfeclty.

Am i missing something here? If i am right, most likely i am doing something wrong about this

$node->annotations = $arr;

Is there a right way to do this or it can't be done at all?

I am new to drupal and, because of that, i am counting mostly on pure PHP. Do you have any suggestion of what should i look for; maybe some Drupal API i am not taking into account.

Thanks in advance :)

Comments

maxferrario’s picture

What if changes your solution number 1 as follows?

$node->annotation = "";
$query = db_query('SELECT note FROM {annotations} WHERE nid = %d
     AND uid = %d', $node->nid, $user->uid);
    while($row = db_fetch_array($query)){
     $node->annotation .= "Annotation =>> " . $row[note] . " <br>");
    }

What I changed is:

  1. set $node->annotation to an empty string before entering the while loop
  2. add every annotation to the end of the $node->annotation string
N4R4YAN’s picture

This so works! Thank you max.
Tho i don't really understand why this works and not the other way i did on 1).
So what i thought about the $node->annotations = ***whatever*** just replacing every time the previous inserted data because of the "while" is wrong?
It actually adds like if it is an array?
And why it won't add the way i coded it and it works the way u did?
Thank you, for me is very important to understand! :)

PS: i find out the "." before the "=" just do all the job...i'd have never thought about it...what that point stands for? is it equal to the "concatenation" function it has inside a normal string?

Again, thank you :)

maxferrario’s picture

In PHP, $a .= $b; is just a short form for $a = $a . $b; see http://www.php.net/manual/en/language.operators.string.php

N4R4YAN’s picture

guessed right then! I didn't know that form.
It's similar to the increment/decrement operators.

But just to be clear, it seems my thought was right then...that in the previous option i coded the content was every time replaced for each iteration. The way you told me, instead, kind of increment $node->annotation content with each new annotation fetched from the query result's array.