2014-03-27 90 views
1

我有一個查詢我的PHP只打印出一行的結果,我需要打印出每個用戶的所有行。Foreach循環從使用PHP的查詢

這裏是PHP:

<?php 
    // see if the form has been completed 
    include_once("php_includes/check_login_status.php"); 
    //include_once("php_includes/db_conx.php"); 
    // Initialize any variables that the page might echo 
    $username = ""; 
    $weight = ""; 
    $weighthist = ""; 
    $id = "";  

    if(isset($_GET["u"])){ 
     $username = preg_replace('#[^a-z0-9]#i', '', $_GET['u']); 
    } 

    $sql = "SELECT users.*, weighthistory.* FROM users JOIN weighthistory USING(id)"; 

    $user_query = mysqli_query($db_conx, $sql); 


    // check if the user exists in the database 
    while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) { 
     $id = $row ["id"]; 
     $username = $row ["username"]; 
     $weight = $row["weight"]; 
     $weighthist = $row["weighthist"]; 
     $point_hist = $row["point_hist"]; 

     } 

     // this is to calculate points score 
     $calweight = $weight - $weighthist;  
     $points = $calweight * 10; 



     $res = mysqli_query($db_conx,'SELECT sum(point_hist) FROM points_history'); 
     if (FALSE === $res) die("Select sum failed: ".mysqli_error); 
     $row = mysqli_fetch_row($res); 
     $sum = $row[0]; 




?> 

下面是HTML:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>Profile Update: <?php echo $u; ?></title> 
    <link rel="icon" href="favicon.ico" type="image/x-icon"> 
    <link rel="stylesheet" type="text/css" href="style.css"> 
    <script src="js/main.js"></script> 
    <script src="js/javascript.js"></script> 
    <script src="js/ajax.js"></script> 
    <style type="text/css"> 
     #updateform{ 
     margin-top:24px;  
     } 
     #updateform > div { 
      margin-top: 12px; 
     } 
     #updateform > input { 
      width: 200px; 
      padding: 3px; 
      background: #F3F9DD; 
     }  
    </style> 

</head> 
<body> 
    <p>&nbsp;</p> 
    <?php include_once("template_pageTop.php"); ?> 
    <div id="pageMiddle">  
    <div id="usernamecss"> Username: <?php echo $username; ?></div> 

    <table width="100%" border="0"> 
     <tr> 
     <td>Name</td> 
     <td>Weight</td> 
     <td>Rank</td> 
     <td>Points</td> 
     </tr> 
     <tr> 
     <td><?php echo $username ?></td> 
     <td><?php echo $weight?></td> 
     <td><?php echo $rank?></td> 
     <td><?php echo $sum?></td> 
     </tr> 
     </table> 
    <p>&nbsp;</p> 
    <strong></strong> 
    <a href="user.php<?php echo "?u=",$username;?>">Go to Profile</a> 
    </form> 
    </div> 
    <?php include_once("template_pageBottom.php"); ?> 
</body> 
</html> 

我是新來這個,所以我怎麼可以打印所有行所有用戶ID,我的想法我必須使用foreach循環。

+0

你說你有,你必須使用foreach循環的想法。這就是你需要做的 – WordsWorth

回答

1

這是你如何能做到這一點...

PHP文件

<?php 
    // see if the form has been completed 
    include_once("php_includes/check_login_status.php"); 
    //include_once("php_includes/db_conx.php"); 
    // Initialize any variables that the page might echo 
    $username = ""; 
    $weight = ""; 
    $weighthist = ""; 
    $id = "";  

    if(isset($_GET["u"])){ 
     $username = preg_replace('#[^a-z0-9]#i', '', $_GET['u']); 
    } 

    $sql = "SELECT users.*, weighthistory.* FROM users JOIN weighthistory USING(id)"; 

    $user_query = mysqli_query($db_conx, $sql); 


    // check if the user exists in the database 
    while ($row = mysqli_fetch_assoc($user_query)) { 
     $id = $row ["id"]; 
     $username = $row ["username"]; 
     $weight = $row["weight"]; 
     $weighthist = $row["weighthist"]; 
     $point_hist = $row["point_hist"]; 

     // this is to calculate points score 
     $calweight = $weight - $weighthist;  
     $points = $calweight * 10; 

     $res = mysqli_query($db_conx,'SELECT sum(point_hist) FROM points_history'); 
     if (FALSE === $res) die("Select sum failed: ".mysqli_error); 
     $row = mysqli_fetch_row($res); 
     $sum = $row[0]; 

     ?> 
     <div id="pageMiddle">  
      <div id="usernamecss"> Username: <?php echo $username; ?></div> 

      <table width="100%" border="0"> 
       <tr> 
        <td>Name</td> 
        <td>Weight</td> 
        <td>Rank</td> 
        <td>Points</td> 
       </tr> 
       <tr> 
        <td><?php echo $username ?></td> 
        <td><?php echo $weight?></td> 
        <td><?php echo $rank?></td> 
        <td><?php echo $sum?></td> 
       </tr> 
      </table> 
      <p>&nbsp;</p> 
      <strong></strong> 
      <a href="user.php<?php echo "?u=",$username;?>">Go to Profile</a> 
     </form> 
    </div> 
     <?php 
    } 
?> 

HTML文件

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>Profile Update: <?php echo $u; ?></title> 
    <link rel="icon" href="favicon.ico" type="image/x-icon"> 
    <link rel="stylesheet" type="text/css" href="style.css"> 
    <script src="js/main.js"></script> 
    <script src="js/javascript.js"></script> 
    <script src="js/ajax.js"></script> 
    <style type="text/css"> 
     #updateform{ 
     margin-top:24px;  
     } 
     #updateform > div { 
      margin-top: 12px; 
     } 
     #updateform > input { 
      width: 200px; 
      padding: 3px; 
      background: #F3F9DD; 
     }  
    </style> 

</head> 
<body> 
    <p>&nbsp;</p> 
    <?php include_once("template_pageTop.php"); ?> 
    <?php include_once("template_pageBottom.php"); ?> 
</body> 
</html> 

編輯:

如果用戶名是在您的points_history表中的列...那麼你就可以改變這個

$res = mysqli_query($db_conx,'SELECT sum(point_hist) FROM points_history'); 
if (FALSE === $res) die("Select sum failed: ".mysqli_error); 
$row = mysqli_fetch_row($res); 
$sum = $row[0]; 

這個

$query = "SELECT sum(point_hist) FROM points_history WHERE username = $username"; 
$res = mysqli_query($db_conx, $query); 
$row = mysqli_fetch_row($res); 
$sum = $row[0]; 
+0

這不起作用,它只顯示一個用戶。 – user3443701

+0

@ user3443701我更新了上面的答案,我將'mysqli_fetch_array'更改爲'mysqli_fetch_row' ...它現在應該可以工作。 –

+0

它給了我一個'mysqli_fetch_row'上的錯誤,這是'mysqli_fetch_row()期望的確切的1個參數' – user3443701