Muzica Veche
Muzica Veche

Reputation: 65

PHP pagination add ID

I have a kind of problem, with following php code:

$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;

that code offer me pagination (u allready know that) , and the url bar adress look's like following:

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

i want to look like following: http://www.site.ro/folder/lista.php?citat=SOME-NUMBERS&page=PAGE-NUMBER

my database table its populated like that:

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

i want to extract data from "citat" column , so link from url bar will look like:

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

every time when i press on next page buton, will look like:

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

etcetera.. how can i modify that code? Thank in advance !

Upvotes: 0

Views: 759

Answers (3)

steven
steven

Reputation: 4875

I am ignoring all your security issues.

This will work as long you display only one item per page:

$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'];
}

and later:

$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> "; 
    }
}

Upvotes: 1

Niket Malik
Niket Malik

Reputation: 1105

Get value of citat from while loop:

$citat = $row['citat'];

Then you can use $citat wherever and however you want, and to check the citat parameter in url you can do it by:

if (isset($_GET['citat'])) {
  //it's there do something
} else {
  //it's absent
}

Upvotes: 0

Andrew
Andrew

Reputation: 528

Do you want to filter database results by 'citat' value?

If so, then you have to build links with get parameter 'citat' in them, like:

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

and later take GET['citat'] and add it in the sql query where part so that only results with certain citat value would be returned, like:

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

NOTE: This is not real example and it is WRONG TO USE LIKE WRITTEN: you must escape GET['citat'] because otherwise your database will be hacked very easily and very soon!

Upvotes: 0

Related Questions