我已经制作了一个php应用程序,该应用程序使用xmldom从html网站获取数据并将其存储在数据库中,然后检索数据并进行一些丰富的计算。并在给输出数据库后将其截断。
我使用while循环进行数据行管理。但是当数据行增加时会花费很多时间。那么加快应用程序速度的更快循环是什么?
xmldom东西浪费时间吗?
这是数据获取的代码
<?php
ini_set('mysql.connect_timeout', 3600);
ini_set('default_socket_timeout', 3600);
ini_set('max_execution_time', '3600');
include "connection.php";
$s9="TRUNCATE TABLE `tbl_share_cost_details`";
$q9=mysqli_query($conn,$s9) or die("ERRRO99:".mysqli_error($conn));
$start = microtime(true); // Time Calculation Start
//$s="SELECT * FROM `tbl_company_share` WHERE id>=1 AND id<=50";
$s="SELECT * FROM `tbl_company_share` WHERE `code_name` IN ( ".$sharename.")";
$q=mysqli_query($conn,$s) or die("ERRRO:".mysqli_error($conn));
$sql="INSERT INTO `tbl_share_cost_details`(`share_name`, `price`, `bid_price_qty`,`offer_price_qty`, `change_price`, `change_price_percentage`, `high_of_day`, `low_of_day`, `open_price`, `buy`, `sell`, `hold`, `added_datetime`) VALUES ";
while($row = mysqli_fetch_array($q)){
$company_name = $row['company_name'];
$code_name = $row['code_name'];
$nse_code = $row['nse_code'];
$has_nse_code = $row['has_nse_code'];
if($has_nse_code==1){
$url="http://www.moneycontrol.com/stocks/cptmarket/compsearchnew.php?search_data=&cid=&mbsearch_str=&topsearch_type=1&search_str=".$nse_code;
} else {
$url="http://www.moneycontrol.com/stocks/cptmarket/compsearchnew.php?search_data=&cid=&mbsearch_str=&topsearch_type=1&search_str=".$code_name;
}
//echo $url; die;
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXpath($doc);
$elements2 = $xpath->query('//span[@id="Nse_Prc_tick"]');
$elements33 = $xpath->query('//span[@class="gr_15 uparw_pc"]');
if($elements2->length > 0) {
$price = $elements2->item(0)->nodeValue;
} else {
$price ='';
}
if($elements33->length > 0) {
//echo 'xx'.$elements33->item(0)->nodeValue;
}
//Todays Changeable Price
$p=$doc->getElementById('n_changetext');
$priceStr = @$p->nodeValue;
$priceArr = explode("(",$priceStr);
$change_price = $priceArr[0];
$change_price_percentage = @rtrim($priceArr[1],")");
//Todays Bid PRICE (QTY.)
$p9=$doc->getElementById('n_bidprice_qty');
$priceStr = @$p9->nodeValue;
$priceArr = explode("(",$priceStr);
//echo "<br>"; echo $priceArr[0].'Bid PRICE';
//echo "<br>"; echo rtrim($priceArr[1],")").'Bid PRICE';
$bid_price_qty = @rtrim($priceArr[1],")");
//Todays Offer PRICE (Qty.)
$p99=$doc->getElementById('n_offerprice_qty');
$priceStr = @$p99->nodeValue;
$priceArr = explode("(",$priceStr);
//echo "<br>"; echo $priceArr[0].'Offer PRICE';
//echo "<br>"; echo rtrim($priceArr[1],")").'Offer PRICE';
$offer_price_qty = @rtrim($priceArr[1],")");
//Todays Open Price
$p11=$doc->getElementById('n_open');
$open_price = @$p11->nodeValue;
//Todays Lowest Price
$p12=$doc->getElementById('n_low_sh');
$low_of_day = @$p12->nodeValue;
//Todays Highest Price
$p13=$doc->getElementById('n_high_sh');
$high_of_day = @$p13->nodeValue;
//Getting BUY Percentage
$classname1="pl_txt";
$nodesss = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname1 ')]");
//echo "<pre>"; print_r($nodesss);
$buy = @$nodesss->item(0)->nodeValue;
//Getting SELL Percentage
$classname2="pl_txt rtxt";
$nodesss = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname2 ')]");
//echo "<pre>"; print_r($nodesss);
$sell = @$nodesss->item(0)->nodeValue;
//Getting HOLD Percentage
$classname3="pl_txt grytxt";
$nodesss = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname3 ')]");
//echo "<pre>"; print_r($nodesss);
$hold = @$nodesss->item(0)->nodeValue;
//===========START SHARE PRICE SHOWING SUCCESS & FAILIOR Code ON 01/10/2016==============================================
$ss="SELECT * FROM `tbl_buy_product_profit_loss` WHERE share_name='".$code_name."'";
$qq=mysqli_query($conn,$ss) or die("ERRRO21:".mysqli_error($conn));
if(mysqli_num_rows($qq)>0){
$Rsq = mysqli_fetch_array($qq);
$price_suggested_at = $Rsq['price_suggested_at'];
$increment_price_val = ($price_suggested_at +((0.3 * $price_suggested_at)/100)); // 0.3% Increment Value
$decrement_price_val = ($price_suggested_at -((0.4 * $price_suggested_at)/100)); // 0.4% Decrement Value
if($price>=$increment_price_val){
$msg='Success';
$cc=($price-$price_suggested_at);
$profit_percent = ($cc/$price_suggested_at)/100;
//===========START DATA INTO product_profit_loss TABLE======================================
$ins="INSERT INTO `tbl_buy_product_profit_loss` SET `share_name`='".$code_name."',`price_suggested_at`='".$price_suggested_at."',`datetime`='".date("Y-m-d H:i:s")."',`price_sold_at`='".$price."',`profit`='".$profit_percent."',`loss`='',`message`='".$msg."'";
$qq=mysqli_query($conn,$ins) or die("ERRRO22:".mysqli_error($conn));
//===========END DATA INTO product_profit_loss TABLE======================================
} else if($price<=$decrement_price_val){
$msg='Failure';
$cc=@($price_suggested_at-$price);
$loss_percent = ($cc/$price_suggested_at)/100;
if($price==''){ $price='0.00'; } else { $price=$price; }
//===========START DATA INTO product_profit_loss TABLE======================================
$ins="INSERT INTO `tbl_buy_product_profit_loss` SET `share_name`='".$code_name."',`price_suggested_at`='".$price_suggested_at."',`datetime`='".date("Y-m-d H:i:s")."',`price_sold_at`='".$price."',`profit`='0.00',`loss`='".$loss_percent."',`message`='".$msg."'";
//echo $ins; die;
$qq=mysqli_query($conn,$ins) or die("ERRRO23:".mysqli_error($conn));
//===========END DATA INTO product_profit_loss TABLE======================================
} else {
$msg='';
$profit_percent = '';
}
}
//===========END SHARE PRICE SHOWING SUCCESS & FAILIOR Code ON 01/10/2016=================================================
//echo "Naba"; die;
if($bid_price_qty==''){ $bid_price_qty='0.00'; } else { $bid_price_qty=$bid_price_qty; }
if($offer_price_qty==''){ $offer_price_qty='0.00'; } else { $offer_price_qty=$offer_price_qty; }
if($change_price_percentage==''){ $change_price_percentage='0.00'; } else { $change_price_percentage=$change_price_percentage; }
if($high_of_day==''){ $high_of_day='0.00'; } else { $high_of_day=$high_of_day; }
if($low_of_day==''){ $low_of_day='0.00'; } else { $low_of_day=$low_of_day; }
if($open_price==''){ $open_price='0.00'; } else { $open_price=$open_price; }
if($price==''){ $price='0.00'; } else { $price=$price; }
$sql .=" ('".$code_name."','".$price."','".$bid_price_qty."','".$offer_price_qty."','".$change_price."','".$change_price_percentage."','".$high_of_day."','".$low_of_day."','".$open_price."','".$buy."','".$sell."','".$hold."','".date("Y-m-d H:i:s")."'),";
}
//$sql .=" ('','','','','','','','','','','','','')";
$sql .="('xxx','0.00','0.00','0.00','','0.00','0.00','0.00','0.00','','','','".date("Y-m-d H:i:s")."')";
//echo $sql; die;
$q=mysqli_query($conn,$sql) or die("ERRRO11:".mysqli_error($conn));
//echo $time_elapsed_secs = microtime(true) - $start;
//echo "<br/>Record(s) Inserted into DB!";
?>
这是获取最终结果的代码
<?php
$where = " 1=1";
if(isset($_REQUEST['share_name']) && $_REQUEST['share_name']<>''){
$share_name = $_REQUEST['share_name'];
//$where .= " AND `share_name`='".$share_name."'";
$sharenameArr = explode(",",$_REQUEST['share_name']);
$sharename = '';
foreach($sharenameArr as $val){
$value = "'".$val."'";
$sharename = $sharename.",".$value;
}
$sharename = ltrim($sharename,",");
include "sharetrading-insert.php";
//echo $sharename;
$where .= " AND `share_name` IN ( ".$sharename.") ";
}
//$s="SELECT * FROM `tbl_share_cost_details` WHERE 1=1 ORDER BY change_price_percentage DESC LIMIT 0,100";
$s="SELECT * FROM `tbl_share_cost_details` WHERE $where ORDER BY change_price_percentage DESC";
//$s="SELECT * FROM `tbl_share_cost_details` WHERE id=115";
$q=mysqli_query($conn,$s) or die("ERRRO:".mysqli_error($conn));
$num = mysqli_num_rows($q);
$i=1;
?>
<?php
while($row = mysqli_fetch_array($q)){
?>
<tr>
<td align="center" valign="middle"><?php echo $i; ?></td>
<td align="center" valign="middle"><?php echo $row['share_name']; ?></td>
<td align="center" valign="middle"><?php echo $row['price']; ?></td>
<td align="center" valign="middle"><?php echo $row['change_price_percentage']; ?></td>
<td align="center" valign="middle"><?php echo $row['high_of_day']; ?></td>
<td align="center" valign="middle"><?php echo $row['low_of_day']; ?></td>
<td align="center" valign="middle"><b>Bid Qty</b>:<?php echo $row['bid_price_qty']; ?><br/>
<b>Offer Qty</b>:<?php echo $row['offer_price_qty']; ?><br/>
<b>Percentage</b>:<?php $xx=@(($row['bid_price_qty']/$row['offer_price_qty'])*100);
if($xx>0){ echo $xx; } else { echo '0'; }?>% <br/>
<b>Date</b>:<?php echo $row['added_datetime']; ?>
</td>
<td align="center" valign="middle"><?php echo $row['open_price']; ?></td>
<td>
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td width="81%"><span class="pl_bar brdwr" style="width:<?php echo $row['buy']; ?>;text-align:center;"><b><?php echo $row['buy']; ?></b></span></td>
<td width="19%">BUY<?php if($row['buy']<>''){?>( <span style="font-weight:bold; color:red;"><?php echo $row['buy']; ?></span>)<?php } ?></td>
</tr>
<tr>
<td><span class="pl_bar brdwr rd" style="width:<?php echo $row['sell']; ?>;text-align:center;"><b><?php echo $row['sell']; ?></b></span></td>
<td>SELL<?php if($row['sell']<>''){?>( <span style="font-weight:bold; color:red;"><?php echo $row['sell']; ?></span>)<?php } ?></td>
</tr>
<tr>
<td><span class="pl_bar hold" style="width:<?php echo $row['hold']; ?>;text-align:center;"><b><?php echo $row['hold']; ?></b></span></td>
<td>HOLD<?php if($row['hold']<>''){?>( <span style="font-weight:bold; color:red;"><?php echo $row['hold']; ?></span>)<?php } ?></td>
</tr>
</table>
</td>
<td align="center"><?php $b='0.75'; if($row['change_price_percentage']>$b && $xx>=500 && $row['buy']>=66){ echo '<span style="font-weight:bold; color:red;">BUY</span>'; }?></td>
</tr>
<?php $i++; } ?>