0

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>";
    }
    }
    ?>

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

halfer
  • 19,824
  • 17
  • 99
  • 186
hjaffer2001
  • 933
  • 6
  • 18
  • 38
  • 2
    [`MySQL`](http://php.net/manual/en/book.mysql.php) (`mysql_*` functions) extension is [***deprecated***](http://php.net/manual/en/function.mysql-connect.php). I suggest to use [`MySQLi`](http://php.net/manual/en/book.mysqli.php) (`mysqli_*` functions) or [`PDO`](http://php.net/manual/en/book.pdo.php) instead. – BlitZ May 14 '13 at 05:48
  • Oh noo.. Could you explain why you need to go with MySQLi or PDO? – hjaffer2001 May 14 '13 at 05:51
  • Follow [link](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – BlitZ May 14 '13 at 05:52
  • do you want to print 10 email on an interval 10 second????? – Ammar Hayder Khan May 14 '13 at 05:56
  • @aligarian - yes you are exactly right. help me out!!! – hjaffer2001 May 14 '13 at 05:57
  • @hjaffer2001 http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php/6381189#6381189 Thanks – rahularyansharma May 14 '13 at 06:09

5 Answers5

3

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)); 
Michael O'Brien
  • 401
  • 3
  • 6
  • In addition, I suggest to be aware of [`max_execution_time`](http://ru2.php.net/manual/en/info.configuration.php#ini.max-execution-time) setting of PHP. Check out [`set_time_limit()`](http://ru2.php.net/manual/en/function.set-time-limit.php). – BlitZ May 14 '13 at 06:02
  • Ah, yes, very good point. I figured that the 10 second pause wouldn't be an issue because PHP's default execution time is 30 seconds but there's no guarantee that is the case in this environment. – Michael O'Brien May 14 '13 at 06:06
  • Michael O'Brien - Thanks for your help. Your answer helped me a lot and i have updated my code. It works perfect. You are so awesome !!! – hjaffer2001 May 14 '13 at 10:07
1

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;
     }
   }
Devon Bernard
  • 2,250
  • 5
  • 19
  • 32
  • Please check my question again. I am not worried about the sleep. But i want to make a group and print the ten email ids first and so on.. – hjaffer2001 May 14 '13 at 06:26
1

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;
 }

}

Ammar Hayder Khan
  • 1,287
  • 4
  • 22
  • 49
  • Please check my question again. I am not worried about the sleep. But i want to make a group and print the ten email ids first and so on.. – hjaffer2001 May 14 '13 at 06:27
1

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.

Strae
  • 18,807
  • 29
  • 92
  • 131
0

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++;
}
Eugene
  • 1,899
  • 1
  • 13
  • 10