I extended some tables with pagination and the possibility to sort columns. Everything is working fine so far, but I could use the very same code on up to 10 pages, so I thought about using functions, but I’m not sure if it’s reasonable (performance etc.) to use a function for every part or if I should combine several things, so I need some advice..
Currently the code for one page looks like this (every number could be a function):
$limit = 30;
1.
$sql = 'SELECT
COUNT(`news_id`)
FROM
`news`';
$stmt = $db->prepare($sql);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
$stmt->free_result();
$stmt->close()
2.
if ($count === 0)
{
$max_pages = 1;
}
else
{
$max_pages = (int)ceil($count/$limit);
}
3.
if (isset($_GET['page']))
{
if (preg_match('/^[0-9]{1,}$/', $_GET['page'])
&& ($_GET['page'] > 1
&& $_GET['page'] <= $max_pages))
{
$current_page = (int)$_GET['page'];
$offset = ($current_page - 1) * $limit;
}
else
{
header('location: http://' .SERVERNAME. '/admin/news/');
exit;
}
}
else
{
$current_page = 1;
$offset = 0;
}
4.
$valid_sort = array('id', 'date', 'title');
if (isset($_GET['sort']))
{
if(in_array($_GET['sort'], $valid_sort))
{
$sort = $_GET['sort'];
}
else
{
header('location: http://' .SERVERNAME. '/admin/news/');
exit;
}
}
else
{
$sort = 'id';
}
5.
$valid_order = array('asc', 'desc');
if (isset($_GET['order']))
{
if(in_array($_GET['order'], $valid_order))
{
$current_order = $_GET['order'];
}
else
{
header('location: http://' .SERVERNAME. '/admin/news/');
exit;
}
}
else
{
$current_order = 'desc';
}
Since 4 and 5 are almost identical I could call the same function with different parameters here, but still I’m not sure if it really makes sense to call up to 5 functions..
Finally I need the following variables: $limit, $count, $max_pages, $current_page, $offset, $sort, $current_order
EDIT : Just to make it clear, the question is not how functions would work or if it’s possible to use them here.. the question is, how complex should a function be relating to what I want to do with it? For example I could write one function with 120 lines and 8 parameters but I would only need to call 1 function / page… or I could keep it short and simple (1 function for 1 purpose / variable) but then I would need to call more functions / page..
Long story short. Yes.
It’s fairly simple. If it’s possible to put something in a function and make it re-usable, do so! It will organise your code and you can re-use it more easily. Otherwise you will have to rewrite your code afterwards to put it in a function anyway. So yes, do so and make it accessible for every single file if necessarily.
Also, if it’s possible for you, try to start with functions to understand them completely, if you do, start with OOP! It will make your life so much easier. I hope this helps.
I am not going to rewrite your code, I think it’s good practice for yourself if you do it on your own. If it’s hard and you can’t finish it yourself, feel free to ask on SO 🙂
About the performance you are talking about, don’t worry. A function is maybe 0.00000000001 nanoseconds slower than copy pasting. But if you consider readability and programming into a count, use functions. If you don’t care how your code looks like, and for a simple tweak you’d like to spend 5 days, don’t use functions 😉