Я удивлен, что никто не упомянул Python termcolor модуль . Использование довольно просто:
from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')
Или в Python 3:
print(colored('hello', 'red'), colored('world', 'green'))
Это не может быть достаточно сложно, однако, для игрового программирования и "цветных блоков", которые Вы хотите сделать...
Соответствует оператору LIMIT
:
SELECT something FROM table LIMIT $limit OFFSET $offset;
//or alternatively
SELECT something FROM table LIMIT $offset,$limit;
Другими словами , выберите что-нибудь из таблицы, но дайте мне только $ limit
записи, начиная с записи $ offset
.
$ offset = ($ page_no - 1) * $ limit
$ page_no
основан на 1.
Дополнительная информация в документации MySQL:
ОТКАЗ: $ limit
и $ offset
используются в этом вопросе только для облегчения понимания. Конечно, вы не хотите строить запрос без надлежащего экранирования значения.
That particular comment unfortunately confuses two common ways to consider pagination or grouping in queries. The SELECT syntax, as Andrew describes, allows an OFFSET parameter, the number of items to skip before returning anything. However, it is most often used with pagination, as in the pagination library from which your quotation was taken. In this case, it is more useful to ask for a specific page number.
To compare the two, consider a case where you've made a search and have gone to page 3, with 10 items per page. Items 1-20 were on the first two pages; therefore, the OFFSET parameter will be 20:
SELECT * FROM table WHERE searched LIMIT 10 OFFSET 20
or
SELECT * FROM table WHERE searched LIMIT 20,10
Unfortunately, the $page_no
parameter in the cited example probably wants to be 3, the page number. In that case, the SQL offset will have to be calculated. Given that get_items
does not seem to be a standard model method, it's probably calculated in there; alternatively, the pagination library seems to have a property called sql_offset
which probably calculates it for you. Either way, the calculation is easy enough:
$offset = max($page_no - 1, 0) * $limit;