0

我正在构建一个页面,脚本完成它在表格中列出名称和点。但是当我要做设计时,用“echo”来做会很难。因此,如果我可以将其放入我可以在 html 文件中使用的 vars 中,我会感到很痛苦。

表看起来像这样

姓名 | 积分 | 日期

我想要的是为 10 个名字行创建一个 var,为 10 个第一个点行创建一个 var。

喜欢

$top1n = $row[0]'name'
$top1p = $row[0]'points'
$top2n = $row[1]'name'
$top2p = $row[1]'points'
$top3n = $row[2]'name'
$top3p = $row[2]'points'
$top4n = $row[3]'name'
$top4p = $row[3]'points'

等等...

等等

请参阅下面的脚本

echo "<table border='1'>";

echo "<tr> <th>Name</th> <th>Tokens</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $resultbtm )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['Name'];
    echo "</td><td>"; 
    echo $row['points'];
    echo "</td></tr>"; 
} 

echo "</table>";
4

3 回答 3

0

你可以这样做:(不需要创建这么多变量)

$str = "<table border='1'>";

$str .= "<tr> <th>Name</th> <th>Tokens</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $resultbtm )) {
    // Print out the contents of each row into a table
    $str .= "<tr>";
    $str .= "<td>".$row['Name']."</td><td>".$row['points']."</td>";
    $str .= "</tr>"; 
} 

$str .= "</table>";
echo $str;
于 2013-06-20T04:57:31.887 回答
0

这是你想要的吗?

$names = array();
$points = array();

while($row = mysql_fetch_array($resultbtm)) {
    $names[] = $row['Name'];
    $points[] = $row['tokens'];
}

echo '<pre>';
print_r($names);
print_r($points);
echo '</pre>';

不是 100% 确定这是否是您要查找的内容,但$namesand$points变量将是包含 mysql 结果集中的名称和令牌(分别)的数组。

旁注:mysql_*由于安全问题,函数自 PHP 5.5.0 起已弃用。如果可以,强烈建议您切换到mysqli_*PDO

于 2013-06-19T18:23:56.567 回答
-1

你可以使用extract

    echo "<table border='1'>";

    echo "<tr> <th>Name</th> <th>Tokens</th> </tr>";
    // keeps getting the next row until there are no more to get
    $c = 1;
    while($row = mysql_fetch_array( $resultbtm )) {
        ${'top'.$c.'n'} = $name;
        ${'top'.$c.'p'} = $points;
        $c++;

        // Print out the contents of each row into a table
        echo "<tr><td>$Name</td><td>$tokens</td></tr>"; 
    } 

    echo "</table>";
于 2013-06-19T18:23:12.640 回答