php - 使用 PHP 从查询中进行 Foreach 循环

标签 php mysql

我在我的 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 循环。

最佳答案

这就是你可以做到的...

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];

关于php - 使用 PHP 从查询中进行 Foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22695996/

相关文章:

mysql - 从另一个表更新 MySQL 表

php - 模数除法返回一个整数?

javascript - 如何将地址附加到 <a> 标签 href

在 MYSQL 中设置 PHP 冷却时间

PHP MYSQL 查询中的 Javascript

javascript - php中onclick后的mySQL请求

php - 将 MySQL 数据标记为 'read'

多插入的 PHP/MySQL 和 Doctrine 最佳实践

javascript - 运行通过 AJAX 调用发送的 Javascript 脚本

php - 如何在php中显示特定日期的记录?