Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8447651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:13:55+00:00 2026-06-10T10:13:55+00:00

I extended some tables with pagination and the possibility to sort columns. Everything is

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:13:57+00:00Added an answer on June 10, 2026 at 10:13 am

    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 🙂

    • edit:
      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 😉
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working with Doctrine_Record classes that autoload just fine for a while; but
I extended JDialog to create a custom dialog where the user must fill some
Specifically, I'm using the Linux command: $ find . -regextype posix-extended -regex '<some regex>'
I've searched similar topics but haven't found what I need.. I extended Users model
I have some extended property column descriptions in a SQL Server 2008 database, and
I am working with some code that uses an OleDbConnection to load data from
I have a database I'm working on that has some queries showing up in
I've been working on a Play application (1.2.4) which will do some crunching on
I have two MySQL tables describing data that can be extended into subclasses, one
Do you generate your data dictionary? If so, how? I use extended procedures in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.