At 09:22 PM 8/10/2011, 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);

while ($num != "10000") {
    while($row = mysql_fetch_assoc($result)) {
        $padnum = number_pad($num, "4");
echo $row['areacode'] . "-" . $row['prefix'] . "-" . $padnum . "<BR>";
        $num++;
    }


}

?>

Try to avoid putting a database query in a loop. In the query only ask for the fields you are going to use, it's faster. Why use your own function to pad a string, when there is a built-in function?

Try this code (untested):
<?php
$nums = range(0,9999);
$q = "SELECT areacode, prefix FROM Test WHERE `areacode` = '907' AND `prefix` = '200' LIMIT 5";
$rs = mysql_query($q);
while ($row = mysql_fetch_assoc($rs)) {
        foreach ($nums as $n) {
echo sprintf('%03d-%03d-%04d',$row['areacode'],$row['prefix'],$n) "<br>\n";
        }
}
?>

Ken

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

Reply via email to