2012-03-14 13 views
-1

ここでコードを貼り付けます。カテゴリを選択して別のページに移動した場合、mysql_fetch_rowsはすべてのカテゴリのすべての行を選択するため、ので、10のエントリがある場合、各カテゴリに5と私は5ページを表示する代わりに各ページごとに1ポストを表示するように設定すると10ページを示していますが、5ページ後にPHPエラーが発生します。ありがとうnは、ここに私のコード行の間に読んPHP変数値ページングの場合

$pagination_shoot = "SELECT id, po_title, po_category FROM tbl_posts WHERE po_category = '{$category}'"; 

$page_dump = mysql_query($pagination_shoot, $db_connect); 

$no_rows = mysql_fetch_row($page_dump); 

$numrows = $no_rows[0]; 

$rows_per_page = 1; 

$lastpage = ceil($numrows/$rows_per_page); 

$page = (int)$page; 

if ($page > $lastpage) { 
$page = $lastpage; 
} // if 

if ($page < 1) { 
$page = 1; 
} // if 

$limit = 'LIMIT ' .($page - 1) * $rows_per_page .',' .$rows_per_page; 

//Run Post Query 
$data_posts = "SELECT id, po_title, po_category FROM tbl_posts WHERE po_category = '{$category}' {$limit}"; //Post Query 

$fetch_data_posts = mysql_query($data_posts, $db_connect); 

while ($list_posts = mysql_fetch_array($fetch_data_posts)) 
+0

それは私には見えます。もしそうなら、[それをオフにする](http://php.net/manual/en/security.globals.php)。 – DaveRandom

+0

条件スクリプトがうまく動作する場所を削除しても、唯一の問題は、どのカテゴリがどの行数を持つかを検出できないことです – PHPSrike

答えて

0

だ、私はあなたがやろうとしているものだと思い、合計でありますどのように多くの結果を知りながらすることができますので、そのデータベースから特定のページの結果をフェッチそこにいくつのページがあるかを調べる。ここでSQL_CALC_FOUND_ROWSを使用していることを行うには正しい方法です:あなたはおそらく有効register_globals` `でサーバーを実行しているかのように

// Populate these variables however you see fit (eg through $_GET) 
$rows_per_page = 1; 
$current_page = 1; 

// ----------- 

// Make sure the page number is >= 1 
$current_page = (int) $current_page; 
if ($current_page < 1) $current_page = 1; 

// Calculate values for LIMIT clause 
$limitQty = (int) $rows_per_page; 
$limitBase = ($current_page - 1) * $limitQty; 


// Run the query 
$query = " 
    SELECT SQL_CALC_FOUND_ROWS 
    `id`, `po_title`, `po_category` 
    FROM `tbl_posts` 
    WHERE `po_category` = '".mysql_real_escape_string($category, $db_connect)."' 
    LIMIT $limitBase, $limitQty 
"; 
$result = mysql_query($query, $db_connect); 

// Store the results in an array and free the MySQL resource 
$data_posts = array(); 
while ($row = mysql_fetch_array($result)) { 
    $data_posts[] = $row; 
} 
mysql_free_result($result); 

// Get the total number of rows 
$query = " 
    SELECT FOUND_ROWS() 
"; 
$result = mysql_query($query, $db_connect); 
$numrows = mysql_fetch_row($result); 
$total_rows = $numrows[0]; 
mysql_free_result($result); 

// Calculate the total number of pages 
$total_pages = ceil($total_rows/$rows_per_page); 

if ($current_page > $total_pages) { 
    // Handle a request that asked for a page which is greater than the total number of pages 
    // You could display an error message, redirect to a page within range, or run another query 
    // to fetch the rows for the last page. 
} 

// Now you can loop the data results and do whatever you want with them 
foreach ($data_posts as $list_posts) { 
    // ... 
} 
関連する問題