Hi All,
I have a table 'names' with a column 'name'.
Using a SELECT statement I've got 10,000 randomized names.

I'd like to put them into groups of 3 and have the 1 remaining name available to do something with later.

Can anybody help with the code for sorting these results into groups please?

Regards

Taffd

Recommended Answers

All 5 Replies

what do you mean groups? you need to go into more detail.

kkeith29,
I have 10,000 names. I wish to put them into groups of 3 names. That is 3,333 groups with 3 names in each.
There will be 1 name left over.

In the future, each group of 3 will select 1 name to represent their group.

I will thus have 3,334 names(the remaining 1 from the first level will be added)

I will then generate further groups of 3, along with any remainder.

I will continue this process, until I arrive at a number of names between about 25 and 200, depending on what I want the final group to consist of.

The code I'm after is to divide the 10,000 into groups of 3 and give me the remainder.

Taffd

how do you want the results. tabular or array ???????

Hi again kkeith,
I'm not sure what you mean by array but it's academic.
This'll be a 'behind the scenes' thing. To give you the full picture:-

This is a method of choosing leaders from an imaginary population, where the governence is largely web-based.
At the first level the electorate is split into groups of 3, who each choose a rep. These reps are again put into groups of 3 and again choose a rep. The process continues until I've got a final 'council' of say, 28 or thereabouts.
I'd envisaged a scenario where the computer selected random groups of 3, who would then be emailed to explain their groups and the closing date for choosing their rep.
The method of selecting the groups is what I'm after. Displaying any results, I can fiddle around with.

Taffd

I am not sure if this is what you are after. This is pretty complicated. You need some way of saving the groups

<?php

$host = ''; //Host
$user = ''; //Mysql Username
$pass = ''; //Mysql Password
$db   = ''; //Database Name

$con  = mysql_connect($host,$user,$pass) or die('Error: Could not connect');
mysql_select_db($db);

$sql = "SELECT * FROM `names`";
$query = mysql_query($sql, $con);
$num = mysql_num_rows($query);
if ($num > 0) {
	$names = array();
	$i = 1;
	while ($row = mysql_fetch_assoc($query)) {
		if ($i == 3) {
			//process $names array here, maybe insert into mysql table to save groups
			$names = array();
			$i = 1;
		}
		$names[] = $row['name'];
	$i++;
	}
}

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