Making a row of columns insert files/fields from a database section based upon a checkbox, alternating order

4 posts by 2 authors in: Forums > CMS Builder
Last Post: February 13, 2014   (RSS)

By Codee - February 7, 2014

Hi,

Right now my client has a graphic row of rotating images pulling from her testimonials section, based upon male OR female for the entire row. So the output in the browser looks like, for female,

female1-graphic | female 2 graphic | female3 graphic | female4 graphic | female 5 graphic

then after 2 or 3 seconds another row replaces the row above, but with male records from the testimonials section and looks like

male1-graphic | male 2 graphic | male3 graphic | male4 graphic | male 5 graphic

then it continues to rotate in/out the same way continuously. [screenshot attached]

What the client wants this rotator to do is have each row display as girl-boy order so,

female-graphic | male  graphic | female graphic | male graphic | female  graphic

Here's the code I use for each single rotation (connects to an image rotator that is embedded in the header of the document that I got from dynamic drive a couple years ago)/

            <!--first set begin-->
            <div class="gallerycontent" subject="Featured Golf Coach Testimonials">

            <?php
              /* STEP 1: LOAD RECORDS - Copy this PHP code block to the TOP of your page BEFORE anything else. */
              require_once "/home/domainname/public_html/lifgAdmin/lib/viewer_functions.php";

              list($testimonialsRecords, $testimonialsmetaData) = getRecords(array(
                'tableName'   => 'testimonials',
                'where'             => " (featured = '1') AND (female = '1') ",
                'orderBy'     => 'RAND()',
                'limit'       => '5',
              ));
              $testimonialsRecord = @$testimonialsRecords[0]; // get first record

            ?>
         <!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
        <center><b><font size=3 face="arial" color="#ffffff">Testimonials from Linda's Golf Students</font></b></center>
            <center>
             <table bgcolor="#5A7BAD" border="0" bordercolor="#000000" cellpadding="0" cellspacing="0">
                <tr>
                    <td valign='bottom' >  <center>
                        <?php foreach ($testimonialsRecords as $record): ?>

                  <!-- STEP 2a: Display Uploads for field 'photo' (Paste this anywhere inside STEP2 to display uploads) -->
                <!-- Upload Fields: num, createdTime, tableName, fieldName, recordNum, preSaveTempId, filePath, filename, extension, thumbFilePath, isImage, hasThumbnail, urlPath, width, height, thumbUrlPath, thumbWidth, thumbHeight, info1, info2, info3, info4, info5 -->
                <?php foreach ($record['photo'] as $upload): ?>

                  <?php if ($upload['hasThumbnail']): ?>
                   <a href="<?php echo $record['_link'] ?>" target="_blank" border="0"> <img src="<?php echo $upload['thumbUrlPath3'] ?>" width="<?php echo $upload['thumbWidth3'] ?>" height="<?php echo $upload['thumbHeight3'] ?>" border="0" alt="" /></a/>
                            
                            <?php break; ?> 
                            
                  <?php elseif ($upload['isImage']): ?>
                <img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" border="0" alt="" /><br/>

                  <?php else: ?>
                    <a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/>

                  <?php endif ?>
                          <?php endforeach ?>
                  <!-- STEP2a: /Display Uploads -->

                       <?php endforeach; ?>

                      <!-- /STEP2: Display Records -->
                        </center>
                         </td>
                    </tr>
                </table>
                </center>
                <center><b><font size=2 face="arial" color="#ffffff">Click on the Thumbnail to Read What Each Says</font></b></center>
            
                        </div>
<!--first set end-->

and the next row obviously has the where statement changed to

'where'             => " (featured = '1') AND (male = '1') ",

So, I don't know how to get the graphics for the testimonials to insert and display on the visual row in female-male order.

Any and all help is appreciated.

By Dave - February 12, 2014

Hi Terry,

So what would the plain HTML for something like this be?  There's where I always start.  I figure out what the hard-coded HTML would be and then figure out how to automate it with CMSB.  

Is it a list of 5 female thumb links and then 5 male thumb links?  And then does it repeat those back and forth or does it show 5 more females, etc? And if so, what happens when we run out of records or have more of one gender than the other?

I'm thinking the easiest would be to show a big list of links grouped by 5 of each gender until we have less than 5 of each gender and then stop.  Would that work? 

Dave Edis - Senior Developer
interactivetools.com

By Dave - February 13, 2014

Hi Terry,

The code in either MySQL or PHP for that is really tricky.  A search for "mysql sort by alternating male and female" shows some pretty complex solutions:

And the PHP solutions aren't much easier (search terms: PHP array, alternating, interleave, male female).  However, I found a function here that I believe does what you want: 

Here's some example code: 

function array_zip_merge() {
  $output = array();
  // The loop incrementer takes each array out of the loop as it gets emptied by array_shift().
  for ($args = func_get_args(); count($args); $args = array_filter($args)) {
    // &$arg allows array_shift() to change the original.
    foreach ($args as &$arg) {
      $output[] = array_shift($arg);
    }
  }
  return $output;
}

$arrayM = array('m1','m2','m3','m4','m5','m6','m7','m8');
$arrayF = array('f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','f11');
$alternating = array_zip_merge($arrayM, $arrayF);  // outputs: m1, f1, m2, f2, m3, f3, m4, f4, m5, f5, m6, f6, m7, f7, m8, f8, f9, f10, f11
exit;

So if you had an array of male records and an array of female records, you could merge them like that and loop over them for alternating results.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com