0

我有一种问题,使用以下 php 代码:

$host="localhost";
$user_name="";
$pwd="";
$database_name="";

$conexiune = mysql_connect($host,$user_name,$pwd) or die("Nu ma pot conecta la         MySQL!");
mysql_select_db($database_name, $conexiune) or die("Nu gasesc baza de date");

if (isset($_GET["page"])) {
    $page  = $_GET["page"];
} else {
    $page=1;
};

$start_from = ($page-1) * 1;
$sql = "SELECT * FROM citate ORDER BY id DESC LIMIT $start_from, 1";
$rs_result = mysql_query ($sql,$conexiune);


while ($row = mysql_fetch_assoc($rs_result))
    echo "<img src='" . $row['poza'] . "' />
             <br />
             " . $row['titlu'] . "
             <br />
             " . $row['descriere'] . "
             <br />
                     " . $row['data'] . "
        ";

$sql = "SELECT COUNT(id) FROM citate";
$rs_result = mysql_query($sql,$conexiune);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 1);

$pagelink ='<a href="lista.php?page='.($page-1).'"><<</a> ';
$pagelink_2='<a href="lista.php?page='.($page+1).'">>></a> ';

if($page>1)
    echo $pagelink;

if($page<2)
    echo "";

for ($i=1; $i<=$total_pages; $i++) {
    if ($i != $page)
        echo "<a href='lista.php?page=".$i."'>".$i."</a> "; // xxxx = your page url address
    if ($i==$page)
        echo " <strong>". $i . "</strong> "; // defining class in the style sheet you         can add colour or border to the displayed number
};

if($page<$total_pages)
    echo $pagelink_2;

该代码为我提供了分页(你已经知道了),并且 url 栏地址如下所示:

    http://www.site.ro/folder/lista.php?page=PAGE-NUMBER

我想看起来像以下: http ://www.site.ro/folder/lista.php?citat=SOME-NUMBERS&page=PAGE-NUMBER

我的数据库表是这样填充的:

  --------------------------------------------------------------
  |  id | poza | titlu | descriere | citat | data | accesari   |
  --------------------------------------------------------------

我想从“citat”列中提取数据,所以来自 url 栏的链接将如下所示:

    http://www.site.ro/folder/lista.php?citat=EXTRACTED-FROM-CITAT&page=PAGE-NUMBER

每次我按下下一页按钮时,都会如下所示:

    http://www.site.ro/folder/lista.php?citat=2748925&page=1
    http://www.site.ro/folder/lista.php?citat=2840194&page=2

等等..我该如何修改该代码?预先感谢 !

4

3 回答 3

1

我忽略了你所有的安全问题。

只要您每页仅显示一项,这将起作用:

$last_citat = 0;
while ($row = mysql_fetch_assoc($rs_result)) {
    echo "<img src='" . $row['poza'] . "' /><br />" . $row['titlu'] . "<br />" . $row['descriere'] . " <br />" . $row['data'] . "";
    $last_citat = $row['citat'];
}

然后:

$pagelink ='<a href="lista.php?citat='.$last_citat.'&page='.($page-1).'"><<</a> ';
$pagelink_2='<a href="lista.php?citat='.$last_citat.'&page='.($page+1).'">>></a> ';

if($page>1) { echo $pagelink; }
if($page<2) { echo ""; }

for ($i=1; $i<=$total_pages; $i++) {
    if ($i != $page) {
        echo "<a href='lista.php?citat=".$last_citat."&page=".$i."'>".$i."</a> "; 
    }
    if ($i==$page) {
        echo " <strong>". $i . "</strong> "; 
    }
}
于 2013-08-22T11:16:38.027 回答
0

您想按“citat”值过滤数据库结果吗?

如果是这样,那么您必须在其中建立带有获取参数“citat”的链接,例如:

<a href="lista.php?citat=NUMBER&page='.($page-1).'"><<</a> 

然后获取 GET['citat'] 并将其添加到 sql 查询where部分,以便仅返回具有特定 citat 值的结果,例如:

$sql = "SELECT * FROM citate WHERE citat = '".GET['citat']."' ORDER BY id DESC LIMIT $start_from, 1";

注意:这不是真实的例子,而且使用起来是错误的:你必须转义GET['citat']因为否则你的数据库很快就会被黑客入侵!

于 2013-08-22T11:09:37.247 回答
0

从 while 循环中获取 citat 的值:

$citat = $row['citat'];

然后,您可以$citat 随心所欲地使用,并检查 url 中的 citat 参数,您可以通过以下方式进行:

if (isset($_GET['citat'])) {
  //it's there do something
} else {
  //it's absent
}
于 2013-08-22T11:12:57.437 回答