...

View Full Version : next/previous links



myrok
01-13-2009, 04:26 PM
Hi there,

I would like to add nex/previous buttons(links) to my current script and also a number showing wich page is displayed of the total (like: page 6/12). Not with a row of numbers like in many examples on internet.
I am showing one record per page.

My question: Should/could still use the script/structure like in the examples, to eventualy accomplish my goal?

Tricky part is the fact that i want to navigate through a selected category (when selected) OR through all records (all assigments (ID)), depending on wich action the user

My code:


<?
$query = "
SELECT
id,
name,
label,
tekst,
category,
klant,
DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum
FROM
upload
";

if(isset($_GET['id']))
{
$query .= " WHERE id = '".mysql_real_escape_string($_GET['id'])."'";

}
elseif(isset($_GET['category']))
{
$query .= " WHERE category = '".mysql_real_escape_string($_GET['category'])."'";
}
else
{
echo 'No id or category given';
}


$result = mysql_query($query) or die('Error : ' . mysql_error());
$myrow = mysql_fetch_assoc($result);



?>

<!-- ### SHOW WORK ######-->

<div >
<div>
<h3> <? echo $myrow['klant'] ?></h3>
<p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'] ?></p>
<p class="newsdate">Middel: <? echo $myrow['category'] ?></p>
<p class="newsdate">Opdracht: <? echo $myrow['label'] ?></p>


<? echo '<img class=plaatje src="image/' . $myrow['name'] . '" />' ; ?>
<p class="newstext"> <? echo ($myrow['tekst']) ?> </p>
</div>
</div> ?>


I have no clue how to realize this within my two different querys..

thnx in advance for any tips/help..!

Fumigator
01-13-2009, 07:34 PM
I'm sorry, I can't visualize your data or how you are displaying it. Could you explain that part or provide a link to a test page or a screenshot or something?

myrok
01-13-2009, 08:32 PM
sure:
http://blikvoer.com/temp/samtest/index.php?pagina=portfolio

The first shown page is going to be blanc later on..

so: two different nex/previous "situations". 1.: within category (after seleceted "middelen")
2. within all (after a selected "klanten" id..)

myrok
01-19-2009, 11:06 AM
any idea?

hinch
01-19-2009, 11:22 AM
code below will do what you want change $page_rows = 4; to $page_rows = 1;

then alter the way it displays and obviously your db details and tables etc



<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("address") or die(mysql_error());

//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM topsites") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM topsites $max") or die(mysql_error());

//This is where you display your query results
while($info = mysql_fetch_array( $data_p ))
{
Print $info['Name'];
echo "<br>";
}
echo "<p>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>

myrok
01-19-2009, 11:54 AM
So you think that i can use youre code for my situation? :

More detailed, for a complete image. In another page i get the category and client list.:
grouped by category:

<a href="index.php?pagina=portfolio&category=<?php echo $category;?>"><? echo $category ?></a>

and grouped by client (id):


<a href="index.php?pagina=portfolio&id=<?php echo $id;?>"><? echo $klant ?></a>
Wich i then get in another php page, with this code:

<?php

$query = "
SELECT
id,
name,
label,
tekst,
category,
klant,
DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum
FROM
upload
";

if(isset($_GET['id']))
{
$query .= " WHERE id = '".mysql_real_escape_string($_GET['id'])."'";

}
elseif(isset($_GET['category']))
{
$query .= " WHERE category = '".mysql_real_escape_string($_GET['category'])."'";
}
else
{
echo 'Geen id of categorie opgegeven';
}


$result = mysql_query($query) or die('Error : ' . mysql_error());
$myrow = mysql_fetch_assoc($result);



?>

<!-- ### SHOW WORK ######-->

<div >
<div>
<h3> <? echo $myrow['klant'] ?></h3>
<p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'] ?></p>
<p class="newsdate">Middel: <? echo $myrow['category'] ?></p>
<p class="newsdate">Opdracht: <? echo $myrow['label'] ?></p>


<? echo '<img class=plaatje src="image/' . $myrow['name'] . '" />' ; ?>
<p class="newstext"> <? echo ($myrow['tekst']) ?> </p>
</div>
</div> ?>

And now i want to "skip" within category OR within all "id's". Based on the user choice in the other page (first codes)
But i don't understand how i should do this with pagination.. wich is mostly based on one choice.
preview: http://blikvoer.com/temp/samtest/index.php?pagina=portfolio

hinch
01-19-2009, 11:59 AM
yeah you just have to update the page count number within the script and customise the select within the script i posted to match your conditions

myrok
01-19-2009, 12:13 PM
I now have this: (im a noob at this things sry..)


<?
if*(!(isset($pagenum)))
{
$pagenum*=*1;
}

//Here*we*count*the*number*of*results
//Edit*$data*to*be*your*query
$data*=*mysql_query("
SELECT
id,
name,
label,
tekst,
category,
klant,
DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum
FROM
upload
")*or*die(mysql_error());

if(isset($_GET['id']))
{
$data .= " WHERE id = '".mysql_real_escape_string($_GET['id'])."'";

}
elseif(isset($_GET['category']))
{
$data .= " WHERE category = '".mysql_real_escape_string($_GET['category'])."'";
}
else
{
echo 'Geen id of categorie opgegeven';
}

$myrow*=*mysql_num_rows($data);

//This*is*the*number*of*results*displayed*per*page
$page_rows*=*1;

//This*tells*us*the*page*number*of*our*last*page
$last*=*ceil($myrow/$page_rows);

//this*makes*sure*the*page*number*isn't*below*one,*or*more*than*our*maximum*pages
if*($pagenum*<*1)
{
$pagenum*=*1;
}
elseif*($pagenum*>*$last)
{
$pagenum*=*$last;
}

//This*sets*the*range*to*display*in*our*query
$max*=*'limit*'*.($pagenum*-*1)***$page_rows*.','*.$page_rows;*

//This*is*your*query*again,*the*same*one...*the*only*difference*is*we*add*$max*into*it
$data_p*=*mysql_query("SELECT***FROM*topsites*$max")*or*die(mysql_error());

//This*is*where*you*display*your*query*results
while($info*=*mysql_fetch_array(*$data_p*))
{
Print*$info['klant'];
Print*$info['category'];
Print*$info['label'];

echo*"<br>";
}
echo*"<p>";

//*This*shows*the*user*what*page*they*are*on,*and*the*total*number*of*pages
echo*"*--Page*$pagenum*of*$last--*<p>";

//*First*we*check*if*we*are*on*page*one.*If*we*are*then*we*don't*need*a*link*to*the*previous*page*or*t he*first*page*so*we*do*nothing.*If*we*aren't*then*we*generate*links*to*the*first*page,*and*to*the*pr evious*page.
if*($pagenum*==*1)
{
}
else
{
echo*"*<a*href='{$_SERVER['PHP_SELF']}?pagenum=1'>*<<-First</a>*";
echo*"*";
$previous*=*$pagenum-1;
echo*"*<a*href='{$_SERVER['PHP_SELF']}?pagenum=$previous'>*<-Previous</a>*";
}

//just*a*spacer
echo*"*----*";

//This*does*the*same*as*above,*only*checking*if*we*are*on*the*last*page,*and*then*generating*the*Next* and*Last*links
if*($pagenum*==*$last)
{
}
else*{
$next*=*$pagenum+1;
echo*"*<a*href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next*-></a>*";
echo*"*";
echo*"*<a*href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last*->></a>*";
}
?>


But getting a Parse error: syntax error, unexpected '{' on

if (!(isset($pagenum)))
{
$pagenum = 1;
}

hinch
01-19-2009, 05:08 PM
well in what you've psted above you've got * in all over the place


if*(!(isset($pagenum)))
{
$pagenum*=*1;
}

if you remove the * it should work

hinch
01-19-2009, 05:08 PM
you'll also want to change the 2nd query too

//This*is*your*query*again,*the*same*one...*the*only*difference*is*we*add*$max*into*it
$data_p*=*mysql_query("SELECT***FROM*topsites*$max")*or*die(mysql_error());

myrok
01-19-2009, 11:36 PM
ok am back. I splitted my previous code to two different ones (to keep it a bit more orchanized for me)
But still i have this "page" question in the url..

My code now (for one of the pages (klanten.php ->clients)


<?
if (!isset($_GET['klant'])) {
// kijken of klant is verkregen
echo "klant niet gedefineerd";
} else {

// category pakken
$query = "
SELECT
id,
name,
label,
tekst,
category,
klant,
DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum
FROM
upload
WHERE
klant = '".mysql_real_escape_string($_GET['klant'])."'
ORDER BY datum DESC
";
$result = mysql_query($query) or die('Error : ' . mysql_error());

while ($myrow = mysql_fetch_assoc($result))

{

?>
<!-- ### WERK TONEN ######-->
<div style="width:500px;background-color:#0CF; float:right">
<div >
<div>
<h3> <? echo $myrow['klant'] ?></h3>
<p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'] ?></p>
<p class="newsdate">Middel: <? echo $myrow['category'] ?></p>
<p class="newsdate">Opdracht: <? echo $myrow['label'] ?></p>


<? echo '<img class=plaatje src="image/' . $myrow['name'] . '" />' ; ?>
<p class="newstext"> <? echo ($myrow['tekst']) ?> </p>
</div>
</div>

</div>

<?php
} // while afsluiten.
} // (else..) aflsuiten..
?>

I've tried several examples concerning pagination. But i'm stuck at this part:


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

Because i'm already sending a variable (if (!isset($_GET['klant']))... )
in the page before:


<a href="index.php?pagina=klanten&klant=<?php echo $klant;?>"><? echo $klant ?>
I just don't get (understand) how to rebuilt this for my code..
example: http://blikvoer.com/temp/samtest/index.php?pagina=klanten&klant=Anwb

hinch
01-20-2009, 01:15 AM
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}

is the page number for the sql queries its got nothing to do with your page you need to load

myrok
01-20-2009, 01:22 AM
so there won't be a
../index.php?pagina=portfolio&category=Boeken&page=1 visible?.. or..

hinch
01-20-2009, 12:00 PM
yes it'll be visible what that if statement does is tell it that if the page=xxx header querystring doesn't exist then it needs to start at page 1 else start at the page as supplied by the querystring.

myrok
01-20-2009, 02:31 PM
It's almost working now. Only problem is the next (previous ed..) button.
It's not giving the right url output.

whole code:

<? if (isset($_GET['pagina'])) {
$pagina = $_GET['pagina'];
} else {
$pagina = 1;
}

$query = "SELECT count(*)
FROM
upload
WHERE
klant = '".mysql_real_escape_string($_GET['klant'])."'
";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);

$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$rows_per_page = 1;
$lastpage = ceil($numrows/$rows_per_page);

$pagina = (int)$pagina;
if ($pagina > $lastpage) {
$pagina = $lastpage;
}
if ($pagina < 1) {
$pagina = 1;
}




$limit = 'LIMIT ' .($pagina - 1) * $rows_per_page .',' .$rows_per_page;


$query = "
SELECT
id,
name,
label,
tekst,
category,
klant,
DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum
FROM
upload
WHERE klant = '".mysql_real_escape_string($_GET['klant'])."'

$limit";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);

while ($myrow = mysql_fetch_assoc($result))

{


?>


<!-- ### SHOW WORK ######-->
<div style="width:500px;background-color:#0CF; float:right">
<div >
<div>
<h3> <? echo $myrow['klant'] ?></h3>
<p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'] ?></p>
<p class="newsdate">Middel: <? echo $myrow['category'] ?></p>
<p class="newsdate">Opdracht: <? echo $myrow['label'] ?></p>


<? echo '<img class=plaatje src="image/' . $myrow['name'] . '" />' ; ?>
<p class="newstext"> <? echo ($myrow['tekst']) ?> </p>
</div>
</div>

</div>


<?php
} // (else..) close..
?>


<?

if ($pagina == 1) {
echo " EERSTE VORIGE";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pagina=1'>EERSTE</a> ";
$prevpage = $pagina-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagina=$prevpage'>VORIGE</a> ";
}

echo " ( pagina $pagina / $lastpage ) ";

if ($pagina == $lastpage) {
echo " VOLGENDE LAATSTE ";
} else {
$nextpage = $pagina+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagina=$nextpage'>VOLGENDE</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagina=$lastpage'>LAATSTE</a> ";
}

?>

I think the problem is in {$_SERVER['PHP_SELF']}, because the url is: ./index.php?pagina=2 instead of index.php?pagina=klanten&klant=Anwb?pagina=2
(or someting like that..)
but i cannot find the exact problem..

Any idea?

hinch
01-20-2009, 02:44 PM
don't use php_SELF :)

actually construct the URL manually

echo " <a href='index.php?klant=$_GET['klant']&pagina=$nextpage'>VOLGENDE</a> ";

myrok
01-20-2009, 03:49 PM
hmm..:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/html/temp/samtest/klanten.php on line 156
wich is:


echo " <a href='index.php?klant=$_GET['klant']&pagina=$nextpage'>VOLGENDE</a> ";

hinch
01-20-2009, 04:10 PM
put {$_GET['klant']} in instead

myrok
01-20-2009, 04:23 PM
its working a bit more now with tis line:


echo " <a href='index.php?pagina=klanten&klant={$_GET['klant']}?pagina=$nextpage'>VOLGENDE</a> ";


But the next page is empty.. :S
example:
http://blikvoer.com/temp/samtest/index.php?pagina=klanten&klant=Anwb

hinch
01-20-2009, 07:10 PM
the problem is your double declaring the pagina variable

index.php?pagina=klanten&klant={$_GET['klant']}?pagina=$nextpage

see you've got pagina=klanten and pagina=$nextpage

change the 2nd pagina=$nextpage to page=$nextpage and change all the vars within the script to $page instead also you don't want the 2nd ? it should be an &

myrok
01-20-2009, 08:29 PM
thnx that was the problem! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum