By liberiangirl90 on
Hi,
browsing the forums I found this code to display the latest 10 nodes of type "page":
<?php
$sql = "SELECT * FROM {node} WHERE type = 'page' AND status = 1 ORDER BY created DESC LIMIT 10";
$result = db_query(db_rewrite_sql($sql));
$latest_nodes = array();
while ($data = db_fetch_object($result)) {
$latest_nodes[] = l($data->title, 'node/' . $data->nid);
}
print theme('item_list', $latest_nodes);
?>and it works. The problem is that it only displays a list. How can I turn it into a table?
Comments
Use Views and forget about
Use Views and forget about the custom code.
Views, views, views.
I would recommend looking into using views (http://drupal.org/project/views) instead of coding this one with php. It has a slight learning curve, but it makes life so much easier for those who don't want to get tabgled up with drupal's php functions.
Here's how you would do this in views:
* Add a new view with an administrative title of "latest_pages," and be sure the type of view radio button is on "Node." You can also give it an administrative description such as "The latest pages on my website."
* Add a Filter (the plus button next to "filter" on the right), look for Node: content type, select "Page," and press update.
* Add a Sort Criteria (above filter), and look for Node: post date. Make the sort order "descending" and update.
* Add a Field (middle column), and look for Node: title. On the settings you see, make sure you check the "link this to its node" checkbox.
* In the left column, "Items to Display:" should already be set to 10 by default, if not, make it 10.
* Right above that should be "Use pager:" be sure it's set to "no"
Save the view
You can now hit the preview button on the bottom of the view to see if it's giving you the right results. If not, check your steps again. If so, there's just one last part:
To add the view to your website, we need to create a place for it to go.
* The most common two places are in a block or on a separate page. You can do either by choosing it in the select list and pressing "Add Display."
* If you went with a page, there are new options on the bottom of the left column. You can give the page a path, as well as set a menu item for it
* If you went with a block, you should go to site building -> blocks, and find your new block.
Making it a table:
* In the left column, click on your "Style:" setting and choose "table"
* There are a lot of options. You can put two fields in the same column of a table by using the select drop downs you see (and put a separator between them too).
* If you want to change the order of your fields, use the up/down arrows next to the "Fields" gear in the center column.
* You can get back to these options by clicking the gear next to your new "table" style
Remember to preview and save!
That's just the bare bones of what views can do. It might take a little bit to understand at first, but it's extremely customizable, and definitely worth it in my opinion. I would die having to code/find code for everything I do with views.
Hope all that helps.
Alan
Thanks a lot, it all works
Thanks a lot, it all works perfectly! :)