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

Group1: ---first ten email ids---

Group2: ---next ten email ids--- . . . . ..

Group3: ---last ten email ids---

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

    <?php
    $con=mysql_connect("localhost","root","admin");
    mysql_select_db("test1",$con);
    $sel=mysql_query("SELECT DISTINCT emailaddress FROM userlist");
    while($row=mysql_fetch_array($sel))
    {
    $mail[]=$row['emailaddress'];
    }


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


    for($i=0;$i<$get_chunk_count;$i++){
    echo "Group :".$i;
    echo "<br>";
    echo "========";
    echo "<br>";
    $count_inside_count = count($chunk[$i]);

    for($j=0;$j<=$count_inside_count;$j++){
    echo "<pre>";
    echo $chunk[$i][$j];
    echo "</pre>";
    }
    }
    ?>

Could someone help me with the above code

Thanks Haan

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

Answers


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;
do
{
    echo $chunk[$i] . '<br />';
    $i++;
    if($i == 10)
        sleep(10);
} while ($i < count($chunk)); 

I am not sure why you want to break them into groups when you can just print them into separate chunks; but if I understand your goal correctly by using this php sleep method you should be able to print 10 IDs wait 10 seconds then print the next 10 Ids etc. until your full list is exhausted.

   $temp = 0;
   for($i=1;$i<=$no_of_emails;$i++){
     echo "stuff";
     temp++;
     if(temp == 10){
       sleep(10);  
       temp = 0;
     }
   }

If i am understand correctly --the following code will work for you...

$con=mysql_connect("localhost","root","admin");
mysql_select_db("test1",$con);
$sel=mysql_query("SELECT DISTINCT emailaddress FROM userlist");
$email=mysql_fetch_array($sel);
 $num_rows=mysql_num_rows($sel);
$counter = 0;
for($i=1;$i<=$num_rows;$i++){
 echo $email[$i];
 $counter++;
 if($counter== 10){
   sleep(10);  
   $counter= 0;
 }

}


Is not clear how an why you want to split the emails list in stack of ten, btw this is a working code (as someone pointed out, you really should consider PDO over mysql_* functions, so i wrote the example with both):

// Mysql, DEPRECATED.
//$q = mysql_connect('localhost', 'YOURDBUSER', 'YOURDBPASSWD');
//mysql_select_db('YOURDBNAME');
//$query = mysql_query('SELECT DISTINCT emailaddress FROM userlist');
//$n = mysql_num_rows($query);

// PDO
$dblink = new PDO('mysql:dbname=YOURDBNAME;host=localhost', 'YOURDBUSER', 'YOURDBPASSWD');
$query = $dblink->query("SELECT DISTINCT emailaddress FROM userlist");
$n = $query->rowCount();


$step = 10;
for($i = 0; $i < $n; $i++)
{
  //  Mysql
  //$res = mysql_fetch_assoc($query);
  // Pdo
  $res = $query->fetch(PDO::FETCH_ASSOC);

  if(!($i % $step))
  {
    // Here we have collected 10 email addresses.
    echo "---SEPARATOR---\n";
  }
  echo $res['emailaddress'] . "\n";
}

EDIT To all whom suggested sleep: it wont work. Sleep is not supposted for 'print 10 emails every 10 seconds': the page output reach the browser after the php code has been executed (server-side)

If you want to achieve that effect, you should play hard with output buffering and im not sure is possible anyway.

Ajax, maybe.


You may try this code:

$con=mysql_connect("localhost","root","admin");
mysql_select_db("test1",$con);
$sel=mysql_query("SELECT DISTINCT emailaddress FROM userlist");
$mailsGroups = array();
$i = 0;
$j = -1;
$mailsPerGroup = 10;

while($row=mysql_fetch_array($sel))
{
    if ($i % $mailsPerGroup == 0) {
        $j++;
        $mailsGroups[$j] = array();
    }
    $mailsGroups[$j][]=$row['emailaddress'];
    $i++;
}

Need Your Help

Secure Search in android application

android security search

Alright guys, here's what I'm trying to do.

Git gui and command line - hook handling error

git version-control mercurial dvcs

I am facing the different kind of situation in using Git Hooks. I have created the pre-commit file in hook directory to increment my version number on every commit. I have no problem in hook as it

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.