I've got a rather simple PHP snippet in the allowed values PHP code box for an integer field (select list):

$i = -50;
$values = array()
while ($i >= 50) {
  $values[] = array($i => $i);
  $i++;
}
print_r($values);
return $values;

The code never gets executed. I don't see the result of the print_r anywhere (same if I change it to dsm($values);). The list on the node add/edit form only lists "- None -" as an option. Any idea what's going on here?

Comments

dawehner’s picture

perhaps it helps you if you use a correct php code :)

<?php
$i = -50;
$values = array()
while ($i >= 50) {
  $values[$i] = $i;
  $i++;
}
print_r($values);
return $values;
?>
markus_petrux’s picture

It looks to me a semi-colon is missing here: $values = array()

Also, print_r() won't show anything here because the PHP snippet output is discarded. Try using drupal_set_message() for this purpose.

tobiasb’s picture

$i = -50;
$values = array();
while ($i <= 50) {
  $values[$i] = $i;
  $i++;
}
print_r($values);
return $values;

-50 >=50 ??

seanr’s picture

markus, looks like that was it. Thanks.

Razorraser, looks like you read that backwards. It's doing a loop while $i is <= to 50, increasing $i by 1 each pass. It starts at -50. The point was to give me a weight menu identical to the one Drupal uses for menu item weights.

tobiasb’s picture

What is the different between while ($i >= 50) (Yours) and my while ($i <= 50) ?

result with $i >= 50 -> array() no good idea

result with $i <= 50 -> array(-50 => -50, -49 => -49.........., 49 => 49, 50 => 50, ) thats works. :D

tobiasb’s picture

Category: bug » support
tobiasb’s picture

Status: Active » Closed (fixed)

Please open the issue again, if you want.

colinjones’s picture

Status: Closed (fixed) » Active

No matter what I put in my Allowed Values PHP section it never seems to get executed. I don't receive any errors - even when I know there is a PHP syntax error, and it always ignores the code and just populates the allowed values with the static list rather than using the PHP. Am I missing something fundamental here? Do I have to "turn on" PHP somewhere first for these CCK fields? I am just trying to have a text select box drop down filled with all the user names on the site, so I was trying this snippet:

global $user;
$sql = "SELECT name FROM users WHERE uid = ".$user->uid.";
$res = db_query($sql);
while($row = db_fetch_array($res)){
$rows[$row['name']] = $row['name'];
}
return $rows;

But it seems like nothing I enter gets executed. Not sure if there is any logging output anywhere, certainly there is nothing in the status log about problems with PHP....

seanr’s picture

colinjones: you've got a sintax error in your code (extra quote). Try this:

global $user;
$sql = "SELECT name FROM users WHERE uid = '$user->uid'";
$res = db_query($sql);
while ($row = db_fetch_array($res)) {
  $rows[$row['name']] = $row['name'];
}
return $rows;
colinjones’s picture

And bingo! That worked :) Thanks....

Now obviously this is only ever going to return the current user... have you any suggestions for modifying the code so that it returns an array of users for a particular named role?

Also, is there anywhere that these syntax errors are reported so that in future I can work out what went wrong?!

seanr’s picture

Unfortunately, the syntax errors just disappear into nowhere. I think that's a bug in CCK.

As for the other question, you'd just have to do a SELECT uid FROM users_roles WHERE rid = '3' (or whatever your role ID is). Then take all of those uids, put them into a comma separated list and modify your query above to be SELECT name FROM users WHERE uid IN ($uids);

nevets’s picture

You could also use a single query like

$role  = 3;  // Or what ever role you are interested in
$sql = "SELECT u.name FROM {users} u JOIN {user_roles} ur USING(uid) WHERE rid = %d";
$res = db_query($sql, $role);
seanr’s picture

Yeah, that's better. Don't know why I didn't think of it. LOL

markus_petrux’s picture

Status: Active » Fixed

So it looks the last case has been fixed.

If anyone has similar problems, then please try to execute your PHP snippet somewhere. For example using separate .php file invoked from the command line. That should help you track syntax errors. For help on general PHP programming, please use the development forums.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.