i want to pull vocabulary id (not term) from mysql with the following command but it returns with "user warning: Subquery returns more than 1 row query:" error message cause some nodes have more than 1 terms.

$sql = db_query("SELECT vid FROM {term_data} WHERE tid = (SELECT tid FROM {term_node} WHERE nid ='$nid');");
		

		while ( $data = db_fetch_object($sql) ) {
 
  print $data->vid;
  }
  

what i am tryin to achieve is to render different node/page layouts depending on its category.

for example :

different page layout for the nodes which belong to "news" category(vocabulary).

diffrent page layout for the nodes which belong to "sports" category(vocabulary) .

i am sure there is more logical ways to achieve this and i would be very glad if you share it with me.

thanks in advance.

Comments

sonorous’s picture

i know the page-term-x.tpl.php and node-x.tpl.php methods or whatever it is. i need a method which can load a different page layout when someone clicks , for example "www.example.com/node/1" link that was sorted out automatically by another sql query to frontpage (not a manually entered url).

francort’s picture

Taxonomy Theme module is right for your problem, unfortunatly it is still under development for drupal 6.x, but maybe you can help to finish it quickly :)
Anyway, something like this can be the right code to get your node's vocabulary ids:

$url_pieces = explode('/', $_GET['q'] );
if( $url_pieces[0] == "node" && is_numeric( $url_pieces[1] ) ){
	$sql = db_query("SELECT vid FROM {term_data} WHERE tid = (SELECT tid FROM {term_node} WHERE nid ='%d')", $url_pieces[1]);
           $my_vid = array();
	while ( $data = db_fetch_object($sql) ) {
	  $my_vid[] = $data->vid;
	}
}

With this( and some more ) you can start a one function module just made to change the theme depending on node's vocabulary. You'll need hook_init , assign global variable $custom_theme and run the function init_theme()

good luck!!!

sonorous’s picture

i can't(and i guess noone can) pull vocabulary id of a node that has multiple terms by help of above code(s). and it gives the error i mentioned above.

there must be a taxonomy function or a different approach to solve this problem :)..

thnx.

yuriy.babenko’s picture

Give this a shot:

	$nid = 8;
	
	$query = "	SELECT		td.vid
				  FROM		  {term_data} td,
							    {term_node} tn
				  WHERE		 tn.nid = %d
				  AND		   tn.tid = td.tid
				  GROUP BY	  td.vid
			 ";
			 
	$result = db_query($query, $nid);
	
	while($data = db_fetch_object($result))
	{
		echo '<pre>';
		print_r($data);
		echo '</pre>';
	}
	
	exit();

//edit
The above code groups results by the vocabulary ID, so even if you have two taxonomy terms from the same vocabulary assigned to a node, you will only get one row result.
//edit

---

The problem with your code is that for nid = 8 you have two taxonomy terms, so your subquery returns two result rows and breaks the query. (You cant equate two rows to one field, doesn't make sense.)

---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

sonorous’s picture

and it works. maybe you would want to add this code to php snippet section cause i believe its a useful solution ( for newbies of course :) )

thanks again to both of you guys.

francort’s picture

where are you doing this tests? on module, theme or page?