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 Codee - February 13, 2014

the html code above doesn't state?  Right now it shows 1 row, 5 photos/links, either all female, or all male...and it rotates the single row every 2 seconds or so. So we "see" in the browser 5 female, then 5 male, then 5 female, then 5 male, etc.  It's easy and has been working that way for couple years.  What she wants it to do is show in 1 row at a time: female | male | female | male | female...so basically every 1 row shows as girl-boy-girl-boy-girl ?  Does that make more sense?

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