Executar script várias vezes sem while
Bom pessoal, eu tenho o seguinte código que insere dados aleatórios no banco de dados (MySql). Este Script deve inserir vários dados aleatórios, então eu optei pela função while, porém demora muito, têm algum alternativo de como eu posso fazer isso de maneira rápida e sem travar? Desde já agradeço :) Segue o meu código:
<?php
function randquote()
{
$quotes= array(
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"AB",
"BC",
"CD",
"DE",
"EF",
"FG",
"GH",
"HI",
"IJ",
"JK",
"KL",
"LM",
"MN",
"NO",
"OP",
"PQ",
"QR",
"RS",
"ST",
"TU",
"UV",
"VW",
"WX",
"XY",
"YZ",
"ZA",
);
return $quotes[rand(0, count($quotes)-1)];
}
function flLevel()
{
$flLevel = array(
"FL030",
"FL025",
"FL017",
"FL033",
);
return $flLevel[rand(0, count($flLevel)-1)];
}
function flightType()
{
$flightType = array(
"P",
"A",
"K",
);
return $flightType[rand(0, count($flLevel)-1)];
}
function deptime()
{
$deptime = array(
"00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00",
);
return $deptime[rand(0, count($deptime)-1)];
}
function float_min($num)
{
$num = number_format($num,2);
$num_temp = explode('.', $num);
$num_temp[1] = $num-(number_format($num_temp[0],2));
$saida = number_format(((($num_temp[1]) * 60 / 100)+$num_temp[0]),2);
$saida = strtr($saida,'.',':');
return $saida;
}
$i = 1;
while( $i <= 3 ){
$numAleat = rand(2, 7);
srand((float) microtime() * 10000000);
$input = array("1", "2", "3", "4", "5", "6", "7",);
$rand_keys = array_rand($input, $numAleat);
$flDaysOfweek = $input[$rand_keys[0]];
$flDaysOfweek .= $input[$rand_keys[1]];
$flDaysOfweek .= $input[$rand_keys[2]];
$flDaysOfweek .= $input[$rand_keys[3]];
$flDaysOfweek .= $input[$rand_keys[4]];
$flDaysOfweek .= $input[$rand_keys[5]];
$flDaysOfweek .= $input[$rand_keys[6]];
//Executa a consulta
$sql = "SELECT * FROM phpvms_airports ORDER BY RAND() LIMIT 1";
$res = mysql_query($sql);
//Exibe as linhas encontradas na consulta
while ($row = mysql_fetch_array($res))
{
$fldepicao = $row['icao'];
}
//Executa a consulta
$sql = "SELECT * FROM phpvms_airports ORDER BY RAND() LIMIT 1";
$res = mysql_query($sql);
//Exibe as linhas encontradas na consulta
while ($row = mysql_fetch_array($res))
{
$flarriacao = $row['icao'];
}
//Executa a consulta
$sql = "SELECT * FROM phpvms_aircraft ORDER BY RAND() LIMIT 1";
$res = mysql_query($sql);
//Exibe as linhas encontradas na consulta
while ($row = mysql_fetch_array($res))
{
$flaircraft = $row['id'];
}
$pricedist = OperationsData::getAirportDistance($flarriacao, $flaircraft);
$price = number_format($pricedist/100 * 10);
$code = "MLA";
$flightnum .= randquote();
$flightnum = rand(3,9999);
$distdepicao = $fldepicao;
$distarriacao = $flarriacao;
$route = "DCT";
$routedetails = "";
$dpicao = $fldepicao;
$arricao = $flarriacao;
$aircraft = $flaircraft;
$fllevel = flLevel();
$distance = OperationsData::getAirportDistance($distdepicao, $distarriacao);
$deptime = deptime();
$arrtime = $flarrtime;
$flightTime = $flFlightTime;
$daysofweek = $flDaysOfweek;
$price = $price;
$flighttype = flightType();
$intn = '500';
$fltime = $distance/$intn;
$total = $deptime + $fltime;
$result = $total;
$totalTime = $total;
$h24 = '24';
$enabled = '1';
if($result >= $h24)
{
$result2 = $result - $h24;
$result3 = float_min($result2);
}else{
$result3 = float_min($result);
}
mysql_query("INSERT INTO `phpvms_schedules`(
`id`,
`code`,
`flightnum`,
`depicao`,
`arricao`,
`route`,
`route_details`,
`aircraft`,
`flightlevel`,
`distance`,
`deptime`,
`arrtime`,
`flighttime`,
`daysofweek`,
`price`,
`flighttype`,
`timesflown`,
`notes`,
`enabled`,
`bidid`,
`week1`,
`week2`,
`week3`,
`week4`,
`payforflight`
)
VALUES
(
'',
'$code',
'$flightnum',
'$distdepicao',
'$distarriacao',
'$route',
'a:0:{}',
'$aircraft',
'$fllevel',
'$distance',
'$deptime',
'$result3',
'$fltime',
'$daysofweek',
'$price',
'$flighttype',
'',
'',
'$enabled',
'0',
'',
'',
'',
'',
'$price'
)"
);
}Discussão (2)
Carregando comentários...