PHP Class / Module

Web, Mobile & Software Dev Other - Software Development Posted 2 years ago

Hourly Job

Hours to be determined
Less than 1 week

Details

Looking for a contractor who can create a class or function which will handle the paging in my code. Below is what my code looks like write now, i want the code which takes care of the paging part outside the actual code and be able to reuse the code

function get_country()
{
    global $link, $txt, $initial_limit;

    $tokencheck = auth_checktoken($_GET['session_token']);
    if (!is_array($tokencheck))
        return(
                json_encode(array('status' => 0, 'message' => $txt[2]))
                );

    $country = mysqli_real_escape_string($link, trim($_GET['filter']) . '%');

    $offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
    $limit = isset($_GET['limit']) ? intval($_GET['limit']) + 1 : 26;


    $query = db($link, "call sp_country_SEL('$country', $limit, $offset)");



    $records = mysqli_num_rows($query);

    if ($records == 0)
        return( json_encode(array('status' => 0, 'message' => $txt[14])));

    $previouspage = 1;
    $nextpage = 1;
    if ($offset == 0)
        $previouspage = '';
    if ($records < $limit)
        $nextpage = '';

    $current_record = 1;

    $output = '{"status" : 1, "data": ';
    $array = array();

    $limit--;

    $to_fetch = $records > $limit ? $limit : $records;

    while ($current_record <= $to_fetch) {
        $record = mysqli_fetch_array($query);

        list($cc_iso, $tld, $country_name, $image) = $record;
        $array[] = array(
            'cc_iso' => $cc_iso,
            'country_name' => $country_name,
            'image' => $image,
        );
        $current_record++;
    }
    $output .= json_encode($array, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);

    if ($previouspage) {
        // Previous page exists
        $newoffset = $offset - $limit;
        $previouspage = 'https://' . $_SERVER['HTTP_HOST'] . '/lookup?country&offset=' . $newoffset . '&limit=' . $limit;
    }
    if ($nextpage) {
        // Link for next page
        $newoffset = $offset + $limit;
        $nextpage = 'https://' . $_SERVER['HTTP_HOST'] . '/lookup?country&offset=' . $newoffset . '&limit=' . $limit;
    }
    if ($previouspage || $nextpage) {
        $pagging = ', "paging": { ';
        if ($nextpage)
            $pagging .= '"next" : "' . $nextpage . '"';
        if ($previouspage && $nextpage)
            $pagging .= ' , ';
        if ($previouspage)
            $pagging .= '"previous" : "' . $previouspage . '"';
        $pagging .= '}';
    } else {
        $pagging = '';
    }
    $output .= $pagging . '}';
    return($output);

}

if (isset($_GET['country'], $_GET['filter'], $_GET['session_token'] ))
        echo get_country();
?>

Skills Required:

Client Activity on this Job

Last Viewed: 2 years ago

Applicants: 14

Hired: 1

Other open jobs by this client ({{ jobsCount }})


About the Client

(4.53) 13 reviews

United States
Laguna Niguel 06:14 PM

53 Jobs Posted
48% Hire Rate, 1 Open Job

$8,441 Total Spent
26 Hires, 3 Active

$19.78/hr Avg Hourly Rate Paid
423 Hours

Member Since Apr 15, 2012