从数据库“PHP”调用数据后扩展表

时间:2011-07-28 08:54:10

标签: php database

<?php
    $dbhost = 'localhost';
    $dbuser = '/////////';
    $dbpass = '////////';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

    $dbname = '///////';
    mysql_select_db($dbname);
    mysql_query("SET NAMES 'utf8'", $conn);  
    $result = mysql_query($query);
    $query  = "SELECT * FROM 3orod";
    $result = mysql_query($query);
    $i= 0;
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html dir="rtl" xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <meta content="ar-sa" http-equiv="Content-Language" />
    <title>آخر العروض</title>
    <style type="text/css">
    body {
      width: 99%;
      margin-left:auto;
      margin-right:auto;
      background-color: gray;
      color: #505050;
    }

    .website {
      background-color: #FFFFFF;
      border:1px solid #000000;
      border-radius: 10px;
      min-width:860px;
      box-shadow: 5px 5px 5px #000000;
      max-height: 99%;
      min-height: 600;
      height: 99%;
    }

    .header {
    background-color: #E6F3DB;
    font-size:small;
    font-style:oblique;

    }

    .header2 {
    background-color: #E6F3DB;
    font-size:small;
    font-style:oblique;

    }


    .row {
    background-color: #FFFFFF;
    font-size:small;
    font-style:oblique;
    }


    table {
    border:1px solid #000000;
    max-width:99%;
    }
    td {
    }
    </style>

    </head>

    <body>
    <div class="website">
    <table width="100%" border="1">
    <?php
    while($row = mysql_fetch_assoc($result))
    {

    if ($i == 0){
    echo "<tr>".
            "<td  class='header'>   {$row['A']}</td>".
            "<td width='3%' class='header'> {$row['B']}</td>".
             "<td  class='header'>  {$row['C']}</td>".
             "<td  class='header'>  {$row['D']}</td>".
             "<td  class='header'>  {$row['E']}</td>".
             "<td  class='header'>  {$row['F']}</td>".
             "<td  class='header'>  {$row['G']}</td>".
             "<td  class='header'>  {$row['H']}</td>".
             "<td  class='header2'> {$row['I']}</td>".
             "<td width='1px' class='header'>   {$row['J']}</td>".
             "<td width='1px' class='header'>   {$row['K']}</td>".
             "<td  class='header'>  {$row['L']}</td>".
             "<td  class='header'>  {$row['M']}</td>".
             "<td  class='header'>  {$row['N']}</td>".
             "<td  class='header'>  {$row['O']}</td>".
             "<td  class='header'>  {$row['P']}</td>".
             "<td  class='header'>  {$row['Q']}</td>".
             "<td  class='header'>  {$row['R']}</td>".
             "<td  class='header'>  {$row['S']}</td>".
             "<td  class='header'>  {$row['T']}</td>".
             "<td  class='header'>  {$row['U']}</td>".
             "<td  class='header'>  {$row['V']}</td>".
             "<td  class='header'>  {$row['W']}</td>".
             "<td  class='header'>  {$row['X']}</td>".
             "<td  class='header'>  {$row['Y']}</td>".
             "<td  class='header'>  {$row['Z']}</td>".
             "</tr>"     ;
             $i=1;
    }
    else
    {
        echo "<tr>".
            "<td  class='row'>  {$row['A']}</td>".
            "<td width='3%' class='row'>    {$row['B']}</td>".
             "<td  class='row'> {$row['C']}</td>".
             "<td  class='row'> {$row['D']}</td>".
             "<td  class='row'> {$row['E']}</td>".
             "<td  class='row'> {$row['F']}</td>".
             "<td  class='row'> {$row['G']}</td>".
             "<td  class='row'> {$row['H']}</td>".
             "<td  class='row2'>    {$row['I']}</td>".
             "<td width='1px' class='row'>  {$row['J']}</td>".
             "<td width='1px' class='row'>  {$row['K']}</td>".
             "<td  class='row'> {$row['L']}</td>".
             "<td  class='row'> {$row['M']}</td>".
             "<td  class='row'> {$row['N']}</td>".
             "<td  class='row'> {$row['O']}</td>".
             "<td  class='row'> {$row['P']}</td>".
             "<td  class='row'> {$row['Q']}</td>".
             "<td  class='row'> {$row['R']}</td>".
             "<td  class='row'> {$row['S']}</td>".
             "<td  class='row'> {$row['T']}</td>".
             "<td  class='row'> {$row['U']}</td>".
             "<td  class='row'> {$row['V']}</td>".
             "<td  class='row'> {$row['W']}</td>".
             "<td  class='row'> {$row['X']}</td>".
             "<td  class='row'> {$row['Y']}</td>".
             "<td  class='row'> {$row['Z']}</td>".
             "</tr>"     ;
    } 
    }
    mysql_close();
    ?>
    </table>
    </div>
    </body>

    </html>

如果您看到结果,您可以看到细胞无缘无故地扩张 http://www.almousaakar.com/test/3orod/admin/

我的问候

1 个答案:

答案 0 :(得分:1)

细胞不会无缘无故地膨胀,它们会膨胀以适应细胞内容物。看看automatic table layout。此外,HTML4规范说明了table cell width

  

如果列宽对于特定表格单元格的内容来说过于狭窄,则用户代理可能会选择重排表格。

将一个巨大的页面作为样本链接并在没有警告的情况下链接到一个巨大的页面都是不好的形式。有些浏览器可能行为不端,人们不会欣赏你崩溃他们的浏览器。 Samples(页面和代码)应简明扼要。从示例代码中删除任何无关的内容,并减少显示的结果。

题外话

对于标题单元格,请使用<th>,而不是<td>。保持semantic

如果您发现自己重复代码,则应使用函数或循环。

mysql扩展正在推向弃用。使用PDO或mysqli,它们都支持预备语句。

希望您没有像在示例中那样将数据库凭据放在主脚本中。敏感信息应保存在一个位置,以便更容易保护。而且,你重复的东西越多,拼写错误的可能性就越大。 Don't repeat yourself

名称应该是描述性的;单字母列(或变量或...)名称不是。他们甚至参与了daily WTF。希望这只是在示例代码中,生产代码(和数据库)具有更多描述性名称。

样式表优于width属性,用于设置表格单元格宽度,这是结构与表示分离的结果。您可以为单元格提供一个在选择器中定位的类;当他们拥有更广泛的浏览器支持时,您可以在选择器中使用:nth-child():nth-of-type()伪类(例如tr > nth-child(2)th:nth-of-type(2), td:nth-of-type(2))而不是类。来自以前链接到的HTML4规范的相同部分:

  

请注意。虽然不推荐使用TABLE元素的width属性,但鼓励作者使用样式表来指定表格宽度。

这是一个包含其中一些问题的更改的示例。首先,'localdb.php',一个处理数据库连接创建的类,允许您在某处隔离凭证(尽管未显示在哪里)。请注意,它仍然存在问题(例如数据访问和表示过于紧密耦合)。

<?php
class LocalDB {
    static function connect($dbName=null) {
        if ($dbName) {
            $dbName = ";dbname=$dbName";
        }
        try {
            $db = PDO("mysql:hostname=localhost$dbName", 
                      self::user(), self::password(), 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            # can cause problems in some versions of PHP 5.2
            $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, False);
            return $db;
        } catch (PDOException $exc) {
            # remove call to PDO's constructor from trace, which will include
            # database credentials
            throw new PDOException($exc->getMessage(), $exc->getCode());
        }
    }
    static private user() {...}
    static private password() {...}
}

接下来是主脚本(没有embedded style sheet,因为在示例中无关紧要,并且在任何情况下都应该放在外部样式表中):

<?php
include_once('path/to/localdb.php');
$db = LocalDB::connect(...);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="rtl" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <meta content="ar-sa" http-equiv="Content-Language" />
    <title>آخر العروض</title>
  </head>
  <body>
    <div class="website">
    <?php
    try {
        $result = $db->query(...);
        $result->setFetchMode(PDO::FETCH_ASSOC);
        ?>
        <table>
          <?php $header = $result->fetch(); ?>
          <tr>
            <?php foreach ($header as $field => $value) { ?>
              <th class="<?php echo $field ?>"><?php echo $value; ?></th>
            <?php } ?>
          </tr>
          <?php foreach ($result as $row) { ?>
            <tr>
              <?php foreach ($row as $field => $value ?>
                <td class="<?php echo $field ?>"><?php echo $value ?></td>
              <?php } ?>
            </tr>
          <?php } ?>
        </table>
      <?php } catch (PDOException $exc) { ?>
          <p>There was an internal error when fetching the data. It's been logged, and we'll look into it.</p>
          <?php 
          // log exception
          ...
      } ?>
    </div>
  </body>
</html>