1

我一直在努力尝试让一堆数据在使用 PHP 整齐显示的表结构中工作,我必须说我遇到了困难。我终于能够调用这些列,这样如果我添加一个字段,它就会始终将它添加到我的表中,我希望这很容易管理。但是,当我最初设置表格时,我将其按随机顺序排列。现在,当我开始使用 DESCRIBEtable时,它会以准确的顺序将它们提供给我,但我找不到更好地组织它们的方法。在我拥有的表格的数据库末尾说有月/年是没有意义的。

<?php 
require 'connect.php';
?>
<h3>Monthly Finances | <a href="new.php">Add New Month</a> </h3>
<table border="1" bordercolor ="#000000">
<tr>
<?php
$columns = "DESCRIBE  `month` ";
if($query_run_columns = mysql_query($columns)){
    $columnNames = array();
        while($column = mysql_fetch_assoc($query_run_columns)){
        echo '<th>'.$column['Field'].'</th>';
    }
}else{
    echo'sql error';
}

?>


<?php
$gatherdata = "SELECT `month`, `year`,`rent`,`electric`,`cable`,`cellphone`,`renters` FROM `month` ";
if($query_run = mysql_query($gatherdata)){

    while($row = mysql_fetch_assoc($query_run)){
        $rent = $row['rent'];
        $electric = $row['electric'];
        $cable = $row['cable'];
        $cellphone = $row['cellphone'];
        $renters = $row['renters'];
        $month = $row['month'];
        $year = $row['year'];

        echo '<tr>';
        echo '<td align="center">'.$month.'</td>';
        echo '<td algin="center">'.$year.'</td>';
        echo '<td align="center">'.$rent.'</td>';
        echo '<td align="center">'.$electric.'</td>';
        echo '<td align="center">'.$cable.'</td>';
        echo '<td align="center">'.$cellphone.'</td>';
        echo '<td align="center">'.$renters.'</td>';
        echo '</tr>';
    }
}else{
    echo 'query error';
}
?>
<br>View by month
</table>
<form action="index.php" method="GET">
<select name="months" value="all">
<?php
$gathermonths = "SELECT `month`, `year` FROM `month";

if($query_month_selector = mysql_query($gathermonths)){
    while($month_row = mysql_fetch_assoc($query_month_selector)){
        $month = $month_row['month'];
        $year = $month_row['year'];

        echo '<option value="'.$month.' '.$year.'">' .$month.' '.$year.'</option>';
    }
}
echo $_GET['months'];
?>
<input type="submit" value="View Selected Date"></input>
</select>
</form>

我的代码远未完成或有序,但我一直在这个项目上懈怠,当我有更多时间时,我会更多地清理它。但是,如果您可以的话,请帮我介绍一种有效的组织方法,我们将不胜感激。

4

3 回答 3

1

您的表是否包含索引/ID?

您可以通过“ORDER BY”轻松地按升序/降序排列它们

ALTER TABLE `table_name` ORDER BY `id`

注意默认ORDER BY是升序。DESC如果你想下降,放在最后。

如果您希望您的 mysql 查询以良好的有序方式输出结果,您也可以ORDER BY在查询中使用:

$gathermonths = "SELECT `month`, `year` FROM `month` ORDER BY `month` DESC";

GROUP BY如果您想按月对结果进行分组,还有一些功能:

$gathermonths = "SELECT `month`, `year` FROM `month` GROUP BY `month`";
于 2012-04-28T20:49:13.620 回答
1

DESCRIBE可以使用mysql_fetch_field. 然后列将始终与您在 上的顺序相同SELECT

<?php
$gatherdata = "SELECT `month`, `year`,`rent`,`electric`,`cable`,`cellphone`,`renters` FROM `month` ";
if($query_run = mysql_query($gatherdata)){

    // Loop the columns to build the table headers
    echo '<table>';
    echo '<tr>';
    while ($i < mysql_num_fields($query_run)) {
        $field = mysql_fetch_field($query_run, $i);
        echo '<th>' . $field->name . '</th>';
        $i++;
    }
    echo '</tr>';

    // Your current data loop
    while($row = mysql_fetch_assoc($query_run)){
        // (...)
    }
    echo '</table>'
}
?> 
于 2012-04-28T21:06:14.647 回答
1

由于您已经指定了要获取的列,因此只需显式输出列标题而不是查询数据库。

相反,如果您想抽象 HTML 表生成代码以便它与任意 SQL 表一起使用(从而数据访问与显示分开),请在生成列标题时记录列名,然后在输出时迭代此数组行。您也可以取消$rent = $row['rent'];分配,因为它们对您没有任何好处。使用 PDO:

/* data access */
$finances = $db->query('SELECT `month`, `year`,`rent`,`electric`,`cable`,`cellphone`,`renters` FROM `month`');

$columns = array();
for ($i = 0; $i < $finances->columnCount(); ++$i) {
    $meta = $finances->getColumnMeta($i);
    $columns[$meta['name']] = ucwords($meta['name']);
}

/* data display. Note this is completely independent of the type used to store 
   the colum & row data. All that matters are that $columns and $finances are 
   Traversable
 */
?>
<table>
  <thead>
    <tr>
      <?php foreach ($columns as $name => $label): ?>
        <th><?php echo $label ?></th>
      <?php endforeach ?>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($finances as $monthly): ?>
      <tr>
        <?php foreach ($columns as $name => $label): ?>
          <td><?php echo $monthly[$name]; ?></td>
        <?php endforeach ?>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>

错误处理和抽象到模块中留作练习。

于 2012-04-28T21:20:26.620 回答