[WordPress]独自設定画面で指定DBからデータ取得
functions.php
<?php function my_menu() { add_menu_page('独自設定画面','my menu','manage_options','my_menu','my_options_page','',58); } add_action('admin_menu', 'my_menu'); function my_options_page() { $wpm = new ThisCaseModel(); $wpdb = new wpdb('DBID','DBPW','DBNAME','DBHOST'); $page = empty($_GET['_page']) ? 1 : $_GET['_page']; $sql = array(); $sql[] = "SELECT u.id,u.name"; $sql[] = "FROM users AS u"; $sql[] = "LEFT JOIN others AS o ON u.id = o.user_id"; $sql[] = "WHERE o.id IS NOT NULL"; $sql[] = "ORDER BY u.id ASC"; $sql[] = "LIMIT " . $wpm->get_sql_limit_value_by_page($page,20); $users = $wpdb->get_results(implode(" ",$sql)); array_pop($sql); array_pop($sql); $sql[0] = "SELECT COUNT(*) AS count"; $count = $wpdb->get_results(implode(" ",$sql)); $page_info = $wpm->get_page_no_info($page,$count[0]->count,20); $page_numbers = $wpm->get_page_numbers_for_pager((int)$page_info['current'],(int)$page_info['last_page_no']); include TEMPLATEPATH . DIRECTORY_SEPARATOR . 'admin-my_menu.php'; }
admin-my_menu.php
<style> .users,.users th,.properties td{ border-collapse: collapse; border:1px solid #ccc; } .users th,.users td{ padding: 3px 5px; } .pager{ display: flex; } .pager a{ display: block; border:1px solid #ccc; margin-right: 5px; padding: 3px 10px; } .pager a:link{ text-decoration: none; } </style> <div class="container"> <h2>my menu</h2> <div> <div>件数:<?php echo $page_info['count'];?></div> <div><?php echo $page_info['current'];?>/<?php echo $page_info['last_page_no'];?></div> </div> <table class="users"> <tr> <th>ID</th> <th>name</th> </tr> <?php foreach ($users as $user):?> <tr> <td><?php echo $wpm->esc_html($user->id);?></td> <td><?php echo $wpm->esc_html($user->name);?></td> </tr> <?php endforeach;?> </table> <div class="pager"> <?php foreach ($page_numbers as $page_number):?> <?php if(empty($page_number)):?> … <?php else:?> <a href="<?php echo $wpm->get_url_by_current_url($page_number<2 ? array('exclude_keys'=>array('_page')) : array('exclude_keys'=>array('_page'),'add_values'=>array('_page'=>$page_number)));?>"><?php echo $page_number;?></a> <?php endif;?> <?php endforeach;?> </div> </div>