【问题标题】:List all info within table into html table将表中的所有信息列出到 html 表中
【发布时间】:2018-06-17 03:45:50
【问题描述】:

我不记得如何做到这一点。我正在尝试从数据库中的表中获取所有条目并将其列表到 html 表中。我还试图将从表中提取的条目数限制在 10 到 20 之间。

//Connection Info
include('data.php');

//Query To Pull Data
$sql = mysql_query("select name, death, model, humanity, hkills, bkills, kills, hs, late, ldrank, stime, survival, lastupdate from $stats order by kills limit 10");

//Data Pulled To Be Displayed
while ($row = mysql_fetch_array($sql)) {
    //Username
    $name = $row['name'];
    //Deaths
    $death = $row['death'];
    //Amount of Humanity
    $humanity = $row['humanity'];
    //Player Type
    $model = $row['model'];
    //Murders
    $murder = $row['hkills'];
    //Bandit Kills
    $bandit = $row['bkills'];
    //Zombie Kills
    $zombie = $row['kills'];
    //Head Shots
    $head = $row['hs'];
    //Unknown
    $late = $row['late'];
    //Unknown
    $ldrank = $row['ldrank'];
    //Unknown
    $stime = $row['stime'];
    //Time Survived
    $survival = $row['survival'];
    //Last Time Player Was On
    $update = $row['lastupdate'];
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="1000" border="1">
  <tr>
    <th scope="col">Username</th>
    <th scope="col">Type</th>
    <th scope="col">Friendliness</th>
    <th scope="col">Deaths</th>
    <th scope="col">Murders</th>
    <th scope="col">Bandit Kills</th>
    <th scope="col">Zombie Kills</th>
    <th scope="col">Head Shots</th>
    <th scope="col">Late</th>
    <th scope="col">STime</th>
    <th scope="col">Time Survived</th>
    <th scope="col">LDRank</th>
    <th scope="col">Last Played</th>
  </tr>
  <tr>
    <td><? echo "$name"; ?></td>
    <td><? echo "$model"; ?></td>
    <td><? echo "$humanity"; ?></td>
    <td><? echo "$death"; ?></td>
    <td><? echo "$murder"; ?></td>
    <td><? echo "$bandit"; ?></td>
    <td><? echo "$zombie"; ?></td>
    <td><? echo "$head"; ?></td>
    <td><? echo "$late"; ?></td>
    <td><? echo "$ldrank"; ?></td>
    <td><? echo "$stime"; ?></td>
    <td><? echo "$survival"; ?></td>
    <td><? echo "$update"; ?></td>
  </tr>
</table>

</body>
</html>

【问题讨论】:

    标签: php mysql html-table


    【解决方案1】:
    //Connection Info
    include('data.php');
    
    //Query To Pull Data
    $sql = mysql_query("select name, death, model, humanity, hkills, bkills, kills, hs, late, ldrank, stime, survival, lastupdate from $stats order by kills limit 10");
    
    
    
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <table width="1000" border="1">
      <tr>
        <th scope="col">Username</th>
        <th scope="col">Type</th>
        <th scope="col">Friendliness</th>
        <th scope="col">Deaths</th>
        <th scope="col">Murders</th>
        <th scope="col">Bandit Kills</th>
        <th scope="col">Zombie Kills</th>
        <th scope="col">Head Shots</th>
        <th scope="col">Late</th>
        <th scope="col">STime</th>
        <th scope="col">Time Survived</th>
        <th scope="col">LDRank</th>
        <th scope="col">Last Played</th>
      </tr>
    <?php
    while ($row = mysql_fetch_array($sql)) {
    ?>
      <tr>
        <td><? echo $row["$name"]; ?></td>
        <td><? echo $row["$model"]; ?></td>
        <td><? echo $row["$humanity"]; ?></td>
        <td><? echo $row["$death"]; ?></td>
        <td><? echo $row["$murder"]; ?></td>
        <td><? echo $row["$bandit"]; ?></td>
        <td><? echo $row["$zombie"]; ?></td>
        <td><? echo $row["$head"]; ?></td>
        <td><? echo $row["$late"]; ?></td>
        <td><? echo $row["$ldrank"]; ?></td>
        <td><? echo $row["$stime"]; ?></td>
        <td><? echo $row["$survival"]; ?></td>
        <td><? echo $row["$update"]; ?></td>
      </tr>
    <?php } ?>
    </table>
    
    </body>
    </html>
    

    试试这段代码

    【讨论】:

      【解决方案2】:

      应该是这样的

          <table width="1000" border="1">
            <tr>
              <th scope="col">Username</th>
              <th scope="col">Type</th>
              <th scope="col">Friendliness</th>
              <th scope="col">Deaths</th>
              <th scope="col">Murders</th>
              <th scope="col">Bandit Kills</th>
              <th scope="col">Zombie Kills</th>
              <th scope="col">Head Shots</th>
              <th scope="col">Late</th>
              <th scope="col">STime</th>
              <th scope="col">Time Survived</th>
              <th scope="col">LDRank</th>
              <th scope="col">Last Played</th>
            </tr>
      
          <?php
          while ($row = mysql_fetch_array($sql)) { ?>
          <tr>
              <td><?php echo $row['name']; ?></td>
              // Rest of other values
          <tr>
         <?php } //End while?>
          </table>
      

      【讨论】:

        【解决方案3】:
        //Connection Info
        include('data.php');
        
        $dataFromTable = '';
        //Query To Pull Data
        $sql = mysql_query("select name, death, model, humanity, hkills, bkills, kills, hs, late, ldrank, stime, survival, lastupdate from $stats order by kills limit 0,20");
        
        //Data Pulled To Be Displayed
        while ($row = mysql_fetch_array($sql)) {
            //Username
            $name = $row['name'];
            //Deaths
            $death = $row['death'];
            //Amount of Humanity
            $humanity = $row['humanity'];
            //Player Type
            $model = $row['model'];
            //Murders
            $murder = $row['hkills'];
            //Bandit Kills
            $bandit = $row['bkills'];
            //Zombie Kills
            $zombie = $row['kills'];
            //Head Shots
            $head = $row['hs'];
            //Unknown
            $late = $row['late'];
            //Unknown
            $ldrank = $row['ldrank'];
            //Unknown
            $stime = $row['stime'];
            //Time Survived
            $survival = $row['survival'];
            //Last Time Player Was On
            $update = $row['lastupdate'];
        
           $dataFromTable .= "<tr><td>$name</td>
            <td>$model</td>
            <td>$humanity</td>
            <td>$death</td>
            <td>$murder</td>
            <td>$bandit</td>
            <td>$zombie</td>
            <td>$head</td>
            <td>$late</td>
            <td>$ldrank</td>
            <td>$stime</td>
            <td>$survival</td>
            <td>$update</td></tr>"
        }
        
        
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        </head>
        
        <body>
        <table width="1000" border="1">
          <tr>
            <th scope="col">Username</th>
            <th scope="col">Type</th>
            <th scope="col">Friendliness</th>
            <th scope="col">Deaths</th>
            <th scope="col">Murders</th>
            <th scope="col">Bandit Kills</th>
            <th scope="col">Zombie Kills</th>
            <th scope="col">Head Shots</th>
            <th scope="col">Late</th>
            <th scope="col">STime</th>
            <th scope="col">Time Survived</th>
            <th scope="col">LDRank</th>
            <th scope="col">Last Played</th>
          </tr>
            <?php echo $dataFromTable;?>
        </table>
        
        </body>
        </html>
        

        【讨论】:

          【解决方案4】:

          如果您试图列出所有条目,那么您可以在 while 循环中为数据包装表格行。请参阅下面从您的原始示例更改的示例。基本上,这是如何工作的,PHP 只会在循环开始后处理?&gt;,在循环结束时处理循环结束时的&lt;?php,而循环仍在运行,之后它将继续正常执行。这样,您可以将表格行包装在 while 外观中,并且由于表格行在循环中,它将与变量一起打印出循环中的每个项目,这应该是 10 个项目,因为这就是您定义的限制为。我希望我正确理解了这个问题,这会有所帮助:)

          //Connection Info
          include('data.php');
          
          //Query To Pull Data
          $sql = mysql_query("select name, death, model, humanity, hkills, bkills, kills, hs, late, ldrank, stime, survival, lastupdate from $stats order by kills limit 10");
          
          ?>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <title>Untitled Document</title>
          </head>
          
          <body>
          <table width="1000" border="1">
            <tr>
              <th scope="col">Username</th>
              <th scope="col">Type</th>
              <th scope="col">Friendliness</th>
              <th scope="col">Deaths</th>
              <th scope="col">Murders</th>
              <th scope="col">Bandit Kills</th>
              <th scope="col">Zombie Kills</th>
              <th scope="col">Head Shots</th>
              <th scope="col">Late</th>
              <th scope="col">STime</th>
              <th scope="col">Time Survived</th>
              <th scope="col">LDRank</th>
              <th scope="col">Last Played</th>
            </tr>
            <?php while ($row = mysql_fetch_array($sql)) { ?>
            <tr>
              <td><? echo $row['name']; ?></td>
              <td><? echo $row['model']; ?></td>
              <td><? echo $row['humanity']; ?></td>
              <td><? echo $row['death']; ?></td>
              <td><? echo $row['hkills']; ?></td>
              <td><? echo $row['bkills']; ?></td>
              <td><? echo $row['kills']; ?></td>
              <td><? echo $row['hs']; ?></td>
              <td><? echo $row['late']; ?></td>
              <td><? echo $row['ldrank']; ?></td>
              <td><? echo $row['stime']; ?></td>
              <td><? echo $row['survival']; ?></td>
              <td><? echo $row['lastupdate']; ?></td>
            </tr>
            <?php } ?>
          </table>
          
          </body>
          </html>
          

          【讨论】:

            【解决方案5】:

            应该是这样的:

            <table width='80%' border=0>
                <tr bgcolor='#CCCCCC'>
                    <th>Username</th>
                    <th>Type</th>
                    <th>Deaths</th>     
                </tr>
                    <?php 
                    while($row = mysql_fetch_array($sql)) {
                    ?>
                        <tr>
                            <td><?php echo $row['name'] ?></td>
                            <td><?php echo $row['model'] ?></td>
                            <td><?php echo $row['death'] ?></td> 
                        </tr>
                    <?php 
                    }
                    ?>
            </table>
            

            您说您只需要 10 到 20 个条目。虽然不清楚,但如果您只想要第 10 到第 20 行的条目,那么您可以通过以下方式使用限制:

            SELECT * FROM `your_table` LIMIT 10, 10 
            

            这将从第 10 条记录返回 10 条记录,即从第 10 条记录到第 20 条记录。

            关于 SQL 限制:http://php.about.com/od/mysqlcommands/g/Limit_sql.htm

            【讨论】:

              猜你喜欢
              • 2018-01-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-04-22
              • 2023-04-08
              • 2012-03-09
              • 1970-01-01
              • 2013-12-18
              相关资源
              最近更新 更多