Hi,

I'm having some trouble with telling the Argument Code box to do what I want. I can select a single row from the database, but I can't work out how to get multiple rows back and add them to the $args array. I'd like the results of the SQL query to determine which nodes are displayed.

This is working and returning back 1 result:

if (arg(0) == 'node' && is_numeric(arg(1))) { 
		$sql = "SELECT field_tag_artist_nid FROM {content_field_tag_artist} WHERE nid = %d"; 
		$args[0] = db_result(db_query($sql, arg(1))); 
} 

return $args;

This doesn't display any content at all in the view:

if (arg(0) == 'node' && is_numeric(arg(1))) { 
		$sql = "SELECT field_tag_artist_nid FROM {content_field_tag_artist} WHERE nid = %d"; 
		$results = db_query($sql, arg(1))
		while($row = db_fetch_array($results)) {
			$args[] = $row
		}
} 

return $args;

Can somebody point me in the right direction?

Thanks,
Luke

Comments

inductor’s picture

Does it work when

$args[] = $row

is replaced with

$args[] = $row[0];

?

----
input#edit-delete { margin-left: 4em;}

l_d’s picture

No, I still get nothing. Good suggestion though. I also get nothing if I try SELECT * and $row[3], which is the column the field_tag_artist_nid is in.

For Argument Type I've selected Node: ID, I'm not sure if that affects anything. I don't understand the Argument Code logic, but I've spent a couple of days trying to find information online and trying things out.

inductor’s picture

$results = db_query($sql, arg(1))
$args[0] = array ();
$args[0][] = 'or';
while($row = db_fetch_array($results)) {
  $args[0][] = $row[1];
}

?

In other words, we should pass the following array: ('or', 'nid1', 'nid2', 'nid3'...)

--
input#edit-delete { margin-left: 4em;}

l_d’s picture

Thanks for your help, still no luck though. My code looked like this after your suggestion:

if (arg(0) == 'node' && is_numeric(arg(1))) { 
		$sql = "SELECT field_tag_artist_nid FROM {content_field_tag_artist} WHERE nid = %d"; 
		$results = db_query($sql, arg(1))
		$args[0] = array();
		$args[0][] = 'or';
		while($row = db_fetch_array($results)) {
		  $args[0][] = $row[1];
		}
} 

return $args;

I also tried $args[0][] = $row[0] and $args[0][] = $row but still nothing is showing up yet. There should be two nodes that display.

Thanks.

inductor’s picture

Try to insert

print_r ($args);

before the return statement. This will print the contents of $args array. Post it here.

--
input#edit-delete { margin-left: 4em;}

l_d’s picture

print_r() doesn't seem to cause anything print to screen. I tried manually assigning a value to the $args array before it and that didn't print anything either, so it's not because $args was blank. I'd really like to know how to debug within the Argument Code box to see what's going on! Is there any other way Drupal could print a variable dump from code in the Argument Code box?

l_d’s picture

Hmm .. which is strange because #36 of http://www.lullabot.com/audiocast/podcast-50-drupal-tips-and-tricks seems to say that print_r() should work. I'm making this view for a block, but even ticking Page, assigning the view a URL and viewing it there, I still get nothing back from putting print_r($args); in the Argument Code.

inductor’s picture

Replace your Arguments handling code with print_r($view). Does that show anything?

--
input#edit-delete { margin-left: 4em;}

l_d’s picture

Still nothing :). I'm looking at $view->filter

http://drupal.org/node/86410#comment-631383

As some kind of way of selecting? I'm just clutching at straws at this point. It produces a nice SQL error so at least it's trying something...

l_d’s picture

Okay, I'm now looking at a thread

http://lists.drupal.org/pipermail/support/2007-March/004316.html
http://lists.drupal.org/pipermail/support/2007-March/004319.html
http://lists.drupal.org/pipermail/support/2007-March/004321.html

and it seems the way I understood the $args was all wrong. Which sadly still doesn't help me much. I've realised the reason why I'm struggling is because the table I'm wanting to select from, I'm effectively needing to do a look up in reverse from what Drupal seems to want me to do.

Drupal uses the arguments to create a WHERE condition, except the SQL statement returns the 'nid' values, whereas in this situation I'm wanting to return the field_tag_artist_nid values. I wonder if I can get Drupal to do this at all? If all else fails I'll try directly hacking my code into the template.