Paid affiliate advertisement
$50 for solution to alternating Node Types on a page
Thanks in advance for any help with this.
I'm looking for a solution to alternate between 2 Node Types going down the page.
Here is an example. A page that contains 5 "Story" Node Types and 2 "Advertisement" Node Types:
Webpage
---------
Node Type: Story, Sorted by: Date Created
Node Type: Advertisement, Sorted by: Random
Node Type: Story, Sorted by: Date Created
Node Type: Advertisement, Sorted by: Random
Node Type: Story, Sorted by: Date Created
Node Type: Story, Sorted by: Date Created
Node Type: Story, Sorted by: Date Created
Please contact me if you would like to help. I am not an engineer but have lots of Drupal and PHP code implementation experience.
Thanks again.
Sc0tt
PS. Using Drupal 5.7, MySQL database v4.1.22, PHP v5.2.5, Apache v1.3.37 (Unix)

Have you thought about using
Have you thought about using panels? A single column panel could hold individual views, with each view displaying your content type in any order you desire. Or are you thinking of something completely different?
----------------------------------------------------------------------
http://www.bwv810.com/
I am a writer and researcher. In my spare time I build websites with Drupal.
Je peux communiquer en français. / Я могу общаться на русском языке.
I agree, but...
It looks like he wants to insert ads within the content, so having all the ads on one side might not be the best business decision :)
Maybe a single-column panel with blocks
Block: Single Random Ad
Block: View - Newest or Random Story
Block: Single Random Ad
Block: View - Random/Newest stories.
First, create a view that
First, create a view that displays only your content nodes and no ads.
Assuming you have a theme called "mytheme", and a view called "my_node_list" and your ads type is "ad".
Note that this will not duplicate ads, so it will only display as many ads as you have. If you don't mind duplicates, you could remove the sort clause from the query and pick from the ad_nids array using rand() in PHP.
<?php
// Change variables and function name as needed and drop this in your template.php
function mytheme_views_view_nodes($view, $nodes, $type, $teasers = false, $links = true) {
// Are these the droids we're looking for?
if ($view->name == 'my_node_list') {
$ad_type = 'ad';
$node_count = count($nodes);
$nodes_shown = 0;
// Select as many ad node nids as we need sorted by rand.
$query = "SELECT n.nid FROM {node} n WHERE n.type='%s' ORDER BY RAND() LIMIT %d";
$result = db_rewrite_sql(db_query($query, $ad_type, $node_count));
while ($row = db_fetch_array($result)) {
$ad_nids[] = $row['nid'];
}
// Display each node as usual
foreach ($nodes as $n) {
$node = node_load($n->nid);
$output .= node_view($node, $teasers, false, $links);
// After each node, if there are ad nodes left, display one
// unless this is the last node
if (++$nodes_shown < $node_count) {
$ad_nid = array_pop($ad_nids);
if ($ad_nid) {
$ad_node = node_load($ad_nid);
$output .= node_view($ad_node, $teasers, false, $links);
}
}
}
}
else {
// Otherwise display the standard node list
$output = theme('views_view_nodes', $view, $nodes, $type, $teasers, $links);
}
return $output;
}
?>
Thanks criznach!
This worked perfectly!
I have 20 different sections (taxonomies) on my site that I want to do this to requiring 20 different views and I'm not that familiar with PHP. Could you recommend the best way add a list of multiple view names? Perhaps in the IF statement? See example below:
(pseudo-ish code inserted into your code)
if ($view->name == 'my_node_list' OR 'my_node_list_2' OR 'my_node_list_3' OR 'etc.' ) {Thanks again!
Sc0tt
PS. I will contact you via personal contact form.
I'm not sure if this is as
I'm not sure if this is as automatic as you'd like, but you could use PHP's in_array function, like this...
<?php
// Set up your view names in an array
$valid_views = array('my_node_list', 'my_node_list_2', 'my_node_list_3', 'etc');
if (in_array($view->name, $valid_views)) {
// do something...
}
?>
Thanks criznach!
Array solution worked perfectly.
Thanks again,
Sc0tt