-2
好吧,基本上我設置了一個查詢來連接到兩個表並獲取所有信息,然後使用幾個循環來顯示我的網頁的某個部分。在緩慢的例子是在這裏:http://sage-scape.com/hiscores/compare.ws?user1=kyle 現在我的代碼有點大,但這可能是問題,我將兩個函數結合在一起,因爲我認爲這會加快過程,它沒有幫助太好。代碼如下:如何優化此代碼?
function compare($user1) {
include "mysql.ws";
$user1 = str_replace(" ", "_", $user1);
$query = mysql_query("SELECT a.*, b.* FROM skills a JOIN activities b ON(a.playerName LIKE b.playerName) WHERE a.playerName LIKE '".fixName($user1)."' LIMIT 1") or die(mysql_error());
$row = mysql_fetch_array($query);
$overall['lvl'] = 0;
$overall['xp'] = 0;
for ($i = 1; $i <=25; $i++){
$xptype = findType($i)."xp";
$overall["lvl"] += getLevelForXP($row[$xptype]);
$overall["xp"] += $row[$xptype];
}
//updateDonator($user1);
echo'<div class="OrnamentalBoxBg"><div class="OrnamentalBoxContent">
<div class="header">
<div class="left">
<span class="columnLevel">Level</span>
<span class="columnTotal">Total XP</span>
<span class="columnRank">Rank</span>
</div></div>
<div class="row" data-skill="combat" data-member="false">
<div class="left">
<a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&table=0&rank='.findRank($user1,"0").'#'.findRank($user1,"0").'">
<span class="columnLevel">
'.dots($overall["lvl"]).'
</span>
</a>
<a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&table=0&rank='.findRank($user1,"0").'#'.findRank($user1,"0").'">
<span class="columnTotal">
'.dots($overall["xp"]).'
</span>
</a>
<a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&table=0&rank='.findRank($user1,"0").'#'.findRank($user1,"0").'">
<span class="columnRank">
'.findRank($user1,"0").'
</span>
</a>
</div>
<div class="middle">
<img src="http://sage-scape.com/hiscores/img/overall.png?2" alt="" title="">
<span class="statName"><span><span><span>Overall</span></span></span></span>
</div>
</div>
';
for ($i = 1; $i <=25; $i++){
$xptype = findType($i)."xp";
if($i == 25){
echo'
<div class="row last-row" data-skill="'.findSkill($i).'" data-member="'.member($i).'"> ';
} else {
echo'
<div class="row" data-skill="'.findSkill($i).'" data-member="'.member($i).'"> ';
}
echo'
<div class="left">
<a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&table='.$i.'&rank='.findRank($user1,"$i").'#'.findRank($user1,"$i").'">
<span class="columnLevel">';
if($i == 25){
echo'
'.$row["Dungeoneeringlvl"].'
</span>';
}else{
echo'
'.getLevelForXP($row[$xptype],"$i").'
</span>';
}
echo'
</a>
<a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&table='.$i.'&rank='.findRank($user1,"$i").'#'.findRank($user1,"$i").'">
<span class="columnTotal">
'.dots($row["$xptype"],"$i").'
</span>
</a>
<a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&table='.$i.'&rank='.findRank($user1,"$i").'#'.findRank($user1,"$i").'">
<span class="columnRank">
'.findRank($user1,"$i").'
</span>
</a>
</div>
<div class="middle">
<img src="http://sage-scape.com/hiscores/img/xp_'.fixName(findType($i)).'_total.png?2" alt="" title="" />
<span class="statName"><span><span><span>'.findType($i).'</span></span></span></span>
</div>
</div>
';
}
echo'
</div></div>
<div class="OrnamentalBoxBottom"></div>
</div>
<div class="OrnamentalBox Width923 SegmentTwo" id="activityStats">
<div class="OrnamentalBoxTop"></div>
<div class="OrnamentalBoxTitle"><div class="Centre1"><div class="Centre2">
<h3 class="Gradient NoFlourish Centre"><span class="spacing" aria-hidden="true">Activity Stats</span>
<span class="G0">Activity Stats</span>
<span class="G1" aria-hidden="true">Activity Stats</span>
<span class="G2" aria-hidden="true">Activity Stats</span>
<span class="G3" aria-hidden="true">Activity Stats</span>
<span class="G4" aria-hidden="true">Activity Stats</span>
<span class="G5" aria-hidden="true">Activity Stats</span>
<span class="G6" aria-hidden="true">Activity Stats</span>
</h3></div></div></div>
<div class="FlatHeader playerNames">
<div class="left">
'.BBCode($user1).'</div>';
if(!empty($_GET["user2"])){
echo'
<div class="right">
'.BBCode($_GET["user2"]).'
</div>
';
}
echo'
</div>
<div class="playerAvatars">
<div class="left">
<img src="http://services.runescape.com/m=avatar-rs/'.$user11.'/chat.png?cachebust=1342804665991" alt="" title="">
</div>';
if(!empty($_GET["user2"])){
echo'
<div class="right">
<img src="http://services.runescape.com/m=avatar-rs/'.$_GET["user2"].'/chat.png?cachebust=1342804665991" alt="" title="">
</div>
';
}
echo'
</div>';
echo'
<div class="OrnamentalBoxBg"><div class="OrnamentalBoxContent">
<div class="header">
<div class="left">
<span class="columnScore">Score</span>
<span class="columnRank">Rank</span>
</div>
</div>';
for ($i = 0; $i < 7; $i++){
$xptype = findActivityP($i);
if($i == 6){
echo'
<div class="row last-row">';
} else {
echo'
<div class="row">';
}
echo'
<div class="left">
<a href="http://sage-scape.com/hiscores/overall.ws?category_type=1&table='.$i.'&rank='.findARank($user1,"$i").'#'.findARank($user1,"$i").'">
<span class="columnScore">
'.$row["$xptype"].'
</span>
</a>
<a href="http://sage-scape.com/hiscores/overall.ws?category_type=1&table='.$i.'&rank='.findARank($user1,"$i").'#'.findARank($user1,"$i").'">
<span class="columnRank">
'.findARank($user1,"$i").'
</span>
</a>
</div>
<div class="middle">
<img src="http://sage-scape.com/hiscores/img/'.findActivityP($i).'.png" alt="" title="">
<span class="statName"><span><span><span>'.findActivity($i).'</span></span></span></span>
</div>
</div>
';
}
}
技能表包含51列playerName和25種不同的xp和lvl技能。活動包含8列用戶和一些活動。我有幾個循環,所以代碼不是太大,但有人可以幫我優化這個,所以它會加載更快?
查找重構。通常你想分解成更小的代碼段不大。此外,我會建議在代碼中的不同位置打印出時間以確定時間的使用位置。這將幫助你調試。我不認爲你會發現有太多的人會爲你調試它。所以除非這是一個明顯的錯誤,否則你可能不會得到太多的答案。 – Codeguy007
可能重複的[PHP文件無法輸入部分代碼](http://stackoverflow.com/questions/11575531/php-file-cannot-enter-some-part-of-code) - 您的問題可能是非法的,主題,重複的建議只是一個類似的問題的鏈接,可能或不會幫助你解決你的問題。不過,這些問題最好在http://codereview.stackexchange.com/上提問。 – hakre
如何計算代碼? –