2015-11-07 85 views
0

我的php函數只能檢索和迴應一條信息。由於我添加了30個限制,因此應該使用不同的信息集回顯30次。MySQL查詢只循環一次

我想編輯的網頁可在以下幾點: http://rocketleaguelobby.com/?key=65d9c17e957870ee15161959b2c1dedb&p=/matches

My PHP Code:

<?php 
// Connect to the database // 
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/dbconnect.php'); 

// Send the Query // 
$query = "SELECT * FROM `bets` WHERE `public` = '1' ORDER BY `betTime` LIMIT 0 , 30"; 
$result = mysqli_query($connection, $query); 

if (mysqli_num_rows($result) == 0) { 
    // If no results were found // 
    echo '<span style="font-style: italic;opacity: 0.8;">No Bet History Found</span>'; 
} else { 
    // Echo each bet history found // 
    while($bet = mysqli_fetch_assoc($result)) { 
     // For every bet, get info about the user // 
     $query = "SELECT `steamName`, `steamAvatar` FROM `users` WHERE `steamID` = '".$bet['steamID']."' LIMIT 1"; 
     $result = mysqli_query($connection, $query); 
     while ($user = mysqli_fetch_assoc($result)) { 
      $query = "SELECT `teamShort` FROM `teams` WHERE `id` = '".$bet['betTeam']."' LIMIT 1"; 
      $result = mysqli_query($connection, $query); 
      $betTeam = mysqli_fetch_assoc($result); 
      $timeAgo = time() - $bet['betTime']; 
      if ($timeAgo < 60) { 
       // Seconds // 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Seconds Ago"; 
       } else { 
        $timeAgo .= " Second Ago"; 
       }; 
      } elseif ($timeAgo >= 60 & $timeAgo < 3600) { 
       // Minutes // 
       $timeAgo = round($timeAgo/60); 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Minutes Ago"; 
       } else { 
        $timeAgo .= " Minute Ago"; 
       }; 
      } elseif ($timeAgo >= 3600 & $timeAgo < 86400) { 
       // Hours // 
       $timeAgo = $timeAgo/60; 
       $timeAgo = round($timeAgo/60); 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Hours Ago"; 
       } else { 
        $timeAgo .= " Hour Ago"; 
       }; 
      } elseif ($timeAgo >= 86400 & $timeAgo < 604800) { 
       // Days // 
       $timeAgo = $timeAgo/60; 
       $timeAgo = $timeAgo/60; 
       $timeAgo = round($timeAgo/24); 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Days Ago"; 
       } else { 
        $timeAgo .= " Day Ago"; 
       }; 
      } elseif ($timeAgo >= 604800 & $timeAgo < 2628000) { 
       // Weeks // 
       $timeAgo = $timeAgo/60; 
       $timeAgo = $timeAgo/60; 
       $timeAgo = $timeAgo/24; 
       $timeAgo = round($timeAgo/7); 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Weeks Ago"; 
       } else { 
        $timeAgo .= " Week Ago"; 
       }; 
      } elseif ($timeAgo >= 2628000) { 
       // Months // 
       $timeAgo = $timeAgo/60; 
       $timeAgo = $timeAgo/60; 
       $timeAgo = $timeAgo/24; 
       $timeAgo = $timeAgo/365; 
       $timeAgo = round($timeAgo * 12); 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Months Ago"; 
       } else { 
        $timeAgo .= " Month Ago"; 
       }; 
      }; 
      echo '<div class="feed-post"><a href="http://steamcommunity.com/profiles/'.$bet['steamID'].'"><div class="steam-img" style="background-image: url('.$user['steamAvatar'].');"></div><span class="steam-name" title="'.$user['steamName'].'">'.$user['steamName'].'</span></a> bet <span class="bet-amount">$'.$bet['betAmount'].'</span> on <span class="bet-on">'.$betTeam['teamShort'].'</span><br><span class="bet-time">'.$timeAgo.'</span></div>'; 
     }; 
    }; 
}; 
mysqli_close($connection); 
?> 

我的JavaScript/jQuery代碼:

$("#betHistory").load('/inc/getBets.php'); 

我試圖讓之前30次投注,您應該能夠在我發送的我的網站鏈接上看到。

現在它只回聲1先前下注在數據庫中有多個投注已經。

+0

把你的PHP代碼在這裏,我們可以看到它而不去其他網站 – Terminus

回答

0

對於每個查詢,您確實使用了$結果。通過爲每個查詢使用單獨的變量,您的問題將得到解決。

我更新你根據是代碼:

<?php 
// Connect to the database // 
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/dbconnect.php'); 

// Send the Query // 
$query = "SELECT * FROM `bets` WHERE `public` = '1' ORDER BY `betTime` LIMIT 0 , 30"; 
$betsResult = mysqli_query($connection, $query); 

if (mysqli_num_rows($betsResult) == 0) { 
    // If no results were found // 
    echo '<span style="font-style: italic;opacity: 0.8;">No Bet History Found</span>'; 
} else { 
    // Echo each bet history found // 
    while($bet = mysqli_fetch_assoc($betsResult)) { 
     // For every bet, get info about the user // 
     $query = "SELECT `steamName`, `steamAvatar` FROM `users` WHERE `steamID` = '".$bet['steamID']."' LIMIT 1"; 
     $usersResult = mysqli_query($connection, $query); 
     while ($user = mysqli_fetch_assoc($usersResult)) { 
      $query = "SELECT `teamShort` FROM `teams` WHERE `id` = '".$bet['betTeam']."' LIMIT 1"; 
      $teamsResult = mysqli_query($connection, $query); 
      $betTeam = mysqli_fetch_assoc($teamsResult); 
      $timeAgo = time() - $bet['betTime']; 
      if ($timeAgo < 60) { 
       // Seconds // 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Seconds Ago"; 
       } else { 
        $timeAgo .= " Second Ago"; 
       }; 
      } elseif ($timeAgo >= 60 & $timeAgo < 3600) { 
       // Minutes // 
       $timeAgo = round($timeAgo/60); 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Minutes Ago"; 
       } else { 
        $timeAgo .= " Minute Ago"; 
       }; 
      } elseif ($timeAgo >= 3600 & $timeAgo < 86400) { 
       // Hours // 
       $timeAgo = $timeAgo/60; 
       $timeAgo = round($timeAgo/60); 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Hours Ago"; 
       } else { 
        $timeAgo .= " Hour Ago"; 
       }; 
      } elseif ($timeAgo >= 86400 & $timeAgo < 604800) { 
       // Days // 
       $timeAgo = $timeAgo/60; 
       $timeAgo = $timeAgo/60; 
       $timeAgo = round($timeAgo/24); 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Days Ago"; 
       } else { 
        $timeAgo .= " Day Ago"; 
       }; 
      } elseif ($timeAgo >= 604800 & $timeAgo < 2628000) { 
       // Weeks // 
       $timeAgo = $timeAgo/60; 
       $timeAgo = $timeAgo/60; 
       $timeAgo = $timeAgo/24; 
       $timeAgo = round($timeAgo/7); 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Weeks Ago"; 
       } else { 
        $timeAgo .= " Week Ago"; 
       }; 
      } elseif ($timeAgo >= 2628000) { 
       // Months // 
       $timeAgo = $timeAgo/60; 
       $timeAgo = $timeAgo/60; 
       $timeAgo = $timeAgo/24; 
       $timeAgo = $timeAgo/365; 
       $timeAgo = round($timeAgo * 12); 
       if ($timeAgo >= 2) { 
        $timeAgo .= " Months Ago"; 
       } else { 
        $timeAgo .= " Month Ago"; 
       }; 
      }; 
      echo '<div class="feed-post"><a href="http://steamcommunity.com/profiles/'.$bet['steamID'].'"><div class="steam-img" style="background-image: url('.$user['steamAvatar'].');"></div><span class="steam-name" title="'.$user['steamName'].'">'.$user['steamName'].'</span></a> bet <span class="bet-amount">$'.$bet['betAmount'].'</span> on <span class="bet-on">'.$betTeam['teamShort'].'</span><br><span class="bet-time">'.$timeAgo.'</span></div>'; 
     }; 
    }; 
}; 
mysqli_close($connection); 
?> 
0

您有多個$result =陳述。第二和第三個陳述正在取代之前陳述的內容。將它們更改爲不同的名稱,例如$ result1,$ result2和$ result3,這樣可以解決您的問題。

您也可以在程序的相應部分嘗試使用print_r($bet);print_r($user);以查看這些變量中放入的內容。