WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Page navigation buttons to the admin wordpress plugin page (15 posts)

  1. _OMEGA_
    Member
    Posted 4 years ago #

    Hi all, I think that my ideas are always hard and strange.
    I'm adding more things to my plugin and at the moment I'd like to add the page navigation buttons, like: 1 2 3 .... >> or << Previous Page | Next Page >>

    Unfortunatelly the problem is that in my wordpress plugin admin page I do something like this:

    <?php
    // I want to limit the visualization of the results to 5 per page
    $sql = "SELECT * FROM MyTable ORDER BY CURDATE() DESC LIMIT 5";
    
    $posts_date = $wpdbtest->get_results($sql);
    
    foreach ($posts_date as $post)  {
    ?>
    <table>
    <tr>
    <td><?php echo $post->id_category; ?></td>
    </tr>
    </table>
    <?php } ?>

    I tried to add the standard code for the page navigation buttons, but obviously it doesn't work.

    Any hints on how can I modify the code to add the page navigation buttons??

  2. _OMEGA_
    Member
    Posted 4 years ago #

    I'm play with the edit.php file of the WordPress core.
    1. I've copied/pasted it in my own plugin directory;
    2. renamed it: myfile-edit.php;
    Inside I'm starting to delete some code, so please don't judge if there's still something useless, but I'm making some tries on it.

    <?php
    /**
     * Edit Posts Administration Panel.
     *
     * @package WordPress
     * @subpackage Administration
     */
    
    /** WordPress Administration Bootstrap */
    
    if ( empty($title) )
    	$title = __('Last News Added');
    $parent_file = 'myfile-edit.php';
    wp_enqueue_script('inline-edit-post');
    
    list($post_stati, $avail_post_stati) = wp_edit_posts_query();
    
    require_once('admin-header.php');
    
    if ( !isset( $_GET['paged'] ) )
    	$_GET['paged'] = 1;
    
    if ( empty($_GET['mode']) )
    	$mode = 'list';
    else
    	$mode = esc_attr($_GET['mode']); ?>
    
    <div class="wrap">
    <?php screen_icon(); ?>
    <h2><?php echo esc_html( $title ); ?>  <?php
    
    foreach ( $post_stati as $status => $label ) {
    	$class = '';
    
    	if ( !in_array( $status, $avail_post_stati ) )
    		continue;
    
    	if ( empty( $num_posts->$status ) )
    		continue;
    
    	if ( isset($_GET['id_published']) && $status == $_GET['id_published'] )
    		$class = ' class="current"';
    
    	$status_links[] = "<li><a href='myfile-edit.php?id_published=$status'$class>" . sprintf( _n( $label[2][0], $label[2][1], $num_posts->$status ), number_format_i18n( $num_posts->$status ) ) . '</a>';
    }
    //echo implode( " |</li>\n", $status_links ) . '</li>';
    unset( $status_links );
    
    ?>
    </ul>
    
    <?php if ( have_posts() ) { ?>
    
    <div class="tablenav">
    <?php
    $page_links = paginate_links( array(
    	'base' => add_query_arg( 'paged', '%#%' ),
    	'format' => '',
    	'prev_text' => __('&laquo;'),
    	'next_text' => __('&raquo;'),
    	'total' => $wp_query->max_num_pages,
    	'current' => $_GET['paged']
    ));
    
    ?>
    
    <?php // view filters
    if ( !is_singular() ) {
    
    $arc_query = "SELECT * FROM MyTable WHERE id_published = 'published' ORDER BY id_col_date DESC";
    
    $arc_result = $wpdb->get_results( $arc_query );
    
    $month_count = count($arc_result);
    
    echo 'Count: '.$month_count; // in MyTable I have 2 News Added, in fact I see this output ->   COUNT: 2
    //but in the table of the news that wordpress creates I still see the Articles entries.
    
     }
    } ?>
    </div>
    
    <?php if ( $page_links ) { ?>
    <div class="tablenav-pages"><?php $page_links_text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s–%s of %s' ) . '</span>%s',
    	number_format_i18n( ( $_GET['paged'] - 1 ) * $wp_query->query_vars['posts_per_page'] + 1 ),
    	number_format_i18n( min( $_GET['paged'] * $wp_query->query_vars['posts_per_page'], $wp_query->found_posts ) ),
    	number_format_i18n( $wp_query->found_posts ),
    	$page_links
    ); echo $page_links_text; ?></div>
    <?php } ?>
    
    <div class="clear"></div>
    </div>
    
    <div class="clear"></div>
    
    <?php include( 'myfile-post-rows.php' ); ?>
    
    <div class="tablenav">
    
    <?php
    if ( $page_links )
    	echo "<div class='tablenav-pages'>$page_links_text</div>";
    
     ?>
    
    <?php inline_edit_row( 'post' ); ?>
    
    <div id="ajax-response"></div>
    <br class="clear" />
    </div>

    Unfortunatelly this continues to display the Articles data, instead of the MyTable result.

    Any hint??

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    Example of some very basic paging.. (going back to your first piece of code)..

    // If the query variable for paging is set and more then one, then use it to determine the page, else it's page 1
    $my_paging = ( isset( $_GET['paged'] ) && $_GET['paged'] > 1 ) ? intval( $_GET['paged'] ) : 1;
    
    // Number to show per page
    $per_page = 5;
    
    // If page is more than one, create the offset, per page divided by page number(minus one), else no offset.
    $offset = ( $my_paging > 1 ) ? ( $per_page * ( $my_paging - 1 ) ) : 0;
    
    // Your query, with necessary offsets
    $sql = "SELECT * FROM MyTable ORDER BY CURDATE() DESC LIMIT $offset, $per_page";
    
    $posts_date = $wpdbtest->get_results($sql);
    
    // Ensures you have a result from the query
    if( !$posts_date || empty( $posts_date ) ) die("No results in the query");
    
    // If the count of the results in the query is more then per page
    if( count( $posts_date ) > $per_page ) {
       // Round up the count divided by the amount to show per page
       $total_pages = ceil( count( $posts_date ) / $per_page );
    }
    else {
       // Else it's less or equal to, so there can only be one page (there's not enough to have another page)
       $total_pages = 1;
    }

    That's excluding any navigation links, just wanted to give you a basic example of paging your query..

    Beyond that, you can see further examples by googling for "PHP paging mysql" .. it's where i started when i first wanted to do paging in PHP and MySQL ...

    If you want some additional help, that's fine with me, but i'm not going to write it all for you... ;)

  4. _OMEGA_
    Member
    Posted 4 years ago #

    Thank you for the help, later I'll test it.

    but i'm not going to write it all for you... ;)

    I didn't ask this and I will not ask this! :D

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    ;)

    No problem... you're welcome..

    Post back if you have problems..

  6. _OMEGA_
    Member
    Posted 4 years ago #

    Back :P
    I'm working on this code:

    <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
    <?
    
    $my_hostname = "localhost";
    $my_db_user = "*******";
    $my_db_password = "*********";
    $my_database = "**********";
    
    $wpdbtest_otherdb = new wpdb($my_db_user, $my_db_password, $my_database, $my_hostname);
    
    $page_name="page-admin.php"; //  If you use this code with a different page ( or file ) name then change this
    $start=$_GET['start'];
    if(strlen($start) > 0 and !is_numeric($start)){
    echo "Data Error";
    exit;
    }
    
    $eu = ($start - 0);
    $limit = 2;                                 // No of records to be shown per page.
    $this1 = $eu + $limit;
    $back = $eu - $limit;
    $next = $eu + $limit; 
    
    /////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
    $query2=" SELECT * FROM MyTable ";
    $result2=mysql_query($query2);
    echo mysql_error();
    $nume=mysql_num_rows($result2);
    /////// The variable nume above will store the total number of records in the table////
    
    /////////// Now let us print the table headers ////////////////
    $bgcolor="#f1f1f1";
    echo "<TABLE width=50% align=center  cellpadding=0 cellspacing=0> <tr>";
    echo "<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Image</font></td>";
    echo "<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Name</font></td>";
    echo "<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Category</font></td></tr>";
    
    ////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
    $query=" SELECT * FROM MyTable limit $eu, $limit ";
    $result=mysql_query($query);
    echo mysql_error();
    
    //////////////// Now we will display the returned records in side the rows of the table/////////
    while($noticia = mysql_fetch_array($result))
    {
    if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
    else{$bgcolor='#f1f1f1';}
    
    echo "<tr >";
    echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[id_image]</font></td>";
    echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[id_name]</font></td>";
    echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[id_category]</font></td>"; 
    
    echo "</tr>";
    }
    echo "</table>";
    ////////////////////////////// End of displaying the table with records ////////////////////////
    
    ///////////////////////////////
    if($nume > $limit ){ // Let us display bottom links if sufficient records are there for paging
    
    /////////////// Start the bottom links with Prev and next link with page numbers /////////////////
    echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
    //// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
    if($back >=0) {
    print "<a href='$page_name?start=$back'><font face='Verdana' size='2'>PREV</font></a>";
    }
    //////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
    echo "</td><td align=center width='30%'>";
    $i=0;
    $l=1;
    
    $self = $_SERVER['PHP_SELF'];
    
    for($i=0;$i < $nume;$i=$i+$limit){
    if($i <> $eu){
    echo " <a href='$page_name?paged=$i'><font face='Verdana' size='2'>$l</font></a> ";
    }
    else { echo "<font face='Verdana' size='4' color=red>$l</font>";}        /// Current page is not displayed as link and given font color red
    $l=$l+1;
    }
    
    echo "</td><td  align='right' width='30%'>";
    ///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
    if($this1 < $nume) {
    print "<a href='$page_name?paged=$next'><font face='Verdana' size='2'>NEXT</font></a>";
    }
    echo "</td></tr></table>";
    
    }// end of if checking sufficient records are there to display bottom navigational link.
    ?>
    
    </body>

    It seems working, but when I press the 'Next' or the '2' button I have the problem.
    It seems an URL problem, my admin page where I see correctly the first page with the datas is:

    http://127.0.0.1:8181/wordpress/wp-admin/admin.php?page=last-adds

    When I press one of the two buttons, I'm redirected to:

    http://127.0.0.1:8181/wordpress/wp-admin/page-admin.php?paged=2

    And wordpress says to me:

    Error 404 - Not Found

    Any hint?

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    Yeah, build your URLs in the same format as the plugin page URL.

    If your page is.

    /wp-admin/admin.php?page=last-adds

    ..then the URLs for paged links should be..

    /wp-admin/admin.php?page=last-adds&paged=1

    ..etc..

    NOTE: You should format your code with tabs(or spaces) if there's a chance someone else will have to look at it or work on it, it's painful to read without any indentation.

  8. _OMEGA_
    Member
    Posted 4 years ago #

    You right x2, but usually:
    1. what I write is only for me :)
    2. when I write and test some code I'm lost in thoughts :P

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    No problem, i'm just saying code is far easier to read and understand if it's properly formatted.

    You also have some invalid code and deprecated tags in your HTML, take this line for example..

    <td align=left bgcolor=$bgcolor id='title'>

    Attributes should include quotes.

    bgcolor is deprecated, as is align, you should use style if you want to use inline CSS..
    ie.

    <td style='text-align:left;background-color:$bgcolor'>

    id should only be applied to single element (once on the whole page), if you're applying it to lots of elements use a class ... <td class='someclassname'>

    There's also no need to use the mysql functions, mysql_query and so on... wordpress provides several functions for queries already, and it's pointless having this line if you're going to use the mysql functions..

    $wpdbtest_otherdb = new wpdb($my_db_user, $my_db_password, $my_database, $my_hostname);

    If you need to perform a query, then utilise the wpdb connection you've created above..

    $some_query = $wpdbtest_otherdb->get_results("SELECT * FROM example");
    // $some_query now has an array of objects if a result was returned
  10. _OMEGA_
    Member
    Posted 4 years ago #

    Oh cool for the last hint, really helpful.
    I feel a little guilty with you, cause I'm wasting your time :(

    I've changed the URL in this way:

    $page_name="admin.php?page=last-adds";
    
    for($i=0;$i < $nume;$i=$i+$limit){
    	if($i <> $eu){
    		echo " <a href='$page_name&paged=$i'><font face='Verdana' size='2'>$l</font></a> ";
    	} else {
    		echo "<font face='Verdana' size='4' color=red>$l</font>";
    		}        /// Current page is not displayed as link and given font color red
    	$l=$l+1;
    }
    
    echo "</td><td  align='right' width='30%'>";
    ///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
    if($this1 < $nume) {
    	print "<a href='$page_name&paged=$next'><font face='Verdana' size='2'>NEXT</font></a>";}
    	echo "</td></tr></table>";
    
    }// end of if checking sufficient records are there to display bottom navigational link.

    And when I press the 'Next' or '2'button I've noticed that the URL is changed to:

    http://127.0.0.1:8181/wordpress/wp-admin/admin.php?page=last-adds&paged=2

    but it displays the first page with some results, it doesn't really change the page.

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

    Print out your sql query so you can see if the offset is correct for each page.

    If something doesn't give the result you expect dump/print the data that you think is incorrect ...

    var_dump( $your_var );

    or

    print '<pre>';
    print_r( $your_var );
    print '</pre>';
  12. _OMEGA_
    Member
    Posted 4 years ago #

    Hey back with news....good news :)

    Used the code of the 6th post, modified a lot of parts of it and now finally everything works great.

    Thank you for the chatty and for the hints that helped me to think and fix my problem :)

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    You're welcome ... ;)

    Glad i could help.

  14. _OMEGA_
    Member
    Posted 4 years ago #

    Ok ok I've fixed the page with the code, but..... :)
    Another problem.

    In the "Displayed page" I have this code:

    function display()
    {
     $my_hostname = "localhost";
     $my_db_user = "*******";
     $my_db_password = "*********";
     $my_database = "**********";
    
     $wpdbtest_otherdb = new wpdb($my_db_user, $my_db_password,
                                    $my_database, $my_hostname);
    
     if(isset($_GET['id_nation']))
     {
      $id_nation=mysql_real_escape_string($_GET['id_nation']);
      $myresults = $wpdbtest_otherdb->get_results("SELECT * FROM MyTable WHERE id_nation = '".$id_nation."' ORDER BY id_name ASC");
    
       foreach ($myresults as $myresult)
       {
       ?>
        <table width="500px" height="145px">
        <tr>
        <td>$myresult->id_category</td>
        <td>$myresult->id_name</td>
        </tr>
        </table>
        <?
        }
      }
    ?>

    Now I want to add the pagination here, but now the problem appears.
    This is the working code that I use in the other page:

    $page_name="admin.php?page=myprivatepage/paged.php"; 
    
    $start=$_GET['start'];
    
    if(strlen($start) > 0 and !is_numeric($start)){
    	echo "Data Error";
    	exit;
    }
    
    $eu = ($start - 0); 
    
    $limit = 2;    // No of records to be shown per page by default.
    $this1 = $eu + $limit;
    $back = $eu - $limit;
    $next = $eu + $limit; 
    
    /////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
    $query2=" SELECT * FROM MyTable ";
    
    $result2=mysql_query($query2);
    	echo mysql_error();
    
    $nume=mysql_num_rows($result2);
    /////// The variable nume above will store the total number of records in the table////
    
    /////////// Now let us print the table headers ////////////////
    $bgcolor="#f1f1f1";
    echo "<p><TABLE width=90% align=center border=1> <tr>";
    echo "<td  align='center' style='background-color: #ffcc66; color: #990000; width: 250px; text-align: center; font-weight: 700'>Image</td>";
    echo "<td  align='center' style='background-color: #ffcc66; color: #990000; width: 250px; text-align: center; font-weight: 700'>Name</td>";
    echo "</tr>";
    
    ////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
    $query=" SELECT * FROM MyTable ORDER BY id_col_date DESC limit $eu, $limit ";
    $result=mysql_query($query);
    	echo mysql_error();
    
    //////////////// Now we will display the returned records in side the rows of the table/////////
    while($protot = mysql_fetch_array($result))
    {
    	if($bgcolor=='#f1f1f1'){
    		$bgcolor='#ffffff';
    		} else {
    		$bgcolor='#f1f1f1';
    		}
    
    echo "<tr >";
    echo "<td>$protot[id_image]</td>";
    echo "<td>$protot[id_name]</td>";
    echo "</tr>";
    }
    echo "</table>";
    ////////////////////////////// End of displaying the table with records ////////////////////////
    
    /////////////// Start the buttom links with Prev and next link with page numbers /////////////////
    echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
    //// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
    if($back >=0) {
    	print "<a href='$page_name&start=$back&limit=$limit'><font face='Verdana' size='2'>PREV</font></a>";
    }
    //////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
    	echo "</td><td align=center width='30%'>";
    
    $i=0;
    $l=1;
    
    for($i=0;$i < $nume;$i=$i+$limit){
    	if($i <> $eu){
    		echo " <a href='$page_name&start=$i&limit=$limit'><font face='Verdana' size='2'>$l</font></a> ";
    	} else {
    		echo "<font face='Verdana' size='4' color=red>$l</font>";
    	}        /// Current page is not displayed as link and given font color red
    	$l=$l+1;
    }
    
    echo "</td><td  align='right' width='30%'>";
    ///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
    if($this1 < $nume) {
    	print "<a href='$page_name&start=$next&limit=$limit'><font face='Verdana' size='2'>NEXT</font></a>";
    }
    echo "</td></tr></table>";

    Unfortunatelly I don't be able to add the code above on the one of the "Displayed page" preserving the code, just adding this one!

    Any hint as usual? :D

  15. _OMEGA_
    Member
    Posted 4 years ago #

    Solved!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.