I really badly need to create a static HTML table where some of the cells spit out CCK field data. I am trying to achieve a table that has "blank rows" which in my case is something that views can not do.

So I plan to just manually structure an HTML table and then have a PHP snippet in a cell return data if there is any in the database, else return nothing and the PHP cell in that row will be blank.

Here is what I am trying to achieve:

Parking Row Parking Space Unit ID Status
A 1 90203 OS
A 2
A 3 333425 R

The first two columns ('Parking Row' and 'Parking Space') will be static hard-coded HTML. The second two columns ('Unit ID' and 'Status') are CCK values that must come from the database. I placed bold/italics on the cells that I want to come from CCK field data via PHP snippet.

About the CCK data: I have a database of semi tractor trailers, each one is a node with a unique CCK field called 'Unit ID' (achieved using the Unique Field module). The 'Unit ID' is a real number in the real world, physically painted on the back of each semi trailer; it's how we keep track of our equipment.

So every trailer we own has its own Drupal node, with a unique 'Unit ID' CCK field and a slew of other CCK fields holding information about each individual trailer. The fields that concern us here are 'Parking Row' and 'Parking Space' - they tell us where that trailer is parked.

So the purpose of my entire table is to make a "map" of the parking lot so that we can quickly see which trailer is parked in which space. We also need to see which spaces are empty, which is why I don't/can't use views (if a space is empty then there is no node associated with it). All I need to do is figure out how to PHP-print the 'Unit ID' for the node where 'Parking Row' = x and 'Parking Space' = x. Then I can insert the appropriate snippet into each table cell and it should pull the CCK data, and show blank cells when there is no node (trailer) parked in that space.

Here is how I plan to code it. All I need from you is help with the PHP!

<table>
    <thead>
    <tr>
        <th>Parking Row</th>
        <th>Parking Space</th>
        <th>Unit ID</th>
        <th>Status</th>
    </tr>
  </thead>
  <tbody>
          <tr>
                  <td>A</td>
                  <td>1</td>
                  <td><?php PRINT UNIT_ID FROM CCK FIELD FROM ANY NODE WHERE PARKING_ROW=A & PARKING_SPACE=1 ?></td>
                  <td><?php PRINT STATUS FROM CCK FIELD FROM ANY NODE WHERE PARKING_ROW=A & PARKING_SPACE=1 ?></td>
          </tr>
          <tr>
                  <td>A</td>
                  <td>2</td>
                  <td><?php PRINT UNIT_ID FROM CCK FIELD FROM ANY NODE WHERE PARKING_ROW=A & PARKING_SPACE=2 ?></td>
                  <td><?php PRINT STATUS FROM CCK FIELD FROM ANY NODE WHERE PARKING_ROW=A & PARKING_SPACE=2 ?></td>
          </tr>
          <tr>
                  <td>A</td>
                  <td>3</td>
                  <td><?php PRINT UNIT_ID FROM CCK FIELD FROM ANY NODE WHERE PARKING_ROW=A & PARKING_SPACE=3 ?></td>
                  <td><?php PRINT STATUS FROM CCK FIELD FROM ANY NODE WHERE PARKING_ROW=A & PARKING_SPACE=3 ?></td>
          </tr>
  </tbody>
</table>

I just don't know PHP/CCK well enough to write a working snippet. Do you? Any examples or suggestions are greatly appreciated. Thanks!

Linko

Comments

nevets’s picture

I would probably take a different approach. I would make a content type called parking space, with row, space and a node referrer field to trailers. In trailers I would replace row and space with reference to parking space. I would then build my view showing parking spaces and add a relation on the node referrer field.

lunk rat’s picture

Your approach may be superior to my snippet plans. There are two things I will need to overcome before taking your approach:

(1) I need to understand node referrer fields better.
(2) We are updating all this data from a daily-hand-updated CSV file via the Feeds module in order to reduce the amount of "clicks" for our data entry staff. I am pretty sure that I can still use this approach with the node-referrer method but it may become more complex.

I may have a few questions about this as I proceed. You game?

Thanks again!

JoeMcGuire’s picture

Database Query and Template files

Unfortunately it's very difficult to guide you for what you want to do with the snippets without seeing your database and template structure.

The general concept is to query the database for the data you want and then pass the values through to the template.

  1. http://api.drupal.org/api/function/db_query/6
  2. http://api.drupal.org/api/function/hook_theme/6

Parking Spaces, Node Reference and Views Relationships

I'd suggest exploring the option of creating a node type 'parking space' then creating a node for each of your spaces. Then adding to your trailer node type a Node Reference field to the parking space it is in. You could in theory then have views view of all parking spaces and details of trailers currently referencing each space by using a views relationships.

lunk rat’s picture

After two comments about node reference in the first ten minutes after this post, I will definitely be putting a huge effort into this approach.

If you would be so kind, I would like to know more about/how to use a "views relationship" - any info or links are appreciated. You rock!

JoeMcGuire’s picture

Views documentation

Views has hidden it's documentation away in the Advanced Help module.

  1. Download Advanced Help
    http://drupal.org/project/advanced_help
  2. Enable module
  3. Click on advanced help
  4. Find the views relationship section
lunk rat’s picture

Joe,

I have followed your suggestions and here is what I have so far:

Created a new content type, "Yard Location" with CCK fields "Parking Row" and "Parking Space"

Created a test node of this content type, "Row A Space 1"

Created a node reference CCK field on my "Trailer" content type referencing node type "Yard Location"

Referenced one Yard Location node in a Trailer node via the above node reference field

Created a new view with the following:

Filters: Node type = Yard Location
Fields: Content: Yard Row, Content: Yard Space

. . . and at this point I'm not sure what to do to get Unit ID and Status fields from the referenced Trailer nodes to appear as fields next to the Row and Space fields from the Yard Location nodes.

What do I do with relationships? How do I add the fields in the "Fields" section of the view?

Thanks SO MUCH for your help.

nevets’s picture

Add a Node Referrer field to Yard Location (lets call the field 'trailer'). Configure to use the node reference field from Trailer.

In the view add a relatioship on 'trailer' (the field), mark as NOT required.

Add the fields for "Trailer" to the view, configure them to use the relationship.

lunk rat’s picture

You folks got my project out of a slump and back on the track to production. Your suggestion to use Node Reference and Node Referrer and Views Relationships worked perfectly! Thank you so much for your time and kindness.

lunk rat’s picture

If I have a trailer node with a nodereference field referring to a parking space node, then I change the reference field to point to a different parking space node, the view displays the current nodereference field data as well as all data historically in that filed.

So even though the node has only one value for its nodereference field, the view displays all values that were ever in that field.

Thoughts?

lunk rat’s picture

I other words if I update that field in a node, the view continues to display the old field values, in addition to the new values.

nevets’s picture

It sounds like instead of using node referrer you want to look at the Back reference which enforces a 1-1 relation between 2 node reference fields and if you unset one, unsets the other (given you have both node reference fields setup correctly).

lunk rat’s picture

I installed Back reference but I'm not finding a lot of documentation and I'm not sure where to go from here.

nevets’s picture

Instead of using the node referrer field, delete and a node reference field.

If you have back reference installed when you configure a node reference field you will see new options (all the way to the bottom). For each of the two content types "point" the node references to the other content types node reference field.

lunk rat’s picture

It looks like I only have one problem: the views relationship is showing node revisions--the values from the entire history of changes to the fields in the node--and I only want it to show the latest revision.

I can't see any options in views to prevent node revision data from appearing in the nodereference/views relationships fields. Help?