我的查询中的MYSQL排序列

时间:2012-04-28 20:16:06

标签: php mysql

我一直在努力尝试将一堆数据用于使用PHP整齐地显示的表结构中工作,我必须说我遇到了困难。我终于能够调用列了,以防万一我添加一个字段,它总是将它添加到我的表中,我希望这很容易管理。但是,当我最初设置表时,我把它放在一个随机的顺序。现在,当我来使用DESCRIBE table时,它会按照确切的顺序给我,而我找不到更好地组织它们的方法。在我的表格数据库的末尾说月份/年份是没有意义的。

<?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>

我的代码远非完整或有序,但我一直在懈怠这个项目,当我有更多时间时,我会更清理它。但是,如果你能提供一个有效的组织方法,我将不胜感激。

3 个答案:

答案 0 :(得分: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`";

答案 1 :(得分:1)

您可以使用mysql_fetch_field代替DESCRIBE查询。然后列将始终与您在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>'
}
?> 

答案 2 :(得分:1)

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

相反,如果要抽象HTML表生成代码以使其适用于任意SQL表(从而显示separating数据访问权限),请在生成数组中的列标题时记录列名,然后在输出行时迭代此数组。你也可以取消$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>

错误处理和抽象到作为练习留下的模块。