Closed (fixed)
Project:
Content Construction Kit (CCK)
Version:
6.x-2.2
Component:
number.module
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
4 Jun 2009 at 19:59 UTC
Updated:
14 Nov 2009 at 19:50 UTC
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
Comment #1
dawehnerperhaps it helps you if you use a correct php code :)
Comment #2
markus_petrux commentedIt 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.
Comment #3
tobiasb-50 >=50 ??
Comment #4
seanrmarkus, 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.
Comment #5
tobiasbWhat 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
Comment #6
tobiasbComment #7
tobiasbPlease open the issue again, if you want.
Comment #8
colinjones commentedNo 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....
Comment #9
seanrcolinjones: you've got a sintax error in your code (extra quote). Try this:
Comment #10
colinjones commentedAnd 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?!
Comment #11
seanrUnfortunately, 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);
Comment #12
nevets commentedYou could also use a single query like
Comment #13
seanrYeah, that's better. Don't know why I didn't think of it. LOL
Comment #14
markus_petrux commentedSo 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.