PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 22, 2022

[FIXED] I don't understand how to list JSON with params like ?currentPage=0&pageSize=18

 January 22, 2022     json, php     No comments   

Issue

Can someone please show me some examples or show me how, I already got the parms working but I don't know how I would do the api what im trying to do is make it get the data from my database with the currentpage and pagesize/limit I have got limit to work but I cant figure out how I would do current page

This is what I've got so far

<?php
require ($_SERVER['DOCUMENT_ROOT']."/core/config.php");
require ($_SERVER['DOCUMENT_ROOT']."/core/session.php");

if ($logged == false) {
    header('Location: /');
    exit;
}

    $limit = mysqli_real_escape_string($conn, $_GET['pageSize']);

if(intval($limit)) {
    $sqlSearch = "SELECT * FROM `users` ORDER BY `id` DESC LIMIT $limit";
    $result = $conn->query($sqlSearch);

    while ($userrows = $result->fetch_assoc()) {
        $username = $userrows['username'];

        $apiresp = array('success' => "true", 'username' => $username);
        echo json_encode($apiresp);
    }
}
?>

Solution

You probably want to use offset

SELECT * FROM `users` ORDER BY `id` DESC LIMIT $limit,". ($page -1)*$limit;


Answered By - Peter Trencansky
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing