Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP a little help

medib

New Coder
hi
I have this problem I'm using laravel livewire and I want to paginate a table using JSON data that I got from curl. I wonder if anyone can help. i don't know if it's ok to post the link just to give you an idea of what i want. i dont want to paginate from the database
 

Attachments

  • Screenshot 2022-10-23 204827.jpg
    Screenshot 2022-10-23 204827.jpg
    69 KB · Views: 10
Did you try the simple html Js pagination, get the table if from DB,


If is from DB you can use Limit range in SQL;


Example


<?php
PHP:
global $html;
global $total_records;
global $total_pages;
global $page;


$results_per_page = (int)10;


 if(isset($_GET['page']))
 {
      $page = (int) $_GET['page'];
      $page = (int) $page -1;
      $page = (int) $page * $results_per_page;
  }
else $page =0;


$SQL = 'SELECT COUNT(id) FROM  TABLE X  WHERE';

$total_records = $count;  // VALUE WITCH IS THE RETURN OF THIS SQL;

// Lets Calculate the total of pages

 
if($total_records>$results_per_page)
{
    $total_pages = ceil($total_records / $results_per_page); //get the total records and divide from the amount of data  you want show in this case will be 10  
}
else $total_pages=1;

// Now get the data

$SQL= 'SELECT  (FIELDS) FROM YourTable a (LEFT, RIGHT) JOIN  Table b   Limit '.$page.' , '.$results_per_page


Now we have to use the JS


JavaScript:
   $(document).ready(function ()
    {
        totalPages = <?=$total_pages?>; //This tag <?= passes the value from PHP to Js
        totalRecords = "<?=$total_records?>";
        if((totalRecords)>10)
        {
            for (let i = 1; i < totalPages; i++)
            {
                $(".pagination").append('<a id="'+(i+1)+'" onclick="toNextPage(this);">'+(i+1)+'</a>');
                if(i+1===totalPages)
                {
                    $(".pagination").append('<a onclick="toLastPage(this);">&raquo;</a>');
                }
            }
           
        }
   
        $("#navpage").css("visibility", "visible");
        $("#navpage").css("display", "block");
        $("#navpage").removeClass("hidden").addClass("active");
       
     
    function toFirstPage()
    {
        location.href='url?page='+1;
    }

    function toLastPage()
    {
        location.href='url?page='+totalPages;
    }

    function toNextPage(page)
    {
        last_id= page.id-1;
        location.href='url?page='+page.id;
        event.preventDefault();
     
    }
     

    });







?>
 
Back
Top Bottom