#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    831
    Rep Power
    0

    Mixed Pagination Mystery


    Php Buddies,

    Checkout the following code. It is a pagination page.
    Why is the pagination part being shown twice ? Once at the middle of the page and once at the end ?
    I set it to show 2 results per page. I got the script to pull results from mysql and the results number 3 rows. That means 2 pages of results should be shown.
    2 results on the first page and the final single result on the 2nd page.
    For some reason the 2nd page's single result appears on the first page after the first page's 2 results and another pagination appears at the end of the 3rd result.
    For some reason, the script is combining 2 pages together! Why is that ?
    I used similar pagination code for 2 more different pages with 2 more different queries with no problems.
    But this one is a big problem. Going round in circles for about 2hrs now!

    PHP Code:
    <?php 

    //Required PHP Files. 
    include 'config.php'
    include 
    'header.php'
    include 
    'account_header.php'

    if (!
    $conn

        
    $error mysqli_connect_error(); 
        
    $errno mysqli_connect_errno(); 
        print 
    "$errno$error\n"
        exit(); 

    else 
    {     
        
    //Get the Page Number. Default is 1 (First Page). 
        
    $page_number $_GET["page_number"]; 
        if (
    $page_number == ""
        { 
            
    $page_number 1
        } 

        
    $sender_username "$social_network_admin_username"
        
    $recipient_username "$user"
        
    $links_per_page 2
        
    $max_result 100
        
    //$offset = ($page_number*$links_per_page)-$links_per_page; 
        
    $offset = ($page_number-1)*$links_per_page
        
        
    $query_1 "SELECT COUNT(*) FROM notices ORDER BY id LIMIT ? OFFSET ?"
        
    $stmt_1 mysqli_prepare($conn,$query_1);
        
    mysqli_stmt_bind_param($stmt_1,'si',$links_per_page,$offset); 
        
    mysqli_stmt_execute($stmt_1); 
        
    $result_1 mysqli_stmt_bind_result($stmt_1,$matching_rows_count); 
        
    mysqli_stmt_fetch($stmt_1); 
        
    mysqli_stmt_free_result($stmt_1); 
        
        
    $total_pages ceil($matching_rows_count/$links_per_page); 
        
    $query_2 "SELECT id,date_and_time,recipient_username,sender_username,notice FROM notices n JOIN (SELECT recipient_username,MAX(date_and_time) as date_and_time FROM notices GROUP by recipient_username) mx USING (recipient_username,date_and_time)";
        
    $stmt_2 mysqli_prepare($conn,$query_2); 
        
    mysqli_stmt_execute($stmt_2); 
        
    $result_2 mysqli_stmt_bind_result($stmt_2,$id,$date_and_time,$recipient_username,$sender_username,$notice); 
        
    mysqli_stmt_fetch($stmt_2);     
        
    ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"> 
        <html> 
        <head> 
        <meta content="text/html; charset=ISO-8859-1" http-equiv=" content-type"> 
        <title><?php echo "$site_name User $user Notices in $server_time time."?></title> 
        </head> 
        <body> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User $user Notices in $server_time time."?></span></align> 
        <br> 
        <br> 
        <table width="1500" border="0" cellpadding="5" cellspacing="2" bgcolor="#666666"> 
        <?php if(!$stmt_2
        { 
            
    ?> 
            <tr> 
            <td bgcolor="#FFFFFF">No record found! Try another time.</td> 
            </tr> 
            <?php 
        

        else 
        { 
            if((
    $offset+1)<=$max_result
            { 
                
    printf("<b> %d Result Found ...</b>\n",$matching_rows_count); ?><br> 
                <br> 
                <tr name="headings"> 
                <td bgcolor="#FFFFFF" name="column-heading_submission-number">Submission Number</td> 
                <td bgcolor="#FFFFFF" name="column-heading_logging-server-date-and-time">Date & Time in <?php echo "$server_time?></td> 
                <td bgcolor="#FFFFFF" name="column-heading_recipient-username">To</td> 
                <td bgcolor="#FFFFFF" name="column-heading_sender-username">From</td> 
                <td bgcolor="#FFFFFF" name="column-heading_notice">Notice</td> 
                </tr> 
                <tr name="user-details">             
                <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
                <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
                <td bgcolor="FFFFFF"  name="column-heading_recipient-username"><?php printf("%s",$recipient_username); ?></td> 
                <td bgcolor="#FFFFFF" name="column-heading_sender-username"><?php printf("%s",$sender_username); ?></td> 
                <td bgcolor="#FFFFFF" name="notice"><?php printf("%s",$notice); ?></td> 
                </tr> 
                <?php 
                
    //Use this technique: http://php.net/manual/en/mysqli-stmt.fetch.php 
                
    while(mysqli_stmt_fetch($stmt_2)) 
                { 
                    
    ?> 
                    <tr name="user-details"> 
                    <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
                    <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
                    <td bgcolor="#FFFFFF" name="recipient-username"><?php printf("%s",$recipient_username); ?></td> 
                    <td bgcolor="#FFFFFF" name="sender-username"><?php printf("%s",$sender_username); ?></td> 
                    <td bgcolor="#FFFFFF" name="notice"><?php printf("%s",$notice); ?></td> 
                    </tr> 
                    <?php 
                    ?>
     
                    <tr name="pagination"> 
                    <td colspan="10" bgcolor="#FFFFFF"> Result Pages:             
                    <?php              
                    
    if($page_number $total_pages
                    { 
                        for(
    $i=1;$i<=$total_pages;$i++) //Show Page Numbers in Serial Order. Eg. 1,2,3.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?user=$user&page_number={$i}\">{$i}</a> "
                    }             
                    else 
                    { 
                        for(
    $i=$total_pages;$i>=1;$i--) //Show Page Numbers in Reverse Order. Eg. 3,2,1.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?user=$user&page_number={$i}\">{$i}</a> "
                    } 
                    
    ?> 
                    </td> 
                    </tr> 
                    <?php 
                

            } 
        }
        
    ?>   
        </table>     
        <br> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User $user Notices in $server_time time."?></span></align> 
        <br> 
        </div> 
        <br> 
        </body> 
        </html> 
        <?php 
        
    //Free Result Set. 
        
    mysqli_stmt_free_result($stmt_2); 
        
    //Close Statement Connection. 
        
    mysqli_stmt_close($stmt_2); 
        
    //Close Database Connection. 
        
    mysqli_close($conn); 

    ?>
    The page looks like the following ....
    If I attach an img then you won't see the full page as page a bit long. And so, pasting the generated page's html. if you paste that in your wamp/xamp/lamp then you'll get an idea what the page is looking like in horror:
    PHP Code:

         
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"
        <
    html
        <
    head
        <
    meta content="text/html; charset=ISO-8859-1" http-equiv=" content-type"
        <
    title>Social Network User admin123 Notices in California Usa time.</title
        </
    head
        <
    body
        <
    br
        <
    p align="center"><span style="font-weight:bold;">Social Network User admin123 Notices in California Usa time.</span></align
        <
    br
        <
    br
        <
    table width="1500" border="0" cellpadding="5" cellspacing="2" bgcolor="#666666"
        <
    b6 Result Found ...</b>
    <
    br
                <
    br
                <
    tr name="headings"
                <
    td bgcolor="#FFFFFF" name="column-heading_submission-number">Submission Number</td
                <
    td bgcolor="#FFFFFF" name="column-heading_logging-server-date-and-time">Date Time in California Usa</td
                <
    td bgcolor="#FFFFFF" name="column-heading_recipient-username">To</td
                <
    td bgcolor="#FFFFFF" name="column-heading_sender-username">From</td
                <
    td bgcolor="#FFFFFF" name="column-heading_notice">Notice</td
                </
    tr
                <
    tr name="user-details">             
                <
    td bgcolor="#FFFFFF" name="submission-number">3</td
                <
    td bgcolor="#FFFFFF" name="logging-server-date-and-time">2018-04-11 00:05:00</td
                <
    td bgcolor="FFFFFF"  name="column-heading_recipient-username">admin123</td
                <
    td bgcolor="#FFFFFF" name="column-heading_sender-username">uniquebot</td
                <
    td bgcolor="#FFFFFF" name="notice">RECORD4

    admin123
    ,<br>
                    <
    br
                    
    One of your referrals have confirmed their Id by uploading a video with their personal details. <br>
                    
    We need you to watch their video and verify their personal details to the best of your knowledge. <br>
                    
    You may find their video by <a href="uploads\videos\id_verifications\admin123\Capture 1 (07-01-2016 05-48).wmv">clicking here</a>.
                    <
    br>
                    <
    br>
                    
    Your recruit should be reading-out the following script in (<font color="blue"><b>bold BLUE</fonttext</bbelowword for word and line for line in-front of their camera. <br>
                    
    1. If the details about your recruit are accurate in the script; And <br>
                    
    2. If they manage to read-out every line in the script accurately word for word and line for line; And <br>
                    
    3. Providing the video is no longer than 5 minutes; <br>
                    
    then: <br
                    <
    a href="verify_id.php?verifying_action=verify&verifying_user=admin123">Click to Verify admin123</a>.
                    <
    br>
                    Else, <
    a href="verify_id.php?verifying_action=unverify&verifying_user=admin123">Click to Unverify admin123</a>.<br>
                    <
    br>
                    <
    font color "red">WARNING: Do not verify this recruit if their details are not correct in the script or if they fail to read-out their correct details on the video.
                    
    Doing sowill result in you bearing all the liabilities of the damages they cause using their falsely verified account.</font color><br>
                    <
    br>
                    <
    b><font size="4">SCRIPT START</font size><br>
                    <
    font color "blue">My test Id is0;<br
                    
    My test Domain isgmail.com;<br
                    
    My test Email isadmin@gmail.com;<br
                    
    My test Username isadmin123;<br
                    
    My test First Name isAdmin 1;<br
                    
    My test Middle Name isAdmin 2;<br
                    
    My test Surname isAdmin 3;<br
                    
    My test Gender isfemale;<br
                    
    My test Age Range is71;<br
                    
    My test Religion is: ;<br
                    
    My test Marital Status is: ;<br
                    
    My test Working Status isSelf Employe;<br
                    
    My test Profession isProgrammer;<br>
                    
    My test account_video_verification_code is08b1ec930da1f439ab223771d2ab9db9b8a4e9f6;<br>
                    </
    font color>
                    <
    font size="4">SCRIPT END</font size></b><br>
                    <
    br>
                    </
    td
                </
    tr
                 
                    <
    tr name="user-details"
                    <
    td bgcolor="#FFFFFF" name="submission-number">4</td
                    <
    td bgcolor="#FFFFFF" name="logging-server-date-and-time">2018-04-11 00:05:11</td
                    <
    td bgcolor="#FFFFFF" name="recipient-username">loudgobs</td
                    <
    td bgcolor="#FFFFFF" name="sender-username">admin123</td
                    <
    td bgcolor="#FFFFFF" name="notice">RECORD5

    admin123
    ,<br>
                    <
    br
                    
    One of your referrals have confirmed their Id by uploading a video with their personal details. <br>
                    
    We need you to watch their video and verify their personal details to the best of your knowledge. <br>
                    
    You may find their video by <a href="uploads\videos\id_verifications\admin123\Capture 1 (07-01-2016 05-48).wmv">clicking here</a>.
                    <
    br>
                    <
    br>
                    
    Your recruit should be reading-out the following script in (<font color="blue"><b>bold BLUE</fonttext</bbelowword for word and line for line in-front of their camera. <br>
                    
    1. If the details about your recruit are accurate in the script; And <br>
                    
    2. If they manage to read-out every line in the script accurately word for word and line for line; And <br>
                    
    3. Providing the video is no longer than 5 minutes; <br>
                    
    then: <br
                    <
    a href="verify_id.php?verifying_action=verify&verifying_user=admin123">Click to Verify admin123</a>.
                    <
    br>
                    Else, <
    a href="verify_id.php?verifying_action=unverify&verifying_user=admin123">Click to Unverify admin123</a>.<br>
                    <
    br>
                    <
    b><font color "red">WARNING: Do not verify this recruit if their details are not correct in the script or if they fail to read-out their correct details on the video.
                    
    Doing sowill result in you bearing all the liabilities of the damages they cause using their falsely verified account.</font color></b><br>
                    <
    br>
                    <
    b><font size="4">SCRIPT START</font size><br>
                    <
    font color "blue">My Social Network Id is0;<br
                    
    My Social Network Domain isgmail.com;<br
                    
    My Social Network Email isadmin@gmail.com;<br
                    
    My Social Network Username isadmin123;<br
                    
    My Social Network First Name isAdmin 1;<br
                    
    My Social Network Middle Name isAdmin 2;<br
                    
    My Social Network Surname isAdmin 3;<br
                    
    My Social Network Gender isfemale;<br
                    
    My Social Network Age Range is71;<br
                    
    My Social Network Religion is: ;<br
                    
    My Social Network Marital Status is: ;<br
                    
    My Social Network Working Status isSelf Employe;<br
                    
    My Social Network Profession isProgrammer;<br>
                    
    My Social Network account_video_verification_code isffcff4ee92912fd38600f8761afd3282ae3cbdd5;<br>
                    </
    font color>
                    <
    font size="4">SCRIPT END</font size></b><br>
                    <
    br>
                    </
    td
                    </
    tr
                     
                    <
    tr name="pagination"
                    <
    td colspan="10" bgcolor="#FFFFFF"Result Pages:             
                    <
    a href="/test/search_final_rows_for_each_username.php?user=admin123&page_number=1">1</a> <a href="/test/search_final_rows_for_each_username.php?user=admin123&page_number=2">2</a> <a href="/test/search_final_rows_for_each_username.php?user=admin123&page_number=3">3</a>  
                    </
    td
                    </
    tr
                     
                    <
    tr name="user-details"
                    <
    td bgcolor="#FFFFFF" name="submission-number">5</td
                    <
    td bgcolor="#FFFFFF" name="logging-server-date-and-time">2018-04-11 00:05:31</td
                    <
    td bgcolor="#FFFFFF" name="recipient-username">loudmouth</td
                    <
    td bgcolor="#FFFFFF" name="sender-username">admin123</td
                    <
    td bgcolor="#FFFFFF" name="notice">RECORD6

    Dear admin123
    ,<br>
                    <
    br
                    
    One of your referrals have confirmed their Id by uploading a video with their personal details. <br>
                    
    We need you to watch their video and verify their personal details to the best of your knowledge. <br>
                    
    You may find their video by <a href="uploads\videos\id_verifications\admin123\Capture 1 (07-01-2016 05-48).wmv">clicking here</a>.
                    <
    br>
                    <
    br>
                    
    Your recruit should be reading-out the following script in (<font color="blue"><b>bold BLUE</fonttext</bbelowword for word and line for line in-front of their camera. <br>
                    
    1. If the details about your recruit are accurate in the script; And <br>
                    
    2. If they manage to read-out every line in the script accurately word for word and line for line; And <br>
                    
    3. Providing the video is no longer than 5 minutes; <br>
                    
    then: <br
                    <
    a href="verify_id.php?verifying_action=verify&verifying_user=admin123">Click to Verify admin123</a>.
                    <
    br>
                    Else, <
    a href="verify_id.php?verifying_action=unverify&verifying_user=admin123">Click to Unverify admin123</a>.<br>
                    <
    br>
                    <
    b><font color "red">WARNING: Do not verify this recruit if their details are not correct in the script or if they fail to read-out their correct details on the video.
                    
    Doing sowill result in you bearing all the liabilities of the damages they cause using their falsely verified account.</font color></b><br>
                    <
    br>
                    
    Regards<br>
                    <
    br>
                    
    Admin<br>
                    <
    br>
                    <
    b><font size="4">SCRIPT START</font size><br>
                    <
    font color "blue">My Id is0;<br
                    
    My Domain isgmail.com;<br
                    
    My Email isadmin@gmail.com;<br
                    
    My Username isadmin123;<br
                    
    My First Name isAdmin 1;<br
                    
    My Middle Name isAdmin 2;<br
                    
    My Surname isAdmin 3;<br
                    
    My Gender isfemale;<br
                    
    My Age Range is21;<br
                    
    My Religion isUnitarian;<br
                    
    My Marital Status is: ;<br
                    
    My Working Status isSelf Employed;<br
                    
    My Profession isProgrammer;<br>
                    
    My account_video_verification_code ise90412f19cc118c04effa3b552a562f066c3aa18;<br>
                    </
    font color>
                    <
    font size="4">SCRIPT END</font size></b><br>
                    <
    br>
                    </
    td
                    </
    tr
                     
                    <
    tr name="pagination"
                    <
    td colspan="10" bgcolor="#FFFFFF"Result Pages:             
                    <
    a href="/test/search_final_rows_for_each_username.php?user=admin123&page_number=1">1</a> <a href="/test/search_final_rows_for_each_username.php?user=admin123&page_number=2">2</a> <a href="/test/search_final_rows_for_each_username.php?user=admin123&page_number=3">3</a>  
                    </
    td
                    </
    tr
                       
        </
    table>     
        <
    br
        <
    br
        <
    p align="center"><span style="font-weight:bold;">Social Network User admin123 Notices in California Usa time.</span></align
        <
    br
        </
    div
        <
    br
        </
    body
        </
    html
    Last edited by UniqueIdeaMan; April 30th, 2018 at 03:39 PM.
  2. #2
  3. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,265
    Rep Power
    4193
    Your pagination link code is part of your results loop.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    831
    Rep Power
    0

    Question


    Originally Posted by kicken
    Your pagination link code is part of your results loop.

    Thanks. So, you say I should get it out of the loop ?

    Q1. So, how come I got the pagination in the result loop on other files too but they don't mix 2 pages' paginations into 1 page ? I have to learn the answer to this mystery!
    For example, look at this one that also has the pagination result in the while loop but it is not mixing 2 pages' pagination section into 1 page ....

    PHP Code:
    <?php 

    //Required PHP Files. 
    include 'config.php'
    include 
    'header.php'
    include 
    'account_header.php'

    if (!
    $conn

        
    $error mysqli_connect_error(); 
        
    $errno mysqli_connect_errno(); 
        print 
    "$errno$error\n"
        exit(); 

    else 
    {     
        
    //Get the Page Number. Default is 1 (First Page). 
        
    $page_number $_GET["page_number"]; 
        if (
    $page_number == ""
        { 
            
    $page_number 1
        } 

        
    $sender_username "$social_network_admin_username";  
        
    $recipient_username "$user"
        
    $links_per_page 2
        
    $max_result 100
        
    //$offset = ($page_number*$links_per_page)-$links_per_page; 
        
    $offset = ($page_number-1)*$links_per_page
        
    //$query_1 = "SELECT COUNT(*) FROM notices WHERE recipient_username = ? AND sender_username = ?"; 
        
    $query_1 "SELECT COUNT(*) FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id LIMIT ? OFFSET ?"
        
    $stmt_1 mysqli_prepare($conn,$query_1); 
        
    //mysqli_stmt_bind_param($stmt_1,'ss',$recipient_username,$sender_username); 
        
    mysqli_stmt_bind_param($stmt_1,'ssii',$recipient_username,$sender_username,$links_per_page,$offset);
        
    mysqli_stmt_execute($stmt_1); 
        
    $result_1 mysqli_stmt_bind_result($stmt_1,$matching_rows_count); 
        
    mysqli_stmt_fetch($stmt_1); 
        
    mysqli_stmt_free_result($stmt_1); 
        
        
    $total_pages ceil($matching_rows_count/$links_per_page); 
        
    $query_2 "SELECT id,date_and_time,recipient_username,sender_username,notice FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id LIMIT ? OFFSET ?"
        
    $stmt_2 mysqli_prepare($conn,$query_2); 
        
    mysqli_stmt_bind_param($stmt_2,'ssii',$recipient_username,$sender_username,$links_per_page,$offset); 
        
    mysqli_stmt_execute($stmt_2); 
        
    $result_2 mysqli_stmt_bind_result($stmt_2,$id,$date_and_time,$recipient_username,$sender_username,$notice); 
        
    mysqli_stmt_fetch($stmt_2);     
        
    ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"> 
        <html> 
        <head> 
        <meta content="text/html; charset=ISO-8859-1" http-equiv=" content-type"> 
        <title><?php echo "$site_name User $user Notices in $server_time time."?></title> 
        </head> 
        <body> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User $user Notices in $server_time time."?></span></align> 
        <br> 
        <br> 
        <table width="1500" border="0" cellpadding="5" cellspacing="2" bgcolor="#666666"> 
        <?php if(!$stmt_2
        { 
            
    ?> 
            <tr> 
            <td bgcolor="#FFFFFF">No record found! Try another time.</td> 
            </tr> 
            <?php 
        

        else 
        { 
            if((
    $offset+1)<=$max_result
            { 
                
    printf("<b> %d Result Found ...</b>\n",$matching_rows_count); ?><br> 
                <br> 
                <tr name="headings"> 
                <td bgcolor="#FFFFFF" name="column-heading_submission-number">Submission Number</td> 
                <td bgcolor="#FFFFFF" name="column-heading_logging-server-date-and-time">Date & Time in <?php echo "$server_time?></td> 
                <td bgcolor="#FFFFFF" name="column-heading_recipient-username">To</td> 
                <td bgcolor="#FFFFFF" name="column-heading_sender-username">From</td> 
                <td bgcolor="#FFFFFF" name="column-heading_notice">Notice</td> 
                </tr> 
                <tr name="user-details">             
                <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
                <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
                <td bgcolor="FFFFFF"  name="column-heading_recipient-username"><?php printf("%s",$recipient_username); ?></td> 
                <td bgcolor="#FFFFFF" name="column-heading_sender-username"><?php printf("%s",$sender_username); ?></td> 
                <td bgcolor="#FFFFFF" name="notice"><?php printf("%s",$notice); ?></td> 
                </tr> 
                <?php 
                
    //Use this technique: http://php.net/manual/en/mysqli-stmt.fetch.php 
                
    while(mysqli_stmt_fetch($stmt_2)) 
                { 
                    
    ?> 
                    <tr name="user-details"> 
                    <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
                    <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
                    <td bgcolor="#FFFFFF" name="recipient-username"><?php printf("%s",$recipient_username); ?></td> 
                    <td bgcolor="#FFFFFF" name="sender-username"><?php printf("%s",$sender_username); ?></td> 
                    <td bgcolor="#FFFFFF" name="notice"><?php printf("%s",$notice); ?></td> 
                    </tr> 
                    <?php 
                    ?>
     
                    <tr name="pagination"> 
                    <td colspan="10" bgcolor="#FFFFFF"> Result Pages: 
                    <?php              
                    
    if($page_number $total_pages
                    { 
                        for(
    $i=1;$i<=$total_pages;$i++) //Show Page Numbers in Serial Order. Eg. 1,2,3.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?user=$user&page_number={$i}\">{$i}</a> "
                    }             
                    else 
                    { 
                        for(
    $i=$total_pages;$i>=1;$i--) //Show Page Numbers in Reverse Order. Eg. 3,2,1.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?user=$user&page_number={$i}\">{$i}</a> "
                    } 
                    
    ?> 
                    </td> 
                    </tr> 
                    <?php 
                

            } 
        }
        
    ?>   
        </table>     
        <br> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User $user Notices in $server_time time."?></span></align> 
        <br> 
        </div> 
        <br> 
        </body> 
        </html> 
        <?php 
        
    //Free Result Set. 
        
    mysqli_stmt_free_result($stmt_2); 
        
    //Close Statement Connection. 
        
    mysqli_stmt_close($stmt_2); 
        
    //Close Database Connection. 
        
    mysqli_close($conn); 

    ?>
    Q2. So, you say I should fix this latter code too so the following part of the pagination is no longer in the while loop ?
    PHP Code:
    <tr name="pagination"> 
                    <td colspan="10" bgcolor="#FFFFFF"> Result Pages: 
                    <?php              
                    
    if($page_number $total_pages
                    { 
                        for(
    $i=1;$i<=$total_pages;$i++) //Show Page Numbers in Serial Order. Eg. 1,2,3.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?user=$user&page_number={$i}\">{$i}</a> "
                    }             
                    else 
                    { 
                        for(
    $i=$total_pages;$i>=1;$i--) //Show Page Numbers in Reverse Order. Eg. 3,2,1.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?user=$user&page_number={$i}\">{$i}</a> "
                    } 
                    
    ?> 
                    </td> 
                    </tr>
    ?

    Q3. In the latter code, do you see I have the query labelled as "query_1" ? Do you also see I have coded this line in 2 different ways ? The old version I have commented out. Using the non-commented one as the new version. But, which version is actually correct or more suitable ?
    Both versions work on this particular file but the old version did not work on another file where the query was slightly different. Again, it was showing 2 pages' pagination sections combined into 1 page. That is one mystery I solved last night. Am I on the right track ?
    I was gonna open a new thread about this but since I asked the question here then maybe I get the answer here.

    @Everybody,
    Anyone are welcome to answer my 3 questions posed at Kicken since Kicken rarely prowls this forum or the php section.
    Last edited by UniqueIdeaMan; May 1st, 2018 at 05:53 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    831
    Rep Power
    0
    I tookout the pagination section out of the while loop but no luck. The problem remains which i mentioned in my original post.
    PHP Code:
    <?php 

    //Required PHP Files. 
    include 'config.php'
    include 
    'header.php'
    include 
    'account_header.php'

    if (!
    $conn

        
    $error mysqli_connect_error(); 
        
    $errno mysqli_connect_errno(); 
        print 
    "$errno$error\n"
        exit(); 

    else 
    {     
        
    //Get the Page Number. Default is 1 (First Page). 
        
    $page_number $_GET["page_number"]; 
        if (
    $page_number == ""
        { 
            
    $page_number 1
        } 

        
    $sender_username "$social_network_admin_username"
        
    $recipient_username "$user"
        
    $links_per_page 2
        
    $max_result 100
        
    //$offset = ($page_number*$links_per_page)-$links_per_page; 
        
    $offset = ($page_number-1)*$links_per_page
        
        
    $query_1 "SELECT COUNT(*) FROM notices ORDER BY id LIMIT ? OFFSET ?"
        
    $stmt_1 mysqli_prepare($conn,$query_1);
        
    mysqli_stmt_bind_param($stmt_1,'si',$links_per_page,$offset); 
        
    mysqli_stmt_execute($stmt_1); 
        
    $result_1 mysqli_stmt_bind_result($stmt_1,$matching_rows_count); 
        
    mysqli_stmt_fetch($stmt_1); 
        
    mysqli_stmt_free_result($stmt_1); 
        
        
    $total_pages ceil($matching_rows_count/$links_per_page); 
        
    $query_2 "SELECT id,date_and_time,recipient_username,sender_username,notice FROM notices n JOIN (SELECT recipient_username,MAX(date_and_time) as date_and_time FROM notices GROUP by recipient_username) mx USING (recipient_username,date_and_time)";
        
    $stmt_2 mysqli_prepare($conn,$query_2); 
        
    mysqli_stmt_execute($stmt_2); 
        
    $result_2 mysqli_stmt_bind_result($stmt_2,$id,$date_and_time,$recipient_username,$sender_username,$notice); 
        
    mysqli_stmt_fetch($stmt_2);     
        
    ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"> 
        <html> 
        <head> 
        <meta content="text/html; charset=ISO-8859-1" http-equiv=" content-type"> 
        <title><?php echo "$site_name User $user Notices in $server_time time."?></title> 
        </head> 
        <body> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User $user Notices in $server_time time."?></span></align> 
        <br> 
        <br> 
        <table width="1500" border="0" cellpadding="5" cellspacing="2" bgcolor="#666666"> 
        <?php if(!$stmt_2
        { 
            
    ?> 
            <tr> 
            <td bgcolor="#FFFFFF">No record found! Try another time.</td> 
            </tr> 
            <?php 
        

        else 
        { 
            if((
    $offset+1)<=$max_result
            { 
                
    printf("<b> %d Result Found ...</b>\n",$matching_rows_count); ?><br> 
                <br> 
                <tr name="headings"> 
                <td bgcolor="#FFFFFF" name="column-heading_submission-number">Submission Number</td> 
                <td bgcolor="#FFFFFF" name="column-heading_logging-server-date-and-time">Date & Time in <?php echo "$server_time?></td> 
                <td bgcolor="#FFFFFF" name="column-heading_recipient-username">To</td> 
                <td bgcolor="#FFFFFF" name="column-heading_sender-username">From</td> 
                <td bgcolor="#FFFFFF" name="column-heading_notice">Notice</td> 
                </tr> 
                <tr name="user-details">             
                <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
                <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
                <td bgcolor="FFFFFF"  name="column-heading_recipient-username"><?php printf("%s",$recipient_username); ?></td> 
                <td bgcolor="#FFFFFF" name="column-heading_sender-username"><?php printf("%s",$sender_username); ?></td> 
                <td bgcolor="#FFFFFF" name="notice"><?php printf("%s",$notice); ?></td> 
                </tr> 
                <?php 
                
    //Use this technique: http://php.net/manual/en/mysqli-stmt.fetch.php 
                
    while(mysqli_stmt_fetch($stmt_2)) 
                { 
                    
    ?> 
                    <tr name="user-details"> 
                    <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
                    <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
                    <td bgcolor="#FFFFFF" name="recipient-username"><?php printf("%s",$recipient_username); ?></td> 
                    <td bgcolor="#FFFFFF" name="sender-username"><?php printf("%s",$sender_username); ?></td> 
                    <td bgcolor="#FFFFFF" name="notice"><?php printf("%s",$notice); ?></td> 
                    </tr> 
                    <tr name="pagination"> 
                    <td colspan="10" bgcolor="#FFFFFF"> Result Pages:             
                    
                    <?php 
                

                              
                    if(
    $page_number $total_pages
                    { 
                        for(
    $i=1;$i<=$total_pages;$i++) //Show Page Numbers in Serial Order. Eg. 1,2,3.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?user=$user&page_number={$i}\">{$i}</a> "
                    }             
                    else 
                    { 
                        for(
    $i=$total_pages;$i>=1;$i--) //Show Page Numbers in Reverse Order. Eg. 3,2,1.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?user=$user&page_number={$i}\">{$i}</a> "
                    } 
                    
    ?> 
                    </td> 
                    </tr> 
            <?php         
            

        }
        
    ?>   
        </table>     
        <br> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User $user Notices in $server_time time."?></span></align> 
        <br> 
        </div> 
        <br> 
        </body> 
        </html> 
        <?php 
        
    //Free Result Set. 
        
    mysqli_stmt_free_result($stmt_2); 
        
    //Close Statement Connection. 
        
    mysqli_stmt_close($stmt_2); 
        
    //Close Database Connection. 
        
    mysqli_close($conn); 

    ?>
    Guys, do remember to address my previous post, though. Aswell as this one.

    Thanks!
  8. #5
  9. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,265
    Rep Power
    4193
    Your query does not include the necessary LIMIT clause for pagination, your just selecting everything.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    831
    Rep Power
    0
    Originally Posted by kicken
    Your query does not include the necessary LIMIT clause for pagination, your just selecting everything.
    Lines 26 & 27 state:
    PHP Code:
    $links_per_page 2
    $max_result 100
    Line 68 state:
    PHP Code:
    if(($offset+1)<=$max_result
            { 
                
    printf("<b> %d Result Found ...</b>\n",$matching_rows_count); ?><br> 
                <br> 
                <tr name="headings"> 
    Aren't they enough to do the job ?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    831
    Rep Power
    0
    Kicken,

    I think I understand you now but do confirm ....
    You're saying is that, since I want the last row pulled for each Username the the $max_result pulled should be equal to the number of Usernames. Right ?
    So, since I have 5 Usernames then the $max_result should be 5 and not 100 like I have it. Is this correct ? But, I thought there is no need for this match.
    I thought the following query would do the job regardless of whether the $max_result value in number matches the number of Usernames available or not.

    PHP Code:
    $query_2 "SELECT id,date_and_time,recipient_username,sender_username,notice FROM notices n JOIN (SELECT recipient_username,MAX(date_and_time) as date_and_time FROM notices GROUP by recipient_username) mx USING (recipient_username,date_and_time)"
    I mean, that query plainly tells php to only get the final row of each Username and so since php is able to find 5 Usernames then it should just grab the last 5 rows for each Username. Right ?

    Anyway, do remember to reply to my previous post.

    @Others,
    Others are welcome to chime in ...
    (I'm starting to see my errors now a little but it would really help if I get more feed-back to eliminate the confusion).

    Thanks
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    831
    Rep Power
    0
    Folks,

    I need the answers to my previous 2 posts.
    Plus, I need the answer to this thread which requinix closed by saying I should ask the questions here.
    Wrong Number Shown As Result Found
    And so, care to answer my questions at that thread over here ?
    Don't forget my previous 2 posts over there.

    Thanks a bunch!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Location
    Cheshire, UK
    Posts
    89
    Rep Power
    72
    Answer to post #6 - No.

    Answer to post #7 - Wrong, you will not get the last five records for each username. You will get the last record for each of the five usernames.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    831
    Rep Power
    0
    Originally Posted by Barand
    Answer to post #6 - No.

    Answer to post #7 - Wrong, you will not get the last five records for each username. You will get the last record for each of the five usernames.
    Ha! Ha! Ha! What I meant was "get the last record for each username!". Since there are 5 Usernames on my tbl then I should get a total of 5 results (last entry for each 5 user).

    Regarding post 6. Oh! They are not enough to do the job ? Then care to show me an example how it should be done ? Thanks in advance!

    Guessing, I should change this:

    $max_result = 100;

    to:

    $max_result = $total_number_of_users_registered;

    Correct ? Ok, it shouldn't be hard for me to write a few lines of code to grab the total users number.
    And, I should really do it like this. Right ?

    And, how do I solve this issue ?
    Wrong Number Shown As Result Found

    Thanks man!
    Last edited by UniqueIdeaMan; May 3rd, 2018 at 06:41 AM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Location
    Cheshire, UK
    Posts
    89
    Rep Power
    72
    And, how do I solve this issue ?
    You pay attention to what Kicken told you.

    When paginating, your first query is to determine the total number of results so you can calculate the number of pages.
    The second query is repeated for each page, retrieving the required records for the page.

    Your first query is currently counting the total number of records in the table. This not the number of results that you want to display (which you have finally realized is five)
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    136
    Rep Power
    65
    Back in February, in one of your repetitive threads for this same problem, someone went to the trouble of listing the differences between the two queries involved in pagination - How To Display All Records From Tbl Using PREP STMT ?

    I recommend that you take the time to read that thread again. Your 1st query in this current thread is missing any JOIN/WHERE/GROUP BY/HAVING terms and has ORDER BY and LIMIT terms that should only be in the 2nd query.

    As stated in that thread, the core part of the queries should be the same, by building it once, then using it in both queries -

    PHP Code:
    $core_query "FROM notices n JOIN (SELECT recipient_username,MAX(date_and_time) as date_and_time FROM notices GROUP by recipient_username) mx USING (recipient_username,date_and_time)";

    $count_query "SELECT COUNT(*) $core_query";
    // execute and fetch the data from this query here...

    $data_query "SELECT id,date_and_time,recipient_username,sender_username,notice $core_query ORDER BY whatever_you_want_the_result_to_be_ordered_by LIMIT ?, ?";
    // bind any input data, execute, and fetch the data from this query here... 
    BTW - when you moved the end of the while(){} loop, you broke the html mark up by leaving part of the pagination link <tr>/<td> html inside of the loop.
    Last edited by DSmabismad; May 3rd, 2018 at 09:25 AM.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    831
    Rep Power
    0
    Originally Posted by Barand
    You pay attention to what Kicken told you.
    I did pay attention to what he said. He said:

    Originally Posted by kicken
    Your query does not include the necessary LIMIT clause for pagination, your just selecting everything.
    That is why I asked him the following wrong questions because I did not understand his hint quite:

    Originally Posted by UniqueIdeaMan
    Lines 26 & 27 state:
    PHP Code:
    $links_per_page 2
    $max_result 100
    Line 68 state:
    PHP Code:
    if(($offset+1)<=$max_result
            { 
                
    printf("<b> %d Result Found ...</b>\n",$matching_rows_count); ?><br> 
                <br> 
                <tr name="headings"> 
    Aren't they enough to do the job ?

    To which you Barand replied on behalf of Kicken:

    Originally Posted by Barand
    Answer to post #6 - No.

    And, I was puzzled to why I was wrong (according to you).

    Originally Posted by Barand
    When paginating, your first query is to determine the total number of results so you can calculate the number of pages.
    The second query is repeated for each page, retrieving the required records for the page.
    I understand your 2 statements now. But, even I understand your 2nd statement, I still never would have completed this task without DSmabismad's help by providing the code sample as this GROUPING syntax is really confusing and way over my head!

    Originally Posted by DSmabismad
    Your first query is currently counting the total number of records in the table. This not the number of results that you want to display (which you have finally realized is five)
    This is what Kicken was hinting all along. I understand clear now. Thanks Kicken!

    Anyway, DSmabismad's following reply is clear as daylight to me as of now:

    Originally Posted by DSmabismad
    Back in February, in one of your repetitive threads for this same problem, someone went to the trouble of listing the differences between the two queries involved in pagination - How To Display All Records From Tbl Using PREP STMT ?

    I recommend that you take the time to read that thread again. Your 1st query in this current thread is missing any JOIN/WHERE/GROUP BY/HAVING terms and has ORDER BY and LIMIT terms that should only be in the 2nd query.

    As stated in that thread, the core part of the queries should be the same, by building it once, then using it in both queries -

    PHP Code:
    $core_query "FROM notices n JOIN (SELECT recipient_username,MAX(date_and_time) as date_and_time FROM notices GROUP by recipient_username) mx USING (recipient_username,date_and_time)";

    $count_query "SELECT COUNT(*) $core_query";
    // execute and fetch the data from this query here...

    $data_query "SELECT id,date_and_time,recipient_username,sender_username,notice $core_query ORDER BY whatever_you_want_the_result_to_be_ordered_by LIMIT ?, ?";
    // bind any input data, execute, and fetch the data from this query here... 
    Now I did return back to that thread of mine and find that "someone" you DSmabismad is referring to is yourself. Why are you referring yourself as a third person ? I hope you didn't forget it was you because if so then I'd start worrying you got split personality issue (no offense). If you see yourself as a third person then who would you see yourself as the 1st person ? You get my point ? Unless, like actors, in your head you are now a different person. I sometimes see myself as a third person by seeing myself as another person just to get into their heads to see how they see me (eg. UI Man). I must tell you, there is a risk of living too long as that other person and then not kicking out of it and then getting trapped into a split personality issue. And so, I nowadays don't do that much and no longer care to see how others see me. Let them see me as they want.

    You see Barand, Kicken, Catacaustic and Sepodati why a code sample always helps ? Just by reading his reply it was confirmed what Barand and Kicken were saying all along but there is always that doubt that maybe I am misunderstanding things again just a little somewhere on the side. But that got put to rest when I saw his code sample.
    That is why I always ask for code samples. Not just to cut & paste but to use it as a confirmation to what I learnt from others' hints.

    Anyway, thanks to the following people for their code samples in the present & past:

    Kicken
    Barand
    DSmabismad
    Benanamen

    Slightly remember Catacaustic & Sepodati providing samples too. But not too sure.
    My earliest favourite contributor is Kicken and most latest favourites are DSmabismad and Barand.

    PS - I won't pester Kicken or Benanamen for code samples as I know you usually have luck with them when you don't ask for any.
    But, I find myself getting encouraged to pester DSmabismad and Barand. . I'll go easy with Barand as he seems to have a short temper. I'll try my luck more with DSmabismad. Don't worry, I won't run to him every single time whenever I get the chance (Im reserving Benanamen for that once I jump into pdo and maybe pester Catacaustic now and then. Lol!). Just when I'm in a really bad tight corner.
    I'll hassle Sepodati mostly like I do as I like to joke with him like a grandson does with his grandpaps! Lol! Wind him up a little bit now and then! Lol!

    Take care fellows and lookout for my upcoming threads & posts.
    Tonight, I'm taking a break from programming. Programming 7 days a week is doing my health in. Might aswell take a break and view good Youtube entertaining clips but don't go away in the weekend because I'm gonna ask you guys as much as possible regarding all these GROUPINGs and you're gonna enjoy teaching a little.

    I'll be back DSmabismad so stick to this forum whenever you can and don't get offended about what I said about the split personality thing. I see now how actors & actresses can easily catch this split personality disease.

    Take care man and once again thanks for the code sample both YOU and BARAND regarding this GROUPING subject.

    EDIT: I just couldn't with-hold myself and so here goes ...
    BARAND & DSmabismad, you fellows don't know what you have done by providing those 2 code samples regarding the GROUPINGS.
    I'm gonna learn the GROUPINGS from your codes, make changes here and there (amends to my requirements) and then add then show you 2 how I capitalized it. If things go the way planned, you 2's eye brows would fly sky high once you see $$$ being made by the public (poverty people) with a single feature that is managed by the GROUPINGs. Stay tuned for another 2wks atleast and see where the rabbit hole ends. I'll teach you 2 (as a return favour) how I'm doing all that and I hope you 2 make some heavy bucks out of it

    As a Social Network Admin, I'd like to see the last webpage each of my users saw and so I needed to learn about this GROUPING stuff.

    Now, from a Social network user or member's point of view, imagine this ...
    You login to your social network account (my SN) and clickover to "latest_webpages_viewed.php". Now, you will be presented with all the urls viewed the latest by each member you follow. And guess what, when you click those links to see what your followees have been viewing they will earn ppc income.
    Let's improve this in terms of generating more income ...

    You login to your social network account (my SN) and you no longer need to clickover to "latest_webpages_viewed.php" as there will be iframes where each iframe shows you what each of your followees viewed the latest. No need for you to click any links. The pages would auto load on the iframes and when they auto load, your followees (eg. you family members, mates, etc.) would earn ppc income.
    That is how I make use of the features I learn with php. I capitalize on them to help not so rich people earn a living out of my websites or SNs.
    You guys would feel proud that your teachings (to me) has not gone to waste and is helping people across the world get out of poverty.
    Catacaustic knows internet marketing and he knows about cpm & ppc & cpa but he sees a huge gap between what these 3 business models are and what I claim I could do with them and is confused. With one PM I can dispell all his confusion but I'm keeping him on his toes. When I show him a vid how things work, I want his heart to leap to his mouth (gulp!). Did you know I once had a username "gulp" a dacade back!

    Yeah, I know. I'm a crap php developer and worst a crap php student. But, when you see how I made use of each php feature to help people get out of poverty then I'll quickly get in your good books. Mark my words!

    Take care!
    Last edited by UniqueIdeaMan; May 4th, 2018 at 09:58 AM.

IMN logo majestic logo threadwatch logo seochat tools logo