hi everyone,
I am new to drupal. So, I know basic only.
I want to display list of name from the table 'my_table' as well as give the link. Following is the code,

<?php
function intranet_u_index()
{
$types = array();

$result = db_query('SELECT DISTINCT(name) FROM {my_table_name} ORDER BY name');
foreach ($result as $object) {
$types[] = $object->name;
}
return $types;

This code is not giving me any result.

Can u help me?

Comments

chrisrockwell’s picture

See db_fetch_object() - http://api.drupal.org/api/function/db_fetch_object.

while ($object = db_fetch_object($result)) {
  $types[] = $object->name;
}
sonu07’s picture

I have tried but db_fetch_object() function is not working in drupal 7.

nevets’s picture

What happens if you write DISTINCT(name) as DISTINCT(name) as name?

(side note: db_fetch_object() no longer exists in Drupal 7).

InterceptPoint’s picture

We have two conflicting comments:

1. db_fetch_object() is not working in Drupal 7. I can confirm that - it's not working for me.

2. db_fetch_object() is not in Drupal 7. Well there is no documentation (do a search) so that could be correct.

If so then we need a replacement to provide access to the CCK (now core) custom fields. A simple select of the records from a node will deliver all of the legacy fields but none of the (CCK/core) fields. Just do a print_r($results) dump from a db_query to prove that to yourself. No CCK type field will show up. CCK data is not stored in the basic table for a custom content type. db_fetch_object provides that access in Drupal 6.

That's a real gotcha for me since I write a lot PHP code to pull down the data from custom content types. (Yes, Views is wonderful but I just want better control of my page display).

This could be a real deal killer for me and Drupal 7.

jason_gates’s picture

Docs here http://drupal.org/node/310072

Also check the module examples here: http://drupal.org/project/examples

Please review the node_load() method. Calls the DrupalDefaultEntityController.load() method which in turn calls attachLoad() . AttachLoad attaches any custom fields (analogous to Drupal 6 cck).

Please remember Drupal is open source, Just look at a Drupal 7 installation and review the source code.

Come on now. We are on the third beta release, you actually believed that there is no way to select data from the database as an object? :) I have bridge for sale, interested? :) :)

InterceptPoint’s picture

Jason: Thanks for your input. You have me about 99% of the way. If you can get me that last 1% maybe we can talk about that bridge. If it's that pretty one on the east side of Manhattan I might be interested at the right price.

I've read the documentation but the CVS stuff is a bit much for me so I haven't had a look at your database examples. And yes, by the time you are at your third beta release I'm sure you guys have this all well in hand. But the documentation on how to access CCK fields is really lacking and needs someone's attention.

But in any case, my code shown below, executes fine in Drupal 7 and retrieves the single record from a custom content type: 'links'. The 'links' content type has two custom fields: link_url and link_category. The change I had to make from Drupal 6 to Drupal 7 to access those two 'CCK' fields was as follows:

Drupal 6

$lu=$node->field_link_url[0]['value'];
$lc=$node->field_link_category[0]['value'];

Drupal 7
$lu=$node->field_link_url[und][0]['value'];
$lc=$node->field_link_category[und][0]['value'];

So it was that extra [und] that was the source of my problem. The need for this change going from D6 to D7 may be well documented in the Drupal 7 docs but it didn't quite jump out at me.

One surprise to me was the fact that $lc, a text field selected by a checkbox, returns an index only and not the referenced string value. That seems odd but a print_r($node) returns the following for field_link_category and you can see that all I'm getting is the index from the checkbox list. If you have any insight on that issue you would get credit for that last 1%.

Here is the print_r($node) listing for the link_category field:

[field_link_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 2
)

)

)

And here is the code:

>
// Run the query and fetch the first (and only) record/row
$result = db_query("SELECT * FROM {node} WHERE type = :type", array(':type' => 'links',));
$record = $result->fetch(); 

// print the fields/values of $record
print('<pre>');
print_r($record);
print('</pre>');

// load the node - should include custom fields
$node = node_load($record->nid);

// Let's take a look at what $node returns
print('<pre>');
print_r($node);
print('</pre>');

// And then retrieve the field values
$t=$node->title;
$lu=$node->field_link_url[und][0]['value'];
$lc=$node->field_link_category[und][0]['value'];

//Prints Title OK
print('Title Field = '.$t);
print('<br>');

// Prints Link URL Field OK
print('Link URL Field = '.$lu);
print('<br>');

//Prints Link Category as an integer (from check box)
//$node does NOT return the value of Link Category 
print('Link Category Field = '.$lc);
jason_gates’s picture

Hi,
You problem is, you are trying to develop and diagnose via print statements (probably on a remote installation).

You install Drupal locally (on your local computer). Most hosting services do not give you access to the system logs where the web server will throw php runtime errors. With a local installation you have access to the system logs (e.g. web server, php ). You thus can monitor the system logs.

Next, with a local installation you run Drupal through a debugger so that you can trace the code execution, inspect objects, arrays, etc. It allows you to see how the base system is performing standard tasks.

I'm not sure what you mean by "cvs". CVS is a version control system. Great to use, however you need to set up a local development environment first, then think about version control. You develop locally, then deploy to your remote installation.

When you use print statements, you may be spending half your time just trying to find the output. You create additional tasks for your self. On the other hand, running a local installation through a debugger, you simply set breakpoints. No need to clutter up your code with "print debug".

It looks like you are using a module to extend the base Drupal 7. field_link_url() is not part of the Drupal 7 API, thus I don't know the details, sorry :) However, if you run the code through a debugger, you can watch whatever field_link_url() is doing.

Finally, what is the context of your code. Are you writing a module or are you a themer? In other words are you writing your code in the theme layer?

Hope that helps.

InterceptPoint’s picture

You are misreading the code which, in fact, works just fine - although your de-bugging recommendation is on the mark.

The code you referenced is NOT:

field_link_url()

The code IS

field_link_url[und][0]['value'];

This is an array value not a function and I am not using any unspecified module - this is straight Drupal 7. The field_link_url[und][0]['value']; is (AFAIK) the correct way to access the 'CCK' data in $node in Drupal 7 and it works just fine. I just posted the code here in case some poor soul has the same problem I did and needs a little help.

I really only have one problem and I think it is a 'feature' not a problem.

That problem is that any version of a check-box or radio button list that I have used for my custom content type only provides for the return of the index of an array of the possible values and not the values themselves. For example, I have set up radio buttons and check boxes with the following possible selections:

0|jQuery
1|YQL
2|Web Scraping

That format is per the Drupal instructions below which also state that it is the key (0,1,2 in my case) that will be returned and that key MUST BE A NUMERIC VALUE. So when I check the 'Web Scraping' check-box, Drupal will return '2' which is the correct zero based index value. Now this is easily handled with a little code but if I rearrange the selections or add new ones I have to go modify that code. Now that doesn't make any sense so I'm betting there is something here that I am missing.

Here is the Drupal Instructions when setting up the Check-Box or Radio Button lists:

The possible values this field can contain. Enter one value per line, in the format key|label. The key is the value that will be stored in the database, and must be a numeric value. The label is optional, and the key will be used as the label if no label is specified.

jason_gates’s picture

You are correct, I misread, it's a var not a method.

So what line of what listing does that statement come from? Since you are certain that the statement resides in a base Drupal 7 install just post the listing and line (i.e. file name and line number).

Are you writing a module or are you writing code at the theme layer? Or are you placing php code inside a node?

Again. please post the file listing and line number of field_link_url. It's in your base Drupal 7 install, where?

FYI -- The issue of how checkboxes and radio buttons work is an issue of remedial html, not the Drupal 7 api. Yes, the html form posts the "value" attribute of a radio button, not the text content placed outside of the input tag? http://www.w3schools.com/html/html_forms.asp . Please read the Drupal 7 docs here on radio buttons here http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

Again, please post the line number and list of where you are getting you $lu=$node->field_link_url[und][0]['value']; from? Listing and line number please :) :) I understand you may have taken some code and changed the variable names. If that's the case, same thing, where are you getting the code from?

InterceptPoint’s picture

I write PHP code to generate tables from custom content nodes - the sort of thing that 99% of Drupal users use Views to do with much less effort. I've done it that way myself but I like the control I get with the custom PHP code.

But I don't actually understand your question. My issue is not with the execution of the code. Drupal is doing EXACTLY what it is supposed to be doing. The code is functioning correctly and is returning the correct (int 2) value of my selected CCK field 'link_category' with has an array index of '2'. Nothing wrong there.

My question relates to the DESIGN of Drupal 7. It appears that check-boxes and radio button lists will not return the names given to those selections but simply a key indicating the position of the selection on the list. (That's not based on the output of my code, it is per the Drupal declaration on the 'Manage Fields' definition for check-boxes and radio buttons - see my previous post.) That is useful and you can certainly write code to generate the actual selections but if that is the only way to accomplish the task that seems to me to be a design deficiency in Drupal 7.

This issue is only marginally related to this thread but since I have your attention for the moment I thought it was worth asking. Could be I should just post another thread on this subject and start over. I really think there must be a work-around for this issue since it would be a big problem for modules like Views if that wasn't the case.

jason_gates’s picture

Hi,
This forum is for folks who are extending Drupal's functionality by writing modules. You write modules by using the Drupal API, The Drupal API has different functional categories like the form api. If you coded (or altered) a form with the form api, you can create checkboxes that return strings.

I just realized you aren't referring to the api documentation.

Here is the Drupal Instructions when setting up the Check-Box or Radio Button lists:

The possible values this field can contain. Enter one value per line, in the format key|label. The key is the value that will be stored in the database, and must be a numeric value. The label is optional, and the key will be used as the label if no label is specified.

That's something an end user reads when filling out a dialog in the admin ui, it's not a reference to how to code, right? You still haven't answered the question, are you writing a module, a theme function or are you just storing php code in a module? That's OK, your answer is implied, you don't have a coding question :) Good luck.