JasonBaier / ci3-fire-starter

NOW THAT CODEIGNITER 4 HAS BEEN RELEASED, THIS PROJECT WILL NOT BE UPDATED ANYMORE. CI3 Fire Starter is a CodeIgniter3 skeleton application that includes jQuery and Twitter Bootstrap. It is intended to be light weight, minimalistic and not get in your way of building great CodeIgniter 3 applications.
MIT License
153 stars 152 forks source link

sort by number #30

Closed satrioaw closed 8 years ago

satrioaw commented 8 years ago

let say I add number one field example in user list there I add the number counting by

 <td>
                    <a href="<?php echo current_url(); ?>?sort=no&dir=<?php echo (($dir == 'asc' ) ? 'desc' : 'asc'); ?>&limit=<?php echo $limit; ?>&offset=<?php echo $offset; ?><?php echo $filter; ?>"><?php echo lang('users col no'); ?></a>
                    <?php if ($sort == 'no') : ?><span class="glyphicon glyphicon-arrow-<?php echo (($dir == 'asc') ? 'up' : 'down'); ?>"></span><?php endif; ?>
                </td>
  <?php if ($total) : ?>
                <?php $i = 1;
                 foreach ($users as $user) : ?>
                    <tr>
                        <td<?php echo (($sort == 'no') ? ' class="sorted"' : ''); ?>>
                            <?php echo $i; ?>
                        </td>
                        <td<?php echo (($sort == 'id') ? ' class="sorted"' : ''); ?>>
                            <?php echo $user['id']; ?>
                        </td>
                        <td<?php echo (($sort == 'username') ? ' class="sorted"' : ''); ?>>
                            <?php echo $user['username']; ?>
                        </td>
                        <td<?php echo (($sort == 'first_name') ? ' class="sorted"' : ''); ?>>
                            <?php echo $user['first_name']; ?>
                        </td>
                        <td<?php echo (($sort == 'last_name') ? ' class="sorted"' : ''); ?>>
                            <?php echo $user['last_name']; ?>
                        </td>
                        <td<?php echo (($sort == 'status') ? ' class="sorted"' : ''); ?>>
                            <?php echo ($user['status']) ? '<span class="active">' . lang('admin input active') . '</span>' : '<span class="inactive">' . lang('admin input inactive') . '</span>'; ?>
                        </td>
                        <td<?php echo (($sort == 'is_admin') ? ' class="sorted"' : ''); ?>>
                            <?php echo ($user['is_admin']) ? lang('core text yes') : lang('core text no'); ?>
                        </td>
                        <td>
                            <div class="text-right">
                                <div class="btn-group">
                                    <?php if ($user['id'] > 1) : ?>
                                        <a href="#modal-<?php echo $user['id']; ?>" data-toggle="modal" class="btn btn-danger" title="<?php echo lang('admin button delete'); ?>"><span class="glyphicon glyphicon-trash"></span></a>
                                    <?php endif; ?>
                                    <a href="<?php echo $this_url; ?>/edit/<?php echo $user['id']; ?>" class="btn btn-warning" title="<?php echo lang('admin button edit'); ?>"><span class="glyphicon glyphicon-pencil"></span></a>
                                </div>
                            </div>
                        </td>
                    </tr>
                <?php 
                $i++;
                endforeach; ?>
            <?php else : ?>
                <tr>
                    <td colspan="7">
                        <?php echo lang('core error no_results'); ?>
                    </td>
                </tr>
            <?php endif; ?>

then i try sorting is error found,

A Database Error Occurred

Error Number: 1054

Unknown column 'no' in 'order clause'

SELECT SQL_CALC_FOUND_ROWS * FROM users WHERE deleted = '0' ORDER BY no asc LIMIT 0, 10

Filename: models/Users_model.php

Line Number: 57

I think it because sorting method by calling database. how made sorting number without querying from database?

JasonBaier commented 8 years ago

You can't sort it without querying the database. I did not use a Javascript data grid that stores the entire record set. It only queries what is requested, and the keys must be the names of database columns within the users table. In your case, there is no column 'no'. I honesty don't see a need to sort by a number column since there's already a user id column that will sort asc/desc. Your number column is just showing the sequenced order of display. So I would just change your header so it isn't clickable to this:

<td>
    <?php echo $i; ?>
</td>

and in the 'else', change the colspan since you are adding another column:

<td colspan="8">