PHP Class / Module

PHP Class / Module

Closed - This job posting has been filled.

Job Description

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();
?>

Other open jobs by this client