如何从数据库中按category_id列出产品并按item_id排序?

时间:2019-05-30 05:36:58

标签: html mysql perl cgi

我有项目表,其中的列为:

  1. item_id = 01,02,03,04,05,06,07,08 ....
  2. ctgr_id = 01,01,01,02,02,03,03,03 ....
  3. item_name = Wcar,Bcar,Rcar,Abus,Bbus,Qtruck,Ttruck,Ftruck ....

还有一个名为 ctgr 的表,其中的列为:

  1. ctgr_id = 01、02、03 ...
  2. ctgr_name =汽车,公共汽车,卡车...

现在编码显示的产品如下:

    汽车
    • 01-Wcar
    汽车
    • 02-Bcar
    汽车
    • 03-Rcar
  • 巴士
    • 04-Abus
  • 巴士
    • 05-Bbus
  • 卡车
    • 06-Qtruck
  • 卡车
    • 07-卡车
  • 卡车
    • 08-卡车

但是我想显示如下产品:

  • 汽车

    • 01-Wcar
    • 02-Bcar
    • 03-Rcar
  • 公交车

    • 04-Abus
    • 05-Bbus
  • 卡车

    • 06-Qtruck
    • 07-卡车
    • 08-卡车

来自index.cgi

sub action_index {
    my $self = shift;
    $self->set_property(paginate_row_count => 0);
    my $m_item = $self->model('item');

    $self->set_data(
        'ctgrs',
        $self->model('ctgr')->get_list('del_flg = 0 and show_web = 1')
    );

    $m_item->set_input_params;
    $self->search_execute($m_item->get_list_sql->where("v_item.item_flg = 1"));
    $self->set_data('cart',$self->get_list_cart);
}

来自index.html

<table>
    [% if(@{$c{list}}>0) %]
        [% foreach my $rec (@{$c{list}}) %]

            [% if($rec->{is_item_public}==1) %]

                [% foreach my $i (@{$c{list}}-1) %]
                    <tr>
                        <td align="center">
                        [# $rec->{ctgr_name} #]
                        </td>
                    </tr>
                [% end %]
                    <tr>    
                        <td class="item">
                        [# $rec->{item_id} #]<br>
                        [# $rec->{item_name} #]<br>
                        </td>
                    </tr>
            [% end %]
         [% end %]
    [% else %]
    <tr>
    <td>no data</td>
    </tr>
    [% end %]
</table>

来自文件??

sub search_execute {
    my $self = shift;
    my $sql  = shift;

    my @wheres = ();

    foreach my $key (keys %{$self->{search_params}}){
        my $value = $self->param("search/${key}");
        if(defined $self->{search_params}->{$key}->{func}){
            $self->{search_params}->{$key}->{func}($sql,$value);
        }
        elsif (defined $value && $value ne '') {
            my $type = defined $self->{search_params}->{$key}->{type}
              ? $self->{search_params}->{$key}->{type}
              : '= ?';
            my $field = defined $self->{search_params}->{$key}->{field}
              ? $self->{search_params}->{$key}->{field}
              : $key;
            $value =~s/^\s+//;
            $value =~s/\s+$//;
            my @where_or = ();
            foreach (split(/\s+/,$value)) {
                my $replace = $self->{search_params}->{$key}->{replace} || '?';
                $replace =~ s/\?/$_/;
                push @where_or, ["${field} ${type}",$replace];
            }
            if ($self->{is_search_where_or}) {
                push @wheres,@where_or;
            }
            else {
                $sql->where_or( @where_or );
            }
        }
        elsif ( defined $self->{search_params}->{$key}->{default} ) {
            my $field = $self->{search_params}->{$key}->{field} || $key;
            my $type = $self->{search_params}->{$key}->{type} || '= ?';
            if( $self->{is_search_where_or} ) {
                push @wheres,
                  ["${field} ${type}", $self->{search_params}->{$key}->{default}];
            }
            else{
                $sql->where(
                    ["${field} ${type}", $self->{search_params}->{$key}->{default}] );
            }
        }
    }
    if( $self->{is_search_where_or} ) {
        $sql->where_or(@wheres) if(@wheres);
    }
    my $tmp_orderby = $sql->{ORDERBY};
    $sql->{ORDERBY} = [];
    my $total = $sql->get_count(@_); 
    if($total>0){
        my $page = $self->param('page') || 1;
        $page = 1 unless(($page=~/^\d+$/) && $page>=1);
        my $offset = ($page-1)*$self->{paginate_row_count};
        $sql->{ORDERBY} = $tmp_orderby; 
        if($self->{paginate_row_count}>0){
            $sql->limit($offset,$self->{paginate_row_count});
        }
        my $sth = $sql->execute(@_);
        my @data = ();
        while(my $ra = $sth->fetchrow_hashref){
            $self->filter_in_all($ra);
            push @data,$ra;
        }
        $self->set_data('list',\@data);
    }
}

0 个答案:

没有答案