简体   繁体   English

对来自数据库的foreach插入到表中的行进行排序

[英]Sort rows being inserted into a table by a foreach from database

I have a table that is being fed its content from a database. 我有一个表从数据库中提供其内容。 The table has a with your standard table header row, and then I have a foreach() that grabs information from an include and spits it out into the table body. 该表有一个标准的表头行,然后我有一个foreach(),它从包含中获取信息并将其吐出到表体中。 (inserting a new each entry.) (插入新的每个条目。)

I need the table to be initially sorted by the date closed column, and not the symbol column. 我需要表最初按日期关闭列排序,而不是符号列。 I am using the Tablesorter plugin, and I tried doing this: 我正在使用Tablesorter插件,我尝试这样做:

$(document).ready(function() { 
    // call the tablesorter plugin 
    $("#bin").tablesorter({ 
        // sort on the first column and third column, order asc 
        sortList: [[6,1],[0,0]] 
    }); 
}); 

This works, but the issue is I have a background that is being applied to every other row from the foreach, and when table sorter sorts the rows when the page loads, the background color is not every other, but in sporadic places from table sorter moving those rows. 这是有效的,但问题是我有一个背景,正在应用于foreach中的每一行,当表格分类器在页面加载时对行进行排序时,背景颜色不是所有其他行,而是在表格分类器的零星地方移动那些行。

Here's my code: 这是我的代码:

    <table cellspacing="0" cellpadding="0" id="bin" width="100%">
        <thead>
            <tr>
                <th style="text-align:left; padding-top: 20px;" width="10%">Symbol <img src="/images/sort-arrow.jpg" alt="Sort by Symbol" class="sort-right move-left bottom-image"/></th>
                <th style="text-align:left;" width="20%">Company<br><span class="move_right">Name</span> <img src="/images/sort-arrow.jpg" alt="Sort by Company Name" class="sort-right move-left"/></th>
                <th style="text-align:left; padding-top: 20px;" width="23%">Industry <img src="/images/sort-arrow.jpg" alt="Sort by Industry" class="sort-right move-left bottom-image"/></th>
                <th style="text-align:center;" width="10%"><span class="center-text">Buy</span><br>Date <img src="/images/sort-arrow.jpg" alt="Sort by Buy Date"/></th>
                <th style="text-align:center;" width="10%"><span class="center-text">Buy</span><br>Price &nbsp;<img src="/images/sort-arrow.jpg" alt="Sort by Buy Price"/></th>
                <th style="text-align:center;" width="10%"><span class="center-text">Closed</span><br>Price &nbsp;<img src="/images/sort-arrow.jpg" alt="Sort by Closed Price"/></th>
                <th style="text-align:center;" width="10%"><span class="center-text">Closed</span><br>Date &nbsp;<img src="/images/sort-arrow.jpg" alt="Sort by Closed Date"/></th>
                <th style="text-align:center;" width="10%"><span class="center-text">Total</span><br>Return &nbsp;<img src="/images/sort-arrow.jpg" alt="Sort by Current Return"/></th>
            </tr>
        </thead>
        <tbody>
<?php
    foreach($buylist as $a) {
        $bg = ($c % 2) ? ' class="even"' : '';
        //$direction = (is_numeric($a['creturn']) && $a['creturn'] >= 0) ? 'up_green' : 'down_red';
        //$tick = (is_numeric($a['creturn']) && $a['creturn'] >= 0) ? '<img src="/images/icon_up.png">' : '<img src="/images/icon_down.png">';
        //$tick2 = (is_numeric($a['cchange']) && $a['cchange'] >= 0) ? '<img src="/images/icon_up.png">' : '<img src="/images/icon_down.png">'; 
        //$tick3 = (is_numeric($a['final_return_pct']) && $a['final_return_pct'] >= 0) ? '<img src="/images/icon_up.png">' : '<img src="/images/icon_down.png">';   
        $type = '';
        $entry_price = (is_numeric($a['buyprice'])) ? '$'.$a['buyprice'] : '&ndash;';
        $sold_price = (is_numeric($a['sold_price'])) ? '$'.$a['sold_price'] : '&ndash;';
        $total_return= sprintf("%.02f", (($a['sold_price'] - $a['buyprice'])/$a['buyprice']) * 100);
?>
            <tr<?=$bg;?>>
                <td ><b><a href="/gamechangers/getaquote/?symbolsearch=<?php echo $a['symbol']; ?>"><?=$a['symbol'];?></a></b><?=$type;?></td>
                <td><?=$a['name'];?></td>
                <td valign="top"><?=$a['industry'];?></td>
                <td align="center"><?=$a['buydate'];?></td>
                <td align="center"><?=$entry_price;?></td>
                <td align="center"><?php echo $sold_price; ?></td>
                <td align="center"><?=$a['sold_date'];?></td>
                <td align="center"><?php echo $total_return; ?>%</td>
            </tr>
<?php
        $c++;
    }
?>
        </tbody>
    </table>

CSS for the table: 表格的CSS:

table#bin, table#fallen, table#growth, table#turn { margin:10px 0; border:1px solid #ccc; }
th, td { padding:10px 7px; }
tr th { background:#ededed; color:#545454; font-weight:bold; cursor:pointer;}
#bin tr.even td { background:#e1eff1; }
#turn tr.even td { background:#f7f2d8; }
#fallen tr.even td { background:#f2dcbd; }
#growth tr.even td { background:#deefdc; }
td.title a { text-decoration:none; display:block; text-transform:uppercase; font-weight:bold;}
#bin td.title { background:#5198a0; }
#fallen td.title { background:#e6a850; }
#turn td.title { background:#ebd870; }
#growth td.title { background:#6ab065; }
#bin td.title a { color:#182c2e; font-size:13px;}
#fallen td.title a { color:#352713;  font-size:13px;}
#turn td.title a { color:#37321a;  font-size:13px; }
#growth td.title a { color:#233d21;  font-size:13px;}
hr { border:2px dotted #ccc; border-bottom:none; }
#tooltip { position:absolute; z-index:3000; border:1px solid #111; background-color:#eee; padding:5px; }
#tooltip h3, #tooltip div, #tooltip p { margin:0; }
.right_para_inside {
    border-top: 1px solid #CCC;
    border-bottom: 1px solid #CCC;
    padding-bottom: 7px;
    padding-top: 7px;
    margin-bottom: 5px;
}
.right_para {
    font-size: 16px;
    color: #999;
}
.right_para .right_para_inside a:hover {
    color: #000;
    text-decoration: none;
}

So my question is, how can I have PHP automatically sort the table BY the close date? 所以我的问题是,如何让PHP在截止日期前自动对表格进行排序?

Wanted to add another detail. 想要添加另一个细节。 The user can resort the table by clicking on the header column (thats what the table sorter plugin is used for.) 用户可以通过单击标题列(这就是表分类器插件的用途)来求助表。

When I use the sortList function, it does sort the table by closed date as it is suppose to, but the coloring of the table (odd, even, odd) is than messed up entirely. 当我使用sortList函数时,它会按照假设关闭日期对表进行排序,但是表的着色(奇数,偶数,奇数)完全搞砸了。

Instead of adding the row class via PHP, use CSS: 而不是通过PHP添加行类,使用CSS:

tr:nth-of-type(even) {
  background-color:#ccc;
}

Using CSS instead of adding classes via PHP should fix your issue. 使用CSS而不是通过PHP添加类应该可以解决您的问题。

You may use widgets for tablesorter plugin to color your rows. 您可以使用小部件tablesorter插件而影响了你的行。 And there is already a default widget zebra , which applies the classes odd and even to rows. 并且已经有一个默认的小部件zebra ,它将oddeven类应用于行。

$("#bin").tablesorter({ 
    widgets: ['zebra'] 
}); 

More in plugin documentation . 更多插件文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM