Sorry if there is a solution to this, but I can't find it.

I want a quick and dirty way of having classifieds where I don't have to administer them using node queues.

I have created a flexinode which I call General Classified. Now I want a view that shows only those General Classified that were created in the last 7 days (or 14, or 21, based on your role)

So if the classified was created in the specific period, then they show up, otherwise they have "expired", and it all works without too much brain strain or intervention on my part.

The views module doesn't seem to have a date filter.

I've had a look at the snippets library, but can't see anything that would do what I want. Also, do other people have the problem that the code snippets run off the page and they can't see them???

Any assistance will be greatly appreciated.

Tx, Tys.

Comments

pobster’s picture

  global $user;
  foreach ($user->roles as $role) {
    switch ($role) {
      case "authenticated user":
        $start = time() - 1209600; // 2 weeks
        break;
      default:
        $start = time() - 604800; // 1 week
        break;
    }
  }
  $flexi_id = "flexinode-5";
  $list_no = 10;
  $sql = db_rewrite_sql("SELECT n.type, n.nid FROM {node} n WHERE n.type = '%s' AND n.created >= %d LIMIT %d");
  $result = db_query(sprintf($sql, $flexi_id, $start, $list_no));
  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load(array('nid' => $node->nid)), 1);
  }
  print "<div id=\"classifieds\">";
  print $output;
  print "</div>";

Something like that I guess? Obviously you could do 'fancier' stuff if required such as specify the list length in the 'switch' statement so that you'd get a different one for each role or as the roles are in a foreach (meaning every role your user is in gets counted here) you could decrease the $start variable for each role rather than specify it each time (as I did above).

For example:

  global $user;
  $start = time();
  foreach ($user->roles as $role) {
    switch ($role) {
      case "authenticated user":
        $start = $start - 1209600;
        break;
      default:
        $start = $start - 604800;
        break;
    }
  }
...rest of code...

...And in answer to your last question - nope, I don't get that trouble using the slash or bluemarine themes.

Pobster

tyswan’s picture

Looks fine so far - I don't need fancy. So long as it works and I can see what's happening it's all good. I just have to wait 7 days to see if it works ;)

I'll put in a test for 1 day, and see if my ad expires tomorrow.

Thanks heaps!

One last question...

How do I get all the ads? I don't want to limit myself - I just want them all if they fall within the timeframe. Can i set list_no to something that means "all"? -1 or something?

I'm using the default theme for viewing drupal.org
Check out this post, where it's deeply nested http://drupal.org/node/30967 I can't read any of the sql about a third of the way down. Maybe that's a firefox thing... Also I just noticed that copying & pasting the code works correctly (if you can see what you're copying).

Anyway, cheers, and thanks again.

--
tys

pobster’s picture

It's a firefox thing I'm afraid... I see what you're referring to now, and yes it happens to me as well! I thought you were talking about stuff disappearing off the bottom of the page which happens to me with certain themes :o(

Just remove the LIMIT part of the sql query to return all the entries (and the $list_no references).

Glad to help

Pobster
edit: just to clarify;

  $list_no = 10;  // remove
  $sql = db_rewrite_sql("SELECT n.type, n.nid FROM {node} n WHERE n.type = '%s' AND n.created >= %d");
  $result = db_query(sprintf($sql, $flexi_id, $start));
pobster’s picture

It's very easy to add pagination to the results rather than just display the whole lot. If it's useful to you to do so, here's how it'd look;

  global $user;
  foreach ($user->roles as $role) {
    switch ($role) {
      case "authenticated user":
        $start = time() - 1209600; // 2 weeks
        break;
      default:
        $start = time() - 604800; // 1 week
        break;
    }
  }
  $flexi_id = "flexinode-5";
  $list_no = 5;
  $sql = db_rewrite_sql("SELECT n.type, n.nid FROM {node} n WHERE n.type = '%s' AND n.created >= %d");
  $result = pager_query(sprintf($sql, $flexi_id, $start), $list_no);
  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load(array('nid' => $node->nid)), 1);
  }
  print "<div id=\"classifieds\">";
  print $output;
  print "</div>";
  print theme('pager', array(), $list_no);

Pobster

tyswan’s picture

Nice.

I was going to research that aspect later (only got one test classified so far), but thanks for offering that up - it will save me trawling for the code later.

Hmm, and the truth is I don't really understand how it works *shrug*, so it's helpful that you've shown me how to integrate it.

Thanks again,

--
tys

pobster’s picture

I could break it down and explain/ walk you through it if you'd like?

It's no trouble - I could do with the karma tbh, I'm a bad person.

Pobster

tyswan’s picture

All good karma to you, my friend!

So it seems as though the pagination is simply done through that one call
print theme('pager', array(), $list_no);

Thanks heaps, I appreciate the time and effort to help out php dummies like me.

Cheers,
--
tys

pobster’s picture

Hello again! :o)

No worries, glad to help. I'm afraid one of my tooltips got cut off short... Yes - the pagination links are displayed through the line: print theme('pager', array(), $list_no); but where you normally use say, db_query to get stuff out of the database, you'd use pager_query instead. *That's* where the magic is ;o) ...And yes, that's all you need to do! :o)

Pobster

mooffie’s picture

The views module doesn't seem to have a date filter.

It has. Enter "now" in the value field and "-604800" in the options field (the latter is number of seconds in seven days, IIUC).

(or 14, or 21, based on your role)

Ah, that's the "hard" part. I would create several sub-tabs with different values in the options field.

tyswan’s picture

Pobster's solution is just fine, does exactly what i want.

Just for interests sakes, which filter selection do i choose to type the values and options you've specified into. It may be useful in the future.

Thx.
--
tys

erinc’s picture

moofile:
Great tip, I was trying to figure out that. thanks.

www.erinch.com