On 8/10/2011 6:22 PM, Jason Pruim wrote:
> So here I am attempting to generate some numbers to be inserted into a 
> database... eventually they will make up a phone number (Which I've emailed 
> about before and know about the bad ideas with it.. But it's the customer :)) 
> 
> Here is the code I am working with:
> 
> <?PHP
> function number_pad($number,$n) {
> return str_pad((int) $number,$n,"0",STR_PAD_LEFT);
> }
> 
> $SQL = "SELECT * FROM Test WHERE `areacode` = '907' AND `prefix` = '200' 
> LIMIT 5";
> 
> $result = mysql_query($SQL);
> //$num = "0000";
> //foreach ($result as $key => $value) {
> //    echo $key . "-" . $value . "-" . number_pad($num, "4") . "<BR>";
> //    $num++;
> //}
> 
> while ($num != "10000") {
>     while($row = mysql_fetch_assoc($result)) {
>         $padnum = number_pad($num, "4");
>         echo $row['areacode'] . "-" . $row['prefix'] . "-" . $padnum . "<BR>";
>         $num++;
>     }
>     
> 
> }
> 
> ?>
> 
> basically all I'm trying to do is generate the last 4 digits starting at 0000 
> and going up to 9999. for testing purposes I'm just echoing back but will 
> eventually insert the complete number back into the database as a 7 digit 
> string.
> 
> The error I'm getting is:
> 
> Fatal error: Maximum execution time of 30 seconds exceeded in 
> /home/pruimpho/public_html/jason/dev/clients/flewid/Phone/config/phoneareacodes.php
>  on line25
> 
> which is where the second while starts...
> 
> Does anyone know a better way to do this? 
> 
> I'm off to finish searching google while waiting for some good news :)
> 
> Thanks Everyone!
> 
> 
> Jason Pruim
> pru...@gmail.com
> 
> 
> 

Jason,

Here is my rendition of what you should do.

<?PHP

$SQL = "SELECT  areacode, prefix
        FROM    Test
        WHERE   `areacode` = '907'
        AND     `prefix` = '200'";

$result = mysql_query($SQL);

$values = '';

while( $row = mysql_fetch_assoc($result) ) {
  foreach ( range(0, 9999) AS $n ) {
    $values .= $row['areacode'] . '-' . $row['prefix'] . '-' .
               str_pad($n, 4, '0', STR_PAD_LEFT);
  }
}

echo $values;

?>

It will be much faster if you build the complete string in memory then echo out
the built string.

Jim Lucas

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to