hjaffer2001 hjaffer2001 -4 years ago 58
MySQL Question

PHP Looping and foreach

I will explain my problem with a single example.

  1. I have a set of email ids (fetch from db and it will be dynamic). Let us say there are 100 email ids

  2. I need to group them with a count of 10. In other words, 100 email ids divided by 10 => So there will be 10 loops.

  3. The output should look like

---first ten email ids---

---next ten email ids---
. . . . ..

---last ten email ids---

Here is my php code(i have revised/corrected my code)

$sel=mysql_query("SELECT DISTINCT emailaddress FROM userlist");

$chunk = array_chunk($mail, 10);
$get_chunk_count = count($chunk);

echo "Group :".$i;
echo "<br>";
echo "========";
echo "<br>";
$count_inside_count = count($chunk[$i]);

echo "<pre>";
echo $chunk[$i][$j];
echo "</pre>";

EDITED: The above code works fine, and i have edited. Thanks for all the help :)

Answer Source

You could make use of PHP's array_chunk() function to break your array up into smaller chunks.

Not tested but it should be pretty close I think.

$chunk = array_chunk($mail, 10);
$i = 0;
    echo $chunk[$i] . '<br />';
    if($i == 10)
} while ($i < count($chunk)); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download