This code does a nice job reading the members from my database and setting up a form with checkboxes for each member in one long single-column list.

What I need to do is to write some sort of loop that sets out 3 or 4 columns and splits the names roughly among them. I know that it involves dividing the number of records by the number of columns and using the modulus, but I've been beating my head against a wall for days now trying to get it to actually work. None of the numerous code samples I've tried seems to work.

Can anyone help me out here?

It should be ordered vertically, not horizontally.

<form name="form1" method="post" action="postoffice-popup.php?action=custom_all_list"> 
  <table style="border:1px solid silver;">
  <tr>
    <td>
  <?php
  $result = mysql_query("SELECT id,fname,lname,email FROM members WHERE clubid = '1234' ORDER BY lname,fname");  

  // loop through the array, creating one row per array
  while ($thisrow = mysql_fetch_array($result)) {

    // create a variable for each attribute
    foreach($thisrow as $var => $value){
      $$var = $value;
    }

    $name   = "$lname, $fname";

    if ($thisrow["email"]) {
      $list = $list." $name <$email>; ";
    }

    echo "  <label for='$id'><input type='checkbox' name='members[]' value='$id' id='$id' /><legend for='$id'>$name</label><br />\n";
  }
  ?>
    </td>
  </tr>
  <tr>
    <td>
      <input type="submit" name="Submit" value="Submit" style="height:23px;font-weight:bold;padding-top:0px;">
    </td>
  </tr>
  </table>
 </form>
Member Avatar for diafol

You could use ul/li as opposed to a table. Give the list items:

ul#ulist{
 padding: 0;
 margin: 0;
 width: 320px; /*change this to allow the number of columns required*/
}

ul#ulist li{
  display: block;
  list-style: none;
  float: left;
  width: 100px;
  padding: 0;
  margin: 0 0 5px 0; 
}

you can also put <tr><td> ... </td></tr> in your foreach (each record will be a row in your table)

you can also put <tr><td> ... </td></tr> in your foreach (each record will be a row in your table)

Thanks Silviuks. Point well taken, however, in this case I don't want separate rows, just one row with multiple columns.

I wound up with a good solution which I will be posting tonight for the benefit of all.

You could build another table in your <td> ...

You could build another table in your <td> ...

The trick wasn't building the table but getting the code to know when to switch to another TD. Once I got that info, the whole thing came together perfectly.

I actually prefer to have as little table structure as possible, and a single table with one row and three columns was all that was needed.

I'll post the full code tonight.

Here's the full code of my solution, with which I had some help.

<?php $numCols = 3; ?>
<form name="form1" method="post" action="postoffice-popup.php">
  <table style="border:1px solid silver;" border="1">
    <tr>
<?php
$result = mysql_query("
    SELECT id,fname,lname,iname,email
    FROM members
    ORDER BY lname,fname
    ");

$numPerCol = ceil(mysql_num_rows($result) / $numCols);
// do this for each column
for($col = 1; $col <= $numCols; $col++) {
    echo "<td>";
   $counter = 0;
    // do this for each row.
   while($row = mysql_fetch_assoc($result)) {
        
        // create a variable for each attribute
        foreach($row as $var => $value){
            $$var = $value;
        }
        
        // capture and clean data from this record
        $email    = trim($email);
        $fname    = ($iname) ? $iname : $fname ;
        $name     = "$lname, $fname";

        // append this name/address to the list
        if ($thisrow["email"]) {
            $list .= " $name <$email>; ";
        }

        echo "<label for=\"$id\"><input type=\"checkbox\" name=\"members[]\" value=\"$id\" id=\"$id\" /><legend for=\"$id\">$name</label><br />\n";

      if(++$counter >= $numPerCol) {
            break;
        }
   } // end while
    echo "</td>";
} // end for
?>
        </td>
    </tr>
    <tr>
        <td colspan="<?php echo "$numCols"; ?>">
            <input type="submit" name="Submit" value="Submit" style="height:23px;font-weight:bold;padding-top:0px;">
        </td>
    </tr>
    </table>
</form>

Regarding the $iname variable, $iname is an optional informal name, like 'Bob' for 'Robert'. If there is a value for $iname in the db, that is used, otherwise $fname is used

Member Avatar for diafol

Ok - is it solved?

yep, forgot to mark it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.