Randomized Results

0

I want to show only 3 random records of a POD. I want there to always be 3. I am using the line of code below, but sometimes it shows 1 and sometimes 2, etc. Is there a way to set a minimum of 3 shows?

      $features->findRecords('RAND()', 3);

asked Feb 1 at 9:38

senojeel

3

add comment
enter at least 15 characters

6 Answers

0

As long as you have 3 rows in your database, you should get three rows with that code.

The other option would be to return all the rows and use a for loop to get just the first three records.

Watch out if you are using $_GET variables in your site. Pods will read some of these and modify your output.

answered Feb 1 at 10:56

chris.pilko

889

edited Feb 1 at 10:58

add comment
enter at least 15 characters
0

Maybe I have it in the wrong place. It mostly either shows just one record or no records.

Here is the link: http://cmfi.org/partner/discover-great-projects/

It is under the Featured Projects.

<?php
      $features = new Pod('projects');
                $features->findRecords('RAND()', 3);
      $total_projects = $features->getTotalRows();
    ?>
            <?php if( $total_projects > 0) : ?>
    <div id="featured">

            <h1>Featured Projects</h1>

      <?php while ( $features->fetchRecord() ) : ?>

        <?php
          // set our variables
          $project_id                  = $features->get_field('id');
          $project_name                = $features->get_field('name');
          $project_slug                = $features->get_field('slug');
      $project_country             = $features->get_field('location.name');
          $project_projecttype    = $features->get_field('projecttype.name');
          $project_description    = $features->get_field('summarydesc');
          $project_mpowercode     = $features->get_field('mpowercode');
          $project_image                   = $features->get_field('projectimage');
          $project_fundinggoal     = $features->get_field('projectfundinggoal');
          $project_fundingtext     = $features->get_field('projectfundingtext');
          $project_featured                = $features->get_field('featuredproject');
          $project_large                   = $features->get_field('largeproject');

          // data cleanup
          $project_description = wpautop( $description );
          $project_image       = $project_image[0]['guid'];

        ?>

answered Feb 1 at 11:49

senojeel

3

add comment
enter at least 15 characters
0

I see what you are talking about. You must be calling something else in your code that is advancing the record pointer. Whatever it is, it's below what you show here.

I just made a quick test on one of my sites with this code and in 10 tries always got 3 items returned:

$Record = new Pod('my_pod');
$Record->findRecords('RAND()', 3);
printf ('<p>Total rows  = %d</p>', $Record->getTotalRows() );
$i = 1;
while ($Record->fetchRecord()) {
  printf ('<p>%d: %s</p>', $i++, $Record->get_field('name') );
}

BTW, your line

if( $total_projects > 0) :

is worthless. $total_projects will always equal the total number of items in your pod, regardless of how many you return.

answered Feb 2 at 12:50

chris.pilko

889

add comment
enter at least 15 characters
0

Thanks Chris,

I am really trying to figure this out without asking for your help at every step...

Thanks for the point about the worthless line...I used it only because there was an example that had it there.

I see how your code will work. Obviously I want to return one of those full panels for each record. I do have a conditional that checks to see if a project is checked as featured and is considered a large project. Below is my full code for that section, before putting your code in. I am not sure how to put in the loop what I need.

 <?php
      $features = new Pod('projects');
                $features->findRecords('RAND()', 3);
      $total_projects = $features->getTotalRows();
    ?>
            <?php if( $total_projects > 0) : ?>
    <div id="featured">

            <h1>Featured Projects</h1>

      <?php while ( $features->fetchRecord() ) : ?>

        <?php
          // set our variables
          $project_id                  = $features->get_field('id');
          $project_name                = $features->get_field('name');
          $project_slug                = $features->get_field('slug');
              $project_country             = $features->get_field('location.name');
          $project_projecttype    = $features->get_field('projecttype.name');
          $project_description    = $features->get_field('summarydesc');
          $project_mpowercode     = $features->get_field('mpowercode');
          $project_image                   = $features->get_field('projectimage');
          $project_fundinggoal     = $features->get_field('projectfundinggoal');
          $project_fundingtext     = $features->get_field('projectfundingtext');
          $project_featured                = $features->get_field('featuredproject');
          $project_large                   = $features->get_field('largeproject');

          // data cleanup
          $project_descriptiom = wpautop( $description );
          $project_image       = $project_image[0]['guid'];

        ?>

        <?php if($project_featured == 1 && $project_large == 1){?>
        <div class="featured-project">

        <img src="<?php echo $project_image; ?>" class="teaser" />

                        <div class="give"><a href="https://p2p.paperlesstrans.com/default.aspx?i=cmfms&c3=<?php echo urlencode($project_name); ?>&c4=<?php echo $project_mpowercode; ?>"><img src="<?php bloginfo('template_directory'); ?>/_img/btn.give.png" width="75" height="29" /></a></div>
                        <h2><a href="/partner/discover-great-projects/<?php echo $project_slug;?>"><?php echo $project_name; ?></a></h2>
                        <p><?php echo $project_description; ?></p>
                        <ul class="project-meta">
                        <li><strong>Country:</strong> <?php echo $project_country; ?></li>
                        <li><strong>Project Type:</strong> <?php
                        if(is_array($project_projecttype) && !empty($project_projecttype)) {
                            echo implode(', ',$project_projecttype);
                        }else{
                            echo $project_projecttype;
                        }
                        ?>
                        </li>
                        </ul>

                        <a href="/partner/discover-great-projects/<?php echo $project_slug;?>" class="fom">find out more&#8811;</a>

                        <div class="clr"></div>
                        <?php
                        if(strlen($project_fundinggoal)>0)
                        $goal= number_format($project_fundinggoal);
                        {
                        ?>
                                <div class="fund"><goal><?=$project_fundingtext?> <!--$<?=$goal?>--></goal></div>
                        <?php
                        }
                        ?>
                        <div class="clr"></div>

                        </div>
        <!--end  project -->   
        <?php } ?>

      <?php endwhile ?>
       </div><!-- end featured -->
    <?php endif ?>

answered Feb 2 at 3:03

senojeel

3

add comment
enter at least 15 characters
0

Here is your problem:

<?php if($project_featured == 1 && $project_large == 1){?>

Your query is returning 3 random projects from all the projects in your database, but your code only shows projects that have both the "featured" and "large" flags set. From the behavior of your site, only a small percentage of your projects have both these flags set. Hence, you get 0, 1 or 2 projects displayed.

Get rid of this if statement (and its closing brace) and change your query to the following:

$features->findRecords('RAND()', 3 't.featuredproject = 1 AND t.largeproject = 1');

Now, so long as you have enough of them in your database, you'll get three random projects that are both large and featured.

answered Feb 2 at 3:34

chris.pilko

889

add comment
enter at least 15 characters
0

That worked. There are only 4 in the database right now because it is still being populated. Thank you so much for your help.

answered Feb 2 at 4:54

senojeel

3

add comment
enter at least 15 characters