By anthonybol on
This is a similar question as #278324 with some additional complexity.
I'm trying to populate my custom CCK content type field with data for users to select (multi select) when they create the new content. I have found this snippet that does what I want - but obviously not the data I want to be displayed.
$result = db_query_range('SELECT uid FROM {users} WHERE status != 0 ORDER BY uid DESC', 0, 8);
$items = array();
while ($row = db_fetch_object($result)) {
$account = user_load(array('uid' => $row->uid));
$items[] = check_plain($account->uid);
}
return $items;
Returned value : Array ( [0] => 4 [1] => 3 [2] => 2 [3] => 1 ) - These are UIDs as expected and it populates the multiselect :-)
What I want to do is something like this but I'm very new to PHP and having trouble with the correct syntax to build the proper array.
//I need to determine the uid of the logged in user to build the query
global $user;
$uid = $user->uid;
$result = db_query_range('SELECT `content_field_my_classes`.`field_my_classes_value`
AS `classes`
FROM node, content_field_my_classes
WHERE ((`node`.`uid` = '. $uid .')
AND (`content_field_my_classes`.`nid` = `node`.`nid`)) ORDER BY
`classes` ASC');
//Here's where I get lost. This is essentially the unmodified code from above. Not sure where to go with it
$items = array();
while ($row = db_fetch_object($result)) {
$account = user_load(array('uid' => $row->uid)); // I am pretty sure I don't need user_load()
$items[] = check_plain($account->classes); //What does check_plain() do? Is this a drupal function or PHP?
}
return $items;
If anyone can help me with the last few lines of the code I would be very appreciative.
Cheers!
Comments
SQL and PHP code help
Hi ... just wondering if anyone has a suggestion.
Thanks!
This should do it I think...
A couple of things:
- From the original code that you took from someone, I don't see the point of calling user_load. It didn't do anything in the snippet you provided. Now, there may be some other reason they did it, but it is not obvious why they did. You don't need it, because it loads a user object,and you don't need that from what I understand.
- Try to avoid concatenating strings into your sql query. It is much safer to pass them in as I have done with $uid
- See the API for the check_plain function: http://api.drupal.org/api/function/check_plain/5
@j_ten_man Thanks for the
@j_ten_man
Thanks for the help! I am hoping to use this php code to create a multi-select box for my custom CCK field that I have created called, "my_classes." The data is in the table, "content_field_my_classes." (I'm sure you know this from looking at the code but just clarifying).
So I copied your suggestion into the Widget Settings ->> Default Value ->> PHP Code box for this CCK field. I know that it belongs in the Data Settings ->> PHP Code box to override the Allowed Values but putting it in Default Value returns some sort of feedback that I've been using to try to figure this out. If I just put it in the Data Settings ->> PHP Code box it doesn't return any errors and the multi-select box is just empty. So it's more of a way for me to debug the code - even if it's off base.
It appears that this returned an array with no values in it. When I run the SQL with a uid that I choose (node.uid=4) on my phpMyAdmin page, it returns these values which would be used to build the multi-select box:
Right now, I am getting this message about what is returned in $items
The sample code I used that returned the uid was successful in creating the multi-select box. It's possible that putting it in the Widget Settings ->> Default Value ->> PHP Code box requires one format and the Data Settings ->> PHP Code box requires a different format. I don't really know but since the original sample code snippet worked in the Data Settings ->> PHP Code box then I assume the format for the sample code was acceptable (it did create the multi-select box).
Does this provide any clues to the solution? Again, I appreciate your help very much.
<?php...while ($row =
I think that should do it. Let me know if that works or not.
Making Progress ... I used
Making Progress ...
I used the new version of the while loop and it returned an empty array.
Returned value : Array ( [0] => )
I checked the HTML that it rendered and this was what was generated:
It looks like the array is empty - or just not what the CCK wants. Is there a way to get find out exactly what is returned by $items? I don't have a great deal of PHP experience but what you've written seems to look like it should work. Any other thoughts?
To compare the output from
To compare the output from the Data Settings ->> Allowed Values box to the "non-output" of the Data Settings ->> PHP Code box I did the following in hopes of figuring out how to get the PHP code to work.
I entered the following key|Value pairs into the Data Settings ->> Allowed Values box
and this is the rendered HTML output:
I don't know if this sheds any more light on how to create the php code or not but I'm hoping it will help. I'll keep reading.
First, run this sql in your
First, run this sql in your database:
As long as that returns something then (not sure if this will work) try
this in your while loop and send me a copy of what you get:
Just so you know this will exit your program and should jut print the results. If you remove print_r($row) and exit() calls then this will fix the problem, but this will be good for debugging this. If for some reason it doesn't exit, then that means that the query is not returning any results.
Let me know your results.
Ok ... running the SQL on my
Ok ... running the SQL on my database returned this list of classes:
However, when I ran the extra php code
it did not exit and nothing was put into the multi-select. That doesn't seem to make sense since the SQL on the database does return a result.
EDIT: I tried the PHP code again this time by substituting a real uid in the SQL and it still did not exit even though it's the exact SQL that I used to get the list of classes directly from the database.
It exited this time ... with
It exited this time ... with this output:
stdClass Object ( [classes] => Directed Green )
This is the first returned value from the database. There are three more. Making progress!
Remove the print_r and exit
Remove the print_r and exit statements from the while loop. You should get your desired results.
How do I "unbreak" the site?
How do I "unbreak" the site? I clicked the BACK button, removed the code, and clicked "Save Field Settings" but it still exits.
Try clearing the cache and
Try clearing the cache and cache_content table. I think that should do it.
@j_ten_man Hi j_ten_man ...
@j_ten_man
Hi j_ten_man ... I had to run to a meeting and then the rest of life took over. I emptied the cache_content table but I still get the exit on loading the site. I am not sure how to clear the cache if I can't get to the site. Any other ways to get rid of this?
Thanks!
Weird...
Not sure. Run this query in your db and let me know what you get.
Make sure you replace the type_name with the correct Content type that this is for (if you are unsure then you can remove this part of the query and just use the part up until the AND).
No worries. I had a very
No worries. I had a very recent backup so I just restored it. Since this is a dev site there wasn't much to lose.
It's getting closer! In previous attempts, the rendered HTML form did not have anything in the select form so it looked like this:
But now there is SOMETHING populating it. There are actually five "blank" options each with a value 0->4 which is exactly the number of classes that should be returned in $items.
So ... we *really you* have got it very close. Any ideas of why it's not giving the option even though it fills in the value?
Just as a reminder, here's the code I've used.
Change the
Change the following:
$items[] = $row->field_my_classes_value;
Should be
$items[] = $row->classes;
That's my oversight. Let me know if that works.
Getting Closer
Ok, I have been tweaking the php with more of a trial and error attack and I was able to get some actual data to populate the select list. Here's what I did.
Which renders the following HTML:
The odd thing about this is the
<optgroup>tag. I'm not sure why that's there other than it's due to $items not returning the array in the way CCK likes it.Did you see my above post?
Did you see my above post? You need to change
$items[] = array($row->field_my_classes_value);
to
$items[] = $row->classes;
It Works!
@j_ten_man thanks so much! You've really helped me out and I appreciate it. Now I can move forward. I'm hoping that doing it this way will allow me to create the proper Views to display the information to unauthenticated users. .... that may be another posting at a later time!