0

I'm doing some pagination in PHP but i run into some problems. I did pagination, and everything is working well but what i wanted is to add a form on the page in which user could type in how much items he wants on each page. The problem is, when i enter number of pages (eg. 3), it works fine for the first time and lists 3 items, but when i click on "next page" it again lists default number of elements (2). I've searched everything and anything but can't really find what seems to be a problem. I'm kinda new to php so i guess that i don't get some things yet, but I hope it's not too much of a trouble for you to take look at this and tell me what you think. Thanks in advance! I did it like this:

form:

<?php
    print("
        <form style='padding-left:5px;' method='get'>Broj komponenti po stranici:<input type='text' name='cpp'   /><input class='button3' type='submit' name='cppb' value='Promijeni' /></form>
    ");

check if there is some thing stored in cpp:

$rec_limit = ($_GET['cpp']);
if(!(isset($_GET['cpp']))) { $rec_limit = 2; } //if there is no  cpp set, let it be 2 (default)

right here is database and pagination part (i don't think that this is the part that makes troubles):

$sql = "SELECT count(id) FROM komponenta ";
$retval = mysql_query($sql, $conn);
if(!$retval) {
    die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
$max_pages = ($rec_count / $rec_limit) -1;


if(isset($_GET{'page'}) ) {
    $page = $_GET{'page'} + 1;
    $offset = $rec_limit * $page ;
} else {
    $page = 0;
    $offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);

$sql = "SELECT * ".
    "FROM komponenta ".
    "LIMIT $offset, $rec_limit";

$retval = mysql_query($sql, $conn);
if(!$retval) {
    die('Could not get data: ' . mysql_error());
}
print("<TABLE class='tablica' border=‘1’>");
print("<TR>");
print("<TD></TD>");
print("<TD>Vrsta</TD>");
print("<TD>Proizvođač</TD>");
print("<TD>Frekv</TD>");
print("<TD>Izlazna</TD>");
print("<TD>Dobavljač</TD>");
print("<TD>Status</TD>");
print("<TD>Datum kupnje</TD>");
print("<TD>Datum zaprimanja</TD>");
print("<TD>Opis</TD>");
print("<TD>Napomena</TD>");
print("<TD>Komada</TD>");
print("<TD>Metara</TD>");
print("</TR>");

$br=0;
while($row = mysql_fetch_array($retval, MYSQL_NUM)) {
    print("<TR>");
    print("<TD>". $br . "</TD>");
    print("<TD>". $row["1"]. "</TD>");
    print("<TD>" . $row["2"]. "</TD>");
    print("<TD>" . $row["3"]. "</TD>");
    print("<TD>" . $row["4"]. "</TD>");
    print("<TD>" . $row["5"]. "</TD>");
    print("<TD>" . $row["6"]. "</TD>");
    print("<TD>" . $row["7"]. "</TD>");
    print("<TD>" . $row["8"]. "</TD>");
    print("<TD>" . $row["9"]. "</TD>");
    print("<TD>" . $row["10"]. "</TD>");
    print("<TD>" . $row["11"]. "</TD>");
    print("<TD>" . $row["12"]. "</TD>" );
    print("</TR>");
    $br++;
} 
print("</TABLE>");

and here is the part that bothers me:

if( $page >= $max_pages ) {
    $last = $page - 2;
    echo "<b class='paragraf1'>Page: ". $page ." </b><a class='button3' href=\"protected_page.php?page=$last?cpp=$rec_limit\"><text class='buttontxt'>Last " . $rec_limit . " Records</text></a>";
    /*with ?cpp=$rec_limit i set the url
    (when i press "next") to have for eg ..."?cpp=3"... and it does appear so,
    but the $_GET['cpp'] from the beggining of code obviously doesn't read this..
    Well, at least it doesn't store it into $rec_limit*/
} else if($page > 0) {
    $last = $page - 2;
    echo "<b class='paragraf1'>Page: ". $page ." </b><a class='button3' href=\"protected_page.php?page=$last?cpp=$rec_limit\"><text class='buttontxt'>Last " . $rec_limit . " Records</text></a> ";
    echo "<a class='button3' href=\"protected_page.php?page=$page?cpp=$rec_limit\"><text class='buttontxt'>Next " . $rec_limit . " Records</text></a>";
} else if($page == 0) {
    echo "<b class='paragraf1'>Page: ". $page ." </b><a class='button3' href=\"protected_page.php?page=$page?cpp=$rec_limit\"><text class='buttontxt'>Next " . $rec_limit . " Records</text></a>";
}

1 Answer 1

1
protected_page.php?page=$page?cpp=$rec_limit

replace the 2nd ? by &

protected_page.php?page=$page&cpp=$rec_limit

When you're passing variables in the url after the script name there is a ? then between each variable and the other it is and & that separates.

and at your place I would replace that too:

    if(!(isset($_GET['cpp']))){$rec_limit=2;} 

by:

    if(!isset($_GET['cpp']) || (int)$rec_limit<=0){ $rec_limit=2; }  
Sign up to request clarification or add additional context in comments.

2 Comments

Oh my god, you are kidding me. I literally spent about 3 hours on this. I'm such a noob, I should have known that. Thank you so much man! I love you!
You saved @user3243547's life while your reputation was 911. That's funny. Now I am going to mess it up by upvoting for sharp eyes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.