ページングをmysqlとpostgreSQLでソース比較
//ページング設定 $page = $_REQUEST['page']; if($page == ''){ $page = 1; } $page = max($page, 1); //最終ページを取得する pg_query($conn, "SELECT COUNT(*) AS cnt FROM posts"); $recordSet = pg($sql); $table = pg_fetch_assoc($recordSet); $maxPage = ceil($table['cnt'] / 5); $page = min($page,$maxPage); $start = ($page - 1) * 5; //SQLの結果から得た行数を取得 $num = pg_num_rows($result); if($num > 0){ for( $i=0; $i<$num; $i++ ){ $rows = pg_fetch_array($result, NULL, PGSQL_ASSOC); print('<div class="comments"><p class="comments_message">'.$rows['message']); print('</p><p class="comments_time">'.$rows['time'].'</p>'); print ('<p><a href="delete.php?id=' .$rows['id']. '">[削除]</a>'); print ('<a href="edit.php?id=' .$rows['id']. '">[編集]</a></p></div><!-- . comments-->'); } }
$max_num = 15; $limit = $max_num * 2; $offset = $_GET["of"]; if(empty($offset) || !ctype_digit($offset)){ $offset = 0; $pref_offset = null; }else{ $pref_offset = $offset - $max_num; } if($pref_offset <= 0){ $pref_offset = 0; } //$conn = pg_connect("..."); $sql = "SELECT * FROM posts ORDER BY id asc LIMIT $limit OFFSET $offset"; $result = pg_query($conn,$sql); $num = pg_num_rows($result); if($num > 0){ if($num <= $max_num){ $seek = $num; $next_offset = null; }else{ $seek = $max_num; $next_count = $num - $max_num; $next_offset = $offset + $max_num; } $st = $offset + 1; $ed = $offset + $seek; echo "{$st}件から{$ed}件を表示中<br />"; for($i=0;$i<$seek;$i++){ $data = pg_fetch_assoc($result,$i); //表示する echo "<div>".$data["message"]."</div>\n"; } } if(!empty($pref_offset)){ echo "<a href=\"index.php?of={$pref_offse}\">前の{$max_num}件</a>\n"; } if(!empty($next_offset)){ echo "<a href=\"index.php?of={$next_offset}\">次の{$next_count}件</a>\n"; }