Page 1 of 2

previous/next button cant make it work

Posted: Sat Aug 30, 2003 5:15 pm
by Dream
I was wondering if someone could help me out with some previous/next button.

I've been trying several scripts, but cant seem to get it to work, any ideas?
with the current code it just show all the of them, but trying to get it to show 5 layouts pr page =( any takers?

current using

Code: Select all

<?php 
include ("top.txt"); 
print "<p class=head align=center>Div // CSS Designs</p>\n"; 
$INFO&#1111;'sql_user'] = "aetherea_staff"; 
$INFO&#1111;'sql_pass'] = "*******"; 
$INFO&#1111;'sql_db'] = "aetherea_layoutsdb"; 
$INFO&#1111;'sql_host'] = "localhost"; 

$DB = mysql_connect($INFO&#1111;'sql_host'], $INFO&#1111;'sql_user'], $INFO&#1111;'sql_pass']) or die("Could not connect"); 
mysql_select_db($INFO&#1111;'sql_db']) or die("Could not select database"); 
$query = "SELECT * FROM div WHERE 1 ORDER BY `id` DESC LIMIT 0, 200"; 
$result = mysql_db_query($INFO&#1111;'sql_db'], $query); 

if (!$result) 
echo mysql_errno().": ".mysql_error()."<br />"; 
else 
&#123; 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
&#123; 
$name = $row&#1111;'name']; 
$series = $row&#1111;'series']; 
$id = $row&#1111;'id']; 
$designer = $row&#1111;'designer']; 
echo (" 
<table width='95%' align='center'> 
<tr> 
<td width='95%' class='outline' align='center'><b>$name </b></td> 
</tr> 
</table> 
<table width='95%' height='125' align='center'> 
<tr> 
<td class='outline' valign='top' width='12'><img src='http://aethereality.net/layouts/div/$id/thumb.jpg' width='200' height='125' border='0' align='left' alt='$name'> 
</td> 
<td class='outline' valign='top' width='215' style='padding-left:5px;'> 
<b>Name:</b> $name<br> 
<b>Type:</b> Div // CSS<br> 
<b>Artist:</b> $designer<br> 
<b>ID:</b> $id<br> 
<b>Report:</b> &#1111; <a href='mailto:ivytran87@hotmail.com?subject=Designs Errors || Div || $id || $name ||'>Errors</a> ]<br> 
&#1111; <a href='http://aethereality.net/layouts/div/$id/index.html' target='_blank'>Preview</a> ] &#1111; <a href='http://aethereality.net/layouts/div/$id/layout.zip'>Download</a> ]</td> 

</tr> 
</table> 
<table width='95%' align='center'> 
<tr> 
<td width='95%' class='outline' align='center'><b>$series</b> 
</td> 
</tr> 
</table> 
<br> 
"); 
&#125; 
&#125; 
mysql_close($DB); 
include("bottom.txt"); 
?>

Posted: Sat Aug 30, 2003 6:09 pm
by wmasterj
i can't seem to find anything wrong...does it give you any error - if so then post them to and we can take a look.
I tried it my own server and it gave me no big error...?

Posted: Sat Aug 30, 2003 6:28 pm
by Dream
The current script doesnt give any error, but it post all the layouts on 1 page.

I'm trying to limit it to only 5 entrys pr page

and like under the last entry a small "footer"
[Page] 1 , 2 , 3 , 4 , 5

hope this was maybe more understandable
I've been trying a few functions, but maybe I cant put the in the right place inside the script :/

Posted: Sat Aug 30, 2003 6:44 pm
by McGruff
You're LIMIT clause always finds 200 records starting from offset 0 (ie the first row). Try this:

Code: Select all

<?php
" .. LIMIT $offset, 5";
?>
..and calculate the $offset using something like:

Code: Select all

<?php
// result rows start at 0 so, if 5 items per page, need offsets: 0, 4, 9, 14 etc
$offset = ($page_number - 1) * 5 - 1;
?>
A small improvement is to define a constant for the max items per page in a config.php file. This allows you to edit the number of items per page easily.

Other improvements are to check that the $page_number passed to the script is valid ie: $page_number <= ceil($mysql_num_rows($query) / 5) - maybe choose the last valid page if out of range.

You also need to deal with $page_number <= 0.

Out of range values probably won't be generated by your scripts but could be produced by some exploratory query string tampering for example.

Posted: Sat Aug 30, 2003 7:08 pm
by Dream
I'm going to test that out ^_^ thanks.

Posted: Sat Aug 30, 2003 7:11 pm
by Dream
I'm slow, a noobie hehe.. but where should I input the <?php ?> statements you showed me?

Posted: Sat Aug 30, 2003 8:13 pm
by McGruff
First you need to pass a $page_number to the script. The first view would go to page 1 by default, thereafter the pagination links can set a page number via GET.

Just declare the $offset var before the db query (obviously) - as well as the range checking stuff.

I forgot to mention that a pagination script needs two queries: one to find the total rows as well as the LIMIT query to find the items for the page.

Posted: Sun Aug 31, 2003 4:40 pm
by Dream
This was alot harder than I thought it would be =(. was up all night trying to make it work, but unsuccessfull :/. Going to let it be for a little I think, out of energy hehe..

If anyone wants to complete the code for me your welcome, and I would be in your debt forever, but its like much work, then please dont let me take up your time :)

Going to look at it again when I get some more time.

Thanks for all your help guys, atleast I got to learn something =)

Posted: Sun Aug 31, 2003 11:20 pm
by Dream
ok.. i finally did it =)

but now i got another problem hehe.. I finally was able to make the next/previous buttons...

but now i got another problem... I have trouble importing the $rows in the table.. if you go to http://www.w2r.net/divtest.php and hold your mouse over the "preview/download" link you can see it doesnt "pick" the $row, also its not importing the $row into the table..

I'm not sure if I'm making any sense tho.. kinda hard to explain, but please look at my code :)

Code: Select all

<?php


include ("includes/config.php");

print "<p class=head align=center>Div // CSS Designs</p>\n";

$table = 'div'; // The name of your table in the database
$limit = '5'; // How many results should be shown at a time
$scroll = '10'; // How many elements to the navigation bar are shown at a time

// Get the total number of rows in a database
$query1 = mysql_query ("SELECT * FROM $table");
$numrows = mysql_num_rows ($query1);
//

if (!isset ($_GET&#1111;show])) &#123;
	$display = 1; // Change to $NUMROWS if DESCENDING order is required
&#125; else &#123;
	$display = $_GET&#1111;show];
&#125;

// Return results from START to LIMIT
$start = (($display * $limit) - $limit);
$query2 = mysql_query ("SELECT * FROM $table LIMIT $start,$limit");

while ($myrow = mysql_fetch_array ($query2)) &#123;
	echo "<p>".$myrow&#1111;ROW]."</p>"; // EDIT TO REFLECT YOUR RESULTS
	
//	include ("top.txt");
	


   
$name = $row&#1111;'name'];
$series = $row&#1111;'series'];
$id = $row&#1111;'id'];
$designer = $row&#1111;'designer'];
echo ("
<table width='95%' align='center'>
<tr>
<td width='95%' class='outline' align='center'><b>$name </b></td>
</tr>
</table>
<table width='95%' height='125' align='center'>
<tr>
<td class='outline' valign='top' width='12'><img 
src='http://aethereality.net/layouts/div/$id/thumb.jpg' width='200' 
height='125' border='0' align='left' alt='$name'>
</td>
<td class='outline' valign='top' width='215' style='padding-left:5px;'>
<b>Name:</b> $name<br>
<b>Type:</b> Div // CSS<br>
<b>Artist:</b> $designer<br>
<b>ID:</b> $id<br>
<b>Report:</b> &#1111; <a href='mailto:ivytran87@hotmail.com?subject=Designs 
Errors || Div || $id || $name ||'>Errors</a> ]<br>
&#1111; <a href='http://aethereality.net/layouts/div/$id/index.html' 
target='_blank'>Preview</a> ] &#1111; <a 
href='http://aethereality.net/layouts/div/$id/layout.zip'>Download</a> 
]</td>

</tr>
</table>
<table width='95%' align='center'>
<tr>
<td width='95%' class='outline' align='center'><b>$series</b>
</td>
</tr>
</table>
<br>
");
	
	
&#125;

$paging = ceil ($numrows / $limit);
?>
<CENTER>
<?
// Display the navigation
if ($display > 1) &#123;
	$previous = $display - 1;
	
	echo "&#1111; <a href="$_SERVER&#1111;PHP_SELF]?show=$previous">< Previous</a> | ";

&#125;

if ($numrows != $limit) &#123;
	if ($paging > $scroll) &#123;
		$first = $_GET&#1111;show];
		$last = ($scroll - 1) + $_GET&#1111;show];
	&#125; else &#123;
		$first = 1;
		$last = $paging;
	&#125;
		if ($last > $paging ) &#123;
			$first = $paging - ($scroll - 1);
			$last = $paging;
	&#125;
	for ($i = $first;$i <= $last;$i++)&#123;
		if ($display == $i) &#123;
			echo "<b>$i</b> ";
		&#125; else &#123;
			echo "<a href="$_SERVER&#1111;PHP_SELF]?show=$i">$i</a> ";
		&#125;
	&#125;
&#125;

if ($display < $paging) &#123;
	$next = $display + 1;
	echo "| <a href="$_SERVER&#1111;PHP_SELF]?show=$next">Next ></a> ] ";
	
&#125;
//
?>
</CENTER>
<?
//	include("bottom.txt");
?>

Posted: Mon Sep 01, 2003 12:21 am
by Dream

Code: Select all

while ($myrow = mysql_fetch_array ($query2)) &#123; 
   echo "<p>".$myrow&#1111;ROW]."</p>"; // EDIT TO REFLECT YOUR RESULTS
that code should be

Code: Select all

while ($row= mysql_fetch_array ($query2)) &#123; 
   echo "<p>".$row&#1111;ROW]."</p>"; // EDIT TO REFLECT YOUR RESULTS
thanks for all teh help =) I learned ALOT tonight

Posted: Tue Sep 02, 2003 5:13 pm
by Aaron
You should make a function and post it, this thing has been bugging me for ages...everything Ive tried has failed (so Im stuck with basic next and previous button - http://www.wuggawoo.co.uk/?section=thread&tid=1).

Posted: Sun Sep 07, 2003 9:22 pm
by Dream
I used alot from different premade scripts to get it the way I wanted, but I'll see if I cant put something together that will make some sence ^_^

Posted: Mon Sep 08, 2003 5:57 am
by Aaron
As you can probably see Im using it, many thanks.

To function it would be pretty useful, all you could really do is leave the variables before the sql queries and then function the paging being worked out, which really would be pointless, but would shave a few k off a few pages I guess :)

Posted: Mon Sep 08, 2003 11:06 am
by Dream
^_^

Posted: Thu Sep 11, 2003 1:57 pm
by Dream
This is the code where I took most of my input from (some are rewritten to suit my own script better tho)

PHP Syntax

There are 2 parts for this code :

Code Part A :

// dynamic navigation variables
$rows_per_page=10;
$total_records=mysql_num_rows($result);
$pages = ceil($total_records / $rows_per_page);
mysql_free_result($result);

if (!isset($screen))
$screen=0;
$start = $screen * $rows_per_page;
$q .= "LIMIT $start, $rows_per_page";
$result= mysql_db_query($dbase, $q, $connection) or die
("Could not execute query : $q." . mysql_error());

Code Part B :

// create the dynamic links
if ($screen > 0) {
$j = $screen - 1;
$url = "$PHP_SELF?screen=$j";
echo "<a href=\"$url\">Prev</a>";
}

// page numbering links now

for ($i = 0; $i < $pages; $i++) {
$url = "$PHP_SELF?screen=" . $i;
$j = $i + 1;
echo " | <a href=\"$url\">$j</a> | ";
}

if ($screen < $pages) {
$j = $screen + 1;
$url = "$PHP_SELF?screen=$j";
echo "<a href=\"$url\">Next</a>";
}

Explaination of Syntax

$rows_per_page=10;
Here we declare that we want 10 records in a page. You can set any number you wish here.

$total_records=mysql_num_rows($result);
This one is straight forward, we are declaring a variable $total_records and that have to be the number of rows returned by the query.

$pages = ceil($total_records / $rows_per_page);
Here we set a mathematic rule in which $pages is $total_records divided by $rows_per_page. Then we round up the remainder. This is pretty much common sense since you want to calculate how many pages you want php to generate. After the division, the remainder should made up the last page so you round it up with ceil function.

mysql_free_result($result);
This one is a standard practice that we free the result variable. Optional but good programming practice to free the memory.

if (!isset($screen))
$screen=0;
$start = $screen * $rows_per_page;
Here we tell php that if $screen variable is not set, then $screen will be 0. $start is our first page and is declared as $screen multiply by $rows_per_page. So, you can manually calculate that the first page will be $start=0 since $screen has to be 0.

$q .= "LIMIT $start, $rows_per_page";
$result= mysql_db_query($dbase, $q, $connection) or die
("Could not execute query : $q." . mysql_error());
The dot (.) after $q actually means that the string will be added to the previous query. If you named your sql query, $query, then you have to change this line to $query.. Here, we tell MySQL that we are adding a 2nd portion to the previous query we set. The previous query of course will be what your script does. The 2nd line of course tells MySQL to execute the query or output an error message if failed.

if ($screen > 0) {
$j = $screen - 1;
$url = "$PHP_SELF?screen=$j";
echo "<a href=\"$url\">Prev</a>";
}
This tells php, that if $screen is more than 0 (ie, visitor is not reading the first page), a previous link is to be generated.

for ($i = 0; $i < $pages; $i++) {
$url = "$PHP_SELF?screen=" . $i;
$j = $i + 1;
echo " | <a href=\"$url\">$j</a> | ";
}
This tells php that the visitor is reading the $i page, and the code will loop until the maximum pages, $pages is reached. This will output the number of pages and their links.

if ($screen < $pages-1) {
$j = $screen + 1;
$url = "$PHP_SELF?screen=$j";
echo "<a href=\"$url\">Next</a>";
}
This will output the next link as long as the $screen is not on the last page as declared by $pages.

Usage

Basically, you put in Code Part A after your initial query, and Code Part B where you want the navigation to appear.

Conclusion

This tutorial is a little complex in code, but this is one of the easiest method to output the dynamic navigation. There are more efficiency ways to do the same thing but they are much more complex to code.