MYSQL データベースから特定のクエリを取得し、結果をページ分割するスクリプトを作成しようとしています。
私は解決策が何であるかを知っていると思います。それは、それらを実装/スクリプト化する方法に関する知識が不足していることの問題です。
部分的に機能しているスクリプトは次のとおりです。
<?php
$tstart = 0;
$tend = 0;
//$tpage = 0; // the start page
//$tpages = 0; // number of pages
$tpagelinks = ""; // stores the output pagination
$ttotal = 0; / total number of results
$trows = 5; // number of results per page
$online = ""; // stores the results to display
$dbhost = "localhost";
$dbuser = "****_models";
$dbpass = "****";
$dbcon = @mysql_connect($dbhost,$dbuser,$dbpass) or die('Database error: ' . mysql_error());
$db = mysql_select_db('****_cammodels', $dbcon) or die('Database error: ' . mysql_error());
$query = "SELECT * FROM cbmodels WHERE gender='f' AND status='Public' AND age<='22'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error() . "\nQuery: $query");
while($row = mysql_fetch_array($result))
{
$online .= "#". $ttotal . " \n";
$online .= $row['status'] . "/" . $row['name'] . "/" . $row['gender'] . "/" . $row['age'] . "\n";
$online .= "<br>\n";
$ttotal ++;
}
$tstart = ($tpage * $trows) - $trows;
$tend = $tstart + $trows;
if ($tend > $ttotal) { $tend = $ttotal; }
$tpages = floor($ttotal / $trows);
if ($ttotal % $trows != 0) { $tpages ++; }
if ($tpage > $tpages) { $tpage = 1; }
if ($tpage > 1) { $tpagelinks .= "<a class='allonlinepages_url' href='new_from_db.php?page=" . ($tpage - 1) . "'>PREVIOUS</a>\n"; }
for ($ti = 1; $ti <= $tpages; $ti ++)
{
if ($tpage == $ti) { $tpagelinks .= "$ti \n"; }
else { $tpagelinks .= "<a class='allonlinepages_url' href='new_from_db.php?page=$ti' >$ti</a>\n"; }
}
if ($tpage < $tpages) { $tpagelinks .= "<a class='allonlinepages_url' href='new_from_db.php?page=" . ($tpage + 1) . "' >NEXT</a>\n"; }
echo $online;
echo $tpagelinks;
mysql_close($dbcon);
?>
データベースで見つかったすべての結果を表示しています。ページネーション カウントは正しいページ数を示しているようですが、リンクが間違っているか、結果の次のページが表示されません。
最初にページネーションの行数を取得するためにデータベースにクエリを実行し、次にページ数を計算してから、別のクエリを実行して結果を取得し、ページごとの結果として別の配列に保存する必要があると思いますか? またはその趣旨の何か。私は何度も試しましたが、上記のスクリプトは、他のすべての試みがスクリプトを壊して奇妙な結果をもたらすか、まったく機能しないようにするために得た最も近いものです。
実際のフィードからデータを取得してデータベースに保存するために作成したスクリプトは、オンラインとオフラインのフラグを立てて正しく動作するため、別のスクリプトであるため含めていません。
みんなの助けを前もってありがとう。
EDIT 動作していないコードをすべて削除 RE: コメント。