在计算变量之前打印计算出的值

时间:2019-05-18 03:46:57

标签: php mysqli

这是一个棘手的问题,因此我在这里发帖。我有一个标题,该标题显示在其下面打印的表格的列中所有值的总和。但是,该表是从MYSQL表生成的,并且列值的总和是在生成时计算的。因此,我必须以某种方式打印变量,但是只能在生成表之后才能打印,而且我不确定如何将变量传递回print语句,因此它并不总是打印出0

我觉得解决方案是,总和应该调用脚本(Javascipt),该脚本生成并打印表,并返回要打印的列总和。但是我不确定该怎么做

echo "
    <h3>EPL</h3>
    <h5>Total Score: $total</h5>
    <table class='table table-bordered'>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Score</th>
        </tr>
        <tbody class='row_position'>"?>
        <?php

        require('db_config.php');
        $tablename = $_SESSION['username'] . "_epl";
        $_SESSION['tablename'] = $tablename;
        $sql = "SELECT * FROM $tablename ORDER BY position_order";
        $users = $mysqli->query($sql);
        while($user = $users->fetch_assoc()){
        $con = mysqli_connect('localhost', 'root', 'root', 'predictions');
        $sql1 = "SELECT * FROM `predictions`.`".$tablename."` WHERE (CONVERT(`title` USING utf8) LIKE '%".$user['title']."%')";
        $sql2 = "SELECT * FROM `predictions`.`epl` WHERE (CONVERT(`title` USING utf8) LIKE '%".$user['title']."%')";
        $result = mysqli_query($con, $sql1);
        $row = $result->fetch_assoc();
        $position1 = $row['position_order'];
        $result->close();
        $result = mysqli_query($con, $sql2);
        $row = $result->fetch_assoc();
        $position2 = $row['position_order'];
        $total += abs($position1-$position2);



        ?>
            <tr  id="<?php echo $user['id'] ?>">
                <td><?php echo $user['position_order'] ?></td>
                <td><?php echo $user['title'] ?></td>
                <td><?php echo abs($position1-$position2); ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>

为进一步解释该表,每个用户都有自己的具有相同格式username_league的表,因此我使用一些php代码来获取该表。另外,在这种情况下,我有一个基本案例表“ epl”,在其中比较表并基于“ position_order”列中的绝对差来计算分数。

2 个答案:

答案 0 :(得分:1)

输出缓冲是您的朋友!只需在输出表格之前调用ob_start,即

ob_start();
echo "
<table class='table table-bordered'>
    <tr>
        <th>#</th>
        <th>Name</th>
        <th>Score</th>
    </tr>
    <tbody class='row_position'>"?>
...

然后,一旦完成表的生成,就可以使用ob_get_clean收集表内容的输出,输出标题,然后输出表内容:

...
$table = ob_get_clean();
echo "<h3>EPL</h3>
<h5>Total Score: $total</h5>";
echo $table;

答案 1 :(得分:1)

您应始终致力于将代码和演示文稿分开。除此之外,您的数据库代码效率极低。您正在为原始查询的每个实例重新创建整个数据库对象。

只需将PHP代码放在HTML之前,或者最好将其放在单独的文件中。

<?php

$total = 0;

require('db_config.php');
$tablename = $_SESSION['username'] . "_epl";
$_SESSION['tablename'] = $tablename;
$sql = "SELECT id, position_order, title FROM `$tablename` ORDER BY position_order";
$users_result = $mysqli->query($sql);
while($user = $users_result->fetch_assoc()) {
    $users[] = $user;
}

foreach ($users as $user) {
    $sql1 = "
        SELECT position_order FROM `$tablename` WHERE CONVERT(`title` USING utf8) LIKE '%$user[title]%' LIMIT 1
        UNION
        SELECT position_order FROM `epl` WHERE CONVERT(`title` USING utf8) LIKE '%$user[title]%' LIMIT 1
    ";
    $result = $mysqli->query($sql1);
    $row = $result->fetch_assoc();
    $position1 = $row['position_order'];
    $user["position1"] = $position1;

    $row = $result->fetch_assoc();
    $position2 = $row['position_order'];
    $user["position2"] = $position2;

    $total += abs($position1 - $position2);
}
?>

你在那里;使用单个数据库对象,查询减少三分之一,数组中的所有值。它们可以稍后在文件中或在单独的HTML视图中使用:

<h3>EPL</h3>
<h5>Total Score: <?=$total?></h5>
<table class='table table-bordered'>
    <tr>
        <th>#</th>
        <th>Name</th>
        <th>Score</th>
    </tr>
    <tbody class='row_position'>

    <?php foreach($users as $user):?>
        <tr  id="<?=$user['id'] ?>">
            <td><?=$user['position_order'] ?></td>
            <td><?=$user['title'] ?></td>
            <td><?=abs($user["position1"]-$user["position2"]); ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

我对这里的结构了解不多,但是如果您不能将其设为单个数据库查询,我会感到非常惊讶。

相关问题