You are here: C97net • 5.02 Database Functions
Create a multiple page output from an SQL query.
function sql_multipage ($table, $columns, $where, $order_by, $cur_page,
$script_name = '', $per_page = '')
This function will create a multiple page output from an SQL query. This is useful if the expected SQL result will be too long to fit a page. This is almost the same with MYSQL's LIMIT, only easier, and the output has been paginated.
$table = table name.
$columns = selected columns.
$where = MySQL WHERE conditions.
$order_by = MySQL ORDER BY.
$cur_page = current page.
$script_name = no longer used, kept for compatibility with older qE scripts.
$per_page = number of items per page, the default is defined in ACP > Configuration (usually 20).
Calling this function will return an array of $columns from the $table. You can later use foreach () function to display the results. Also this function will populate $txt['pagination'] with page list, based on skins/default/pagination.tpl & skins/_admin/pagination.tpl depending on current mode.
Example 1. test.php
<?php
require './includes/user_init.php';
// now load the output tpl
$tpl = load_tpl ('test.tpl');
// all block output must be contained in $var['block_list']
// or any block_name defined
$txt['block_list'] = '';
// get entries from db
$p = get_param ('p');
$foo = sql_multipage ($db_prefix.'user', '*', '', 'user_id', $p);
foreach ($foo as $row)
{
// convert the date from SQL format to human format
$row['user_since'] = convert_date ($row['user_since']);
// create the block
$txt['block_list'] .= quick_tpl ($tpl_block['list'], $row);
}
// output
$txt['main_body'] = quick_tpl ($tpl, $txt);
flush_tpl ();
?>
test.tpl
<h1>Welcome to test page!</h1>
<!-- BEGINIF $login -->
<p>You are logged in as: {$current_user_id}</p>
<!-- ELSE -->
<p>You are not logged in.</p>
<!-- ENDIF -->
<table class="table_2" border="0">
<tr>
<th>User ID</th>
<th>Email</th>
<th>Level</th>
<th>Since</th>
<th>Notes</th>
</tr>
<!-- BEGINBLOCK list -->
<tr>
<td>{$user_id}</td>
<td>{$user_email}</td>
<td>{$user_level}</td>
<td>{$user_since}</td>
<td>{$user_notes}</td>
</tr>
<!-- ENDBLOCK -->
</table>
{$pagination}
The sample code will display list of registered users. If the users more than 20, it will display page list, with a link to other pages.
There is no comment. Why not be the first?