Viewed   116 times
$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("Test");

$strSQL = "SELECT * FROM UserAddedRecord WHERE (Name LIKE '%".$getname."%' and State LIKE '%".$getstate."%' and Cell LIKE '%".$getcell."%' and Custgroup LIKE '%".$getgroup."%') AND user_id=$id";

$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);

$Per_Page = 5;   

if (!isset($_GET['Page'])) {
    $Page = 1;
} else {
    $Page = $_GET['Page'];
}

$Prev_Page = $Page - 1;
$Next_Page = $Page + 1;

$Page_Start = (($Per_Page * $Page) - $Per_Page);
if ($Num_Rows <= $Per_Page) {
    $Num_Pages = 1;
} elseif (($Num_Rows % $Per_Page) == 0) {
    $Num_Pages = ($Num_Rows / $Per_Page) ;
} else {
    $Num_Pages = ($Num_Rows / $Per_Page) + 1;
    $Num_Pages = (int) $Num_Pages;
}


$strSQL .=" order  by addedrec_ID DESC LIMIT $Page_Start , $Per_Page";
$objQuery  = mysql_query($strSQL) or trigger_error(mysql_error());;

if ($Prev_Page) {
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'><< Back</a> ";
}

for ($i=1; $i <= $Num_Pages; $i++) {
    if ($i != $Page) {
        echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$i&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>$i</a> ";
    } else {
        echo "<b> $i </b>"; 
    }
}

if ($Page!=$Num_Pages) {
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>Next>></a> ";        
}

mysql_close($objConnect);

This is the code I use to create pagination page for data search.But I just noticed that I have too many records then it will have too many page numbers.I'm trying to limit the page number shown,and look for many examples ..it have many ways to do it but I still don't have idea how to limit it for my method...

 Answers

2

Ok if you mean show something like

Prev 1 2 3 4 5 6 .. 40 41 Next 
Prev 1 2 .. 6 7 8 9 10 .. 40 41 Next

First thing we need to is create a function that can process what we need to output the pagination. Heres a function I use and it works well.

function get_paging_info($tot_rows,$pp,$curr_page)
{
    $pages = ceil($tot_rows / $pp); // calc pages

    $data = array(); // start out array
    $data['si']        = ($curr_page * $pp) - $pp; // what row to start at
    $data['pages']     = $pages;                   // add the pages
    $data['curr_page'] = $curr_page;               // Whats the current page

    return $data; //return the paging data

}

Now this function is pretty solid and works very well for me.

So you pass this function

  • $tot_rows = counted rows for query
  • $pp = items per page
  • $curr_page = the current page number

Ok, now that you have the data you need, you'll need to display it.

Heres what I use and please read it before you think, 'ah, it's too long'. It's actually very simple.

Heres a snapshot of what it will return

    <!-- Create the query -->
    <?php $count = mysql_fetch_assoc( mysql_query ( "SELECT COUNT( rows ) as count FROM table" ) ) ;

    <?php $count = $count[0]['count']; ?>

<!-- Call our function from above -->
<?php $paging_info = get_paging_info($count,5,34); ?>


<p>
    <!-- If the current page is more than 1, show the First and Previous links -->
    <?php if($paging_info['curr_page'] > 1) : ?>
        <a href='' title='Page 1'>First</a>
        <a href='' title='Page <?php echo ($paging_info['curr_page'] - 1); ?>'>Prev</a>
    <?php endif; ?>



    <?php
        //setup starting point

        //$max is equal to number of links shown
        $max = 7;
        if($paging_info['curr_page'] < $max)
            $sp = 1;
        elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
            $sp = $paging_info['pages'] - $max + 1;
        elseif($paging_info['curr_page'] >= $max)
            $sp = $paging_info['curr_page']  - floor($max/2);
    ?>

    <!-- If the current page >= $max then show link to 1st page -->
    <?php if($paging_info['curr_page'] >= $max) : ?>

        <a href='' title='Page 1'>1</a>
        ..

    <?php endif; ?>

    <!-- Loop though max number of pages shown and show links either side equal to $max / 2 -->
    <?php for($i = $sp; $i <= ($sp + $max -1);$i++) : ?>

        <?php
            if($i > $paging_info['pages'])
                continue;
        ?>

        <?php if($paging_info['curr_page'] == $i) : ?>

            <span class='bold'><?php echo $i; ?></span>

        <?php else : ?>

            <a href='' title='Page <?php echo $i; ?>'><?php echo $i; ?></a>

        <?php endif; ?>

    <?php endfor; ?>


    <!-- If the current page is less than say the last page minus $max pages divided by 2-->
    <?php if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2))) : ?>

        ..
        <a href='' title='Page <?php echo $paging_info['pages']; ?>'><?php echo $paging_info['pages']; ?></a>

    <?php endif; ?>

    <!-- Show last two pages if we're not near them -->
    <?php if($paging_info['curr_page'] < $paging_info['pages']) : ?>

        <a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.($paging_info['curr_page'] + 1); ?>' title='Page <?php echo ($paging_info['curr_page'] + 1); ?>'>Next</a>

        <a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.$paging_info['pages']; ?>' title='Page <?php echo $paging_info['pages']; ?>'>Last</a>

    <?php endif; ?>
</p>
Tuesday, December 20, 2022
2

Laravel paginator automatically checks for the the value of page in query string and uses it to paginate the results. The result also automatically generates the next and previous links to help you add them directly. You don't need to change anything to make it work.

In your case you can use $articles->links() in your view to generate the pagination navigation buttons. But if you want to manually set the page then you can do this.

$articles = Articles::paginate(5, ['*'], 'page', $pageNumber);

The default paginate method takes the following parameters.

public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);

The default convention is

mypage.com/articles?page=2
mypage.com/articles?page=3

Also if you use $articles->links() to generate the navigation button, you can also customize the css.

Check out https://laravel.com/docs/5.4/pagination for more info

Thursday, November 3, 2022
5

row is an associative array with an entry for every column in the result set. Since there's only one column there, count($row) returns 1. Instead, you should just access the only column there:

$row = $results->fetch_assoc();
$recordCount = $row["COUNT(id)"];
Monday, October 24, 2022
 
krizz
 
1

This logic confuses me. If $tl is greater than the total, would it not simply be equal to the total?

if($tL > $total)
{
    $tL = (($page - 1) * 20) + ($total % 20);
}

Instead why not:

$tL = ($tL > $total) ? $total : $tl;

Also, I would use SQL to find your total instead of mysql_num_rows. It should prove a little more reliable (especially when you stop using the really old mysql extension). Maybe it will also return the correct total.

SELECT COUNT(id) FROM coupons WHERE zone = '$zone' AND dateExp > CURDATE()

Other than this I am not seeing anything obvious that would explain an incorrect number of rows.

Wednesday, October 5, 2022
1

Currently, the pagination support is based on continuation token only.

Find below some interesting discussion and feature request about this limitation:

  • https://github.com/Azure/azure-documentdb-dotnet/issues/377
  • https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/6350987--documentdb-allow-paging-skip-take

--- Continuation Token Example ---

The following example illustrates a method (very similar to yours) that queries documents based on the desired page number, page size and continuation token:

    private static async Task<KeyValuePair<string, IEnumerable<CeleryTask>>> QueryDocumentsByPage(int pageNumber, int pageSize, string continuationToken)
    {
        DocumentClient documentClient = new DocumentClient(new Uri("https://{CosmosDB/SQL Account Name}.documents.azure.com:443/"), "{CosmosDB/SQL Account Key}");

        var feedOptions = new FeedOptions {
            MaxItemCount = pageSize,
            EnableCrossPartitionQuery = true,

            // IMPORTANT: Set the continuation token (NULL for the first ever request/page)
            RequestContinuation = continuationToken 
        };

        IQueryable<CeleryTask> filter = documentClient.CreateDocumentQuery<CeleryTask>("dbs/{Database Name}/colls/{Collection Name}", feedOptions);
        IDocumentQuery<CeleryTask> query = filter.AsDocumentQuery();

        FeedResponse<CeleryTask> feedRespose = await query.ExecuteNextAsync<CeleryTask>();

        List<CeleryTask> documents = new List<CeleryTask>();
        foreach (CeleryTask t in feedRespose)
        {
            documents.Add(t);
        }

        // IMPORTANT: Ensure the continuation token is kept for the next requests
        return new KeyValuePair<string, IEnumerable<CeleryTask>>(feedRespose.ResponseContinuation, documents);
    }

Now, the following example illustrates how to retrieve documents for a given page by calling the previous method:

    private static async Task QueryPageByPage()
    {
        // Number of documents per page
        const int PAGE_SIZE = 3;

        int currentPageNumber = 1;
        int documentNumber = 1;

        // Continuation token for subsequent queries (NULL for the very first request/page)
        string continuationToken = null;

        do
        {
            Console.WriteLine($"----- PAGE {currentPageNumber} -----");

            // Loads ALL documents for the current page
            KeyValuePair<string, IEnumerable<CeleryTask>> currentPage = await QueryDocumentsByPage(currentPageNumber, PAGE_SIZE, continuationToken);

            foreach (CeleryTask celeryTask in currentPage.Value)
            {
                Console.WriteLine($"[{documentNumber}] {celeryTask.Id}");
                documentNumber++;
            }

            // Ensure the continuation token is kept for the next page query execution
            continuationToken = currentPage.Key;
            currentPageNumber++;
        } while (continuationToken != null);

        Console.WriteLine("n--- END: Finished Querying ALL Dcuments ---");
    }
Monday, September 5, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :