/    Sign up×
Community /Pin to ProfileBookmark

Outputting data in multiple columns

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.

[code=php]<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”;

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>[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJun 06.2010 — In general, one way to do it is:

1) Decide how many columns you want.

2) Calculate how many records per column using the total number of records.

3) For each column, use the sql limit command to extract just the records for that column and display them.
Copy linkTweet thisAlerts:
@NogDogJun 06.2010 — You can do one query, use mysql_num_rows() to get the number of results, then divide by the number of columns. Something like...
[code=php]
$numCols = 3;
$result = mysql_query($sql);
$numPerCol = ceil(mysql_num_rows($result) / $numCols);
echo "<div id='column_list'>n";
for($col = 1; $col <= $numCols; $col++)
{
echo "<ul class='column'>";
$counter = 0;
while($row = mysql_fetch_assoc($result))
{
echo "<li>".$row['foo']."</li>n";
if(++$counter >= $numPerCol) { break; }
}
echo "</ul>n";
}
echo "</div>";
[/code]
Copy linkTweet thisAlerts:
@scaiferwauthorJun 07.2010 — Brilliant, thank you. I can now find something else to stress about.

Here's the full working code, for any who want it:
[code=php]<?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
WHERE clubid = '$clubno'
AND status = 'current'
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>[/code]

Regarding the [B][COLOR="DarkRed"]$iname[/COLOR][/B] variable, iname is an optional informal name, like 'Bob' for 'Robert'. If there is a value for [B][COLOR="DarkRed"]$iname[/COLOR][/B] in the db, that is used, otherwise [B][COLOR="DarkRed"]$fname[/COLOR][/B] is used.
×

Success!

Help @scaiferw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...