Jump to content

[SOLVED] Pagination


uwictech

Recommended Posts

Hi All,

 

I think I'm finally getting somewhere with pagination!

 

I can now submit a query and get the correct number of records back with the correct number of pages.

 

My only problem is, when I press the page numbers or the next button the records disappear.

 

If anyone gets time, could you take a look at my code and tell me where I’m going wrong please?

 

Jamie

 

<?php

require("conf.php");


//max displayed per page
$per_page = 2;

//get start variable
$start = $_GET['start'];

$udfin = $_POST['udf'];

//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM cy_equ WHERE udf = $udfin"));

//count max pages
$max_pages = $record_count / $per_page; //may come out as decimal

if (!$start)
   $start = 0;

//display data
$get = mysql_query("SELECT * FROM cy_equ WHERE udf = $udfin LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))
{
// get data
$id = $row['id'];
$ext = $row['ext'];
$equ = $row['equ'];

echo $id." (".$ext.") (".$equ.")<br />";

}

//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;

//show prev button
if (!($start<=0))
       echo "<a href='qqq.php?start=$prev'>Prev</a> ";

//show page numbers

//set variable for first page
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
if ($start!=$x)
    echo " <a href='qqq.php?start=$x'>$i</a> ";
else
    echo " <a href='qqq.php?start=$x'><b>$i</b></a> ";
$i++;
}

//show next button
if (!($start>=$record_count-$per_page))
       echo " <a href='qqq.php?start=$next'>Next</a>";

?>
<h3> UDF Query </h3>
<form name="form1" method="post" action="http://localhost/qqq.php">
<p> Input UDF: <input type="number" name="udf" value= "<?php echo $udfin; ?>" size="1" maxlength="4"/></p><br>


<p><input type="submit" name="submit" value="GET DATA"/></p>


</form>

Link to comment
Share on other sites

On your first page submission (ie when you first type in a udf) you are using $_POST['udf'] to find out which udf you wish to look at. Since as soon as you click on a link to another page the $_POST array will be reset you will need to persist the value by amending it to your url. So you URL will be page.php?udf=1&start=2. You will then need to make edit your code to use this value, a quick demo (that's perhaps not perfect)...

 

if(isset($_POST['udf'])) {
   $udfin = $_POST['udf'];
} elseif(isset($_GET['udf'])) {
   $udfin = $_GET['udf'];
} else {
   $udfin = 1; // some default value
}

You will also need to edit your pagination to add udf=$udfin to each link.

Link to comment
Share on other sites

Thanks for your input Pete.

 

I think I have been looking at this to long, it's taken up so much of my time, I'm willing to give anything to get this to work!

 

I have made some changes, I don't this they are correct as its defaulting to the default number given in the, if else statement at the top of my code when I press the next button.

 

I know I useless at this guy's, I can't tell you how much I would appreciate it if someone could help me further.

 

Jamie

 

<?php

require("conf.php");


//max displayed per page
$per_page = 5;

//get start variable
$start = $_GET['start'];

if(isset($_POST['udf'])) {
   $udfin = $_POST['udf'];
} elseif(isset($_GET['udf'])) {
   $udfin = $_GET['udf'];
} else {
   $udfin = 1;
}

//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM cy_equ WHERE udf = $udfin"));

//count max pages
$max_pages = $record_count / $per_page; //may come out as decimal

if (!$start)
   $start = 0;

//display data
$get = mysql_query("SELECT * FROM cy_equ WHERE udf = $udfin LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))
{
// get data
$id = $row['id'];
$ext = $row['ext'];
$equ = $row['equ'];
$udf = $row['udf'];
$udf1 = $row['udf1'];
$udf2 = $row['udf2'];

echo  " ID (".$id.") EXT (".$ext.") EQU (".$equ.") UDF (".$udf.") UDF1 (".$udf1.") UDF2 (".$udf2.")<br />";

}

//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;


//show next button
if (!($start>=$record_count-$per_page))
       echo " <a href='qqq.php?udf=1&start=2'>Next</a>";

//show prev button
if (!($start<=0))
       echo "<a href='qqq.php?udf=1&start=2'>Prev</a> ";

//show page numbers

//set variable for first page
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
if ($start!=$x)
    echo " <a href='qqq.php?start=$x'>$i</a> ";
else
    echo " <a href='qqq.php?start=$x'><b>$i</b></a> ";
$i++;
}



?>
<h3> UDF Query </h3>
<form name="form1" method="post" action="http://localhost/qqq.php">
<p> Input UDF: <input type="number" name="udf" value= "<?php echo $udfin; ?>" size="1" maxlength="4"/></p><br>

<p> Q = <font color="red"><?php echo $id; ?> </font> </p>
<p> EXT = <font color="red"><?php echo $ext; ?> </font> </p>
<p> EQU = <font color="red"><?php echo $equ; ?> </font></p>
<p> UDF = <font color="red"><?php echo $udf; ?> </font></p>
<p> UDF2 = <font color="red"><?php echo $udf2; ?> </font></p>
<p> UDF3 = <font color="red"><?php echo $udf3; ?> </font></p>

<p><input type="submit" name="submit" value="GET DATA"/></p>


</form>

Link to comment
Share on other sites

try

<?php

require("conf.php");


//max displayed per page
$per_page = 5;

//get start variable
if (isset($_GET['start']))$start = $_GET['start']; else $start = 0;


if(isset($_POST['udf'])) {
   $udfin = $_POST['udf'];
} elseif(isset($_GET['udf'])) {
   $udfin = $_GET['udf'];
} else {
   $udfin = 1;
}

//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM cy_equ WHERE udf = $udfin"));

//count max pages
$max_pages = $record_count / $per_page; //may come out as decimal

if (!$start)
   $start = 0;

//display data
$get = mysql_query("SELECT * FROM cy_equ WHERE udf = $udfin LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))
{
// get data
$id = $row['id'];
$ext = $row['ext'];
$equ = $row['equ'];
$udf = $row['udf'];
$udf1 = $row['udf1'];
$udf2 = $row['udf2'];

echo  " ID (".$id.") EXT (".$ext.") EQU (".$equ.") UDF (".$udf.") UDF1 (".$udf1.") UDF2 (".$udf2.")<br />";

}

//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;


//show next button
if (!($start>=$record_count-$per_page))
       echo " <a href='qqq.php?udf=$udfin&start=$next'>Next</a>";

//show prev button
if (!($start<=0))
       echo "<a href='qqq.php?udf=$udfin&start=$prev'>Prev</a> ";

//show page numbers

//set variable for first page
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
if ($start!=$x)
    echo " <a href='qqq.php?udf=$udfin&start=$x'>$i</a> ";
else
    echo " <a href='qqq.php?udf=$udfin&start=$x'><b>$i</b></a> ";
$i++;
}



?>
<h3> UDF Query </h3>
<form name="form1" method="post" action="http://localhost/qqq.php">
<p> Input UDF: <input type="number" name="udf" value= "<?php echo $udfin; ?>" size="1" maxlength="4"/></p><br>

<p> Q = <font color="red"><?php echo $id; ?> </font> </p>
<p> EXT = <font color="red"><?php echo $ext; ?> </font> </p>
<p> EQU = <font color="red"><?php echo $equ; ?> </font></p>
<p> UDF = <font color="red"><?php echo $udf; ?> </font></p>
<p> UDF2 = <font color="red"><?php echo $udf2; ?> </font></p>
<p> UDF3 = <font color="red"><?php echo $udf3; ?> </font></p>

<p><input type="submit" name="submit" value="GET DATA"/></p>


</form>

Link to comment
Share on other sites

Thanks Sasa, that worked brilliantly!!! :-*

 

One more thing if possible?

 

I have set up dynamaic pages so that if someone clicks on a link to go to the udf search page the url looks like this http://localhost/index1.php?page=udfsearch

 

When I setup the next buttons they obviously run on the same page, at the moment the link is set up run on my test page which is called qqq so the link is

<a href='qqq.php?udf=$udfin&start=$next'>Next</a>";

 

If I want it to run on my dynamic page what do I change the link to?

 

Jamie

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.