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 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 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